-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose current repository name to Java with @AutoBazelRepository #16534
Conversation
2ad9baa
to
e743290
Compare
@cushon I followed your recommendation in #16281 (comment) and experimented with annotation processing rather than JavaBuilder magic. The ergonomics seem decent enough and the only change to JavaBuilder is passing the repository name via The integration test is most likely not in the correctly place, I only put it there so that the test only runs against java_tools built from HEAD. How would I do this properly - split up the PR into two parts with a java_tools update in between? @Wyverald Do you think this is usable? |
I don't understand the test failure - locally, |
6c63677
to
bb84246
Compare
I changed this commit so that the flag is injected in |
bb84246
to
30c63c4
Compare
out.printf(" * The canonical name of the repository containing the Bazel target that\n"); | ||
out.printf(" * compiled {@link %s}.\n", annotatedClass.getQualifiedName().toString()); | ||
out.printf(" */\n"); | ||
out.printf(" static final String BAZEL_REPOSITORY = \"%s\";\n", repositoryName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cushon Given that the class is called AutoBazelRepository_X
, should I maybe replace this with NAME
? More concise and less repetitive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did this in a separate commit because it just seemed better, let me know if you disagree and I can revert.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good!
src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java
Outdated
Show resolved
Hide resolved
private void emitClass(TypeElement annotatedClass) { | ||
// This option is always provided by JavaBuilder. | ||
String repositoryName = processingEnv.getOptions() | ||
.getOrDefault("bazel.repository", "for testing only"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be better, that this results in an error? User wants to get repository, but they get a string "for testing only". This can only result in a hard to debug problems.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made this a hard error.
@bazel-io flag |
@bazel-io fork 6.0.0 |
src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java
Outdated
Show resolved
Hide resolved
Java targets depending on `@bazel_tools//tools/java/runfiles` can add the new `@AutoBazelRepository` to a class to have an annotation processor generate a companion class with a `BAZEL_REPOSITORY` constant containing the repository name of the target that compiled the class.
fd68e9c
to
c4ddafa
Compare
src/main/java/com/google/devtools/build/lib/rules/java/JavaInfoBuildHelper.java
Outdated
Show resolved
Hide resolved
Java targets depending on `@bazel_tools//tools/java/runfiles` can add the new `@AutoBazelRepository` to a class to have an annotation processor generate a companion class with a `BAZEL_REPOSITORY` constant containing the repository name of the target that compiled the class. This requires a small addition to JavaBuilder to parse the repository name out of the target label and pass it to javac as a processor option. Work towards bazelbuild#16124 Closes bazelbuild#16534. PiperOrigin-RevId: 487573496 Change-Id: Id9b6526ce32268089c91c6d17363d1e7682f64a4
) Java targets depending on `@bazel_tools//tools/java/runfiles` can add the new `@AutoBazelRepository` to a class to have an annotation processor generate a companion class with a `BAZEL_REPOSITORY` constant containing the repository name of the target that compiled the class. This requires a small addition to JavaBuilder to parse the repository name out of the target label and pass it to javac as a processor option. Work towards #16124 Closes #16534. PiperOrigin-RevId: 487573496 Change-Id: Id9b6526ce32268089c91c6d17363d1e7682f64a4
Java targets depending on
@bazel_tools//tools/java/runfiles
can add the new@AutoBazelRepository
to a class to have an annotation processor generate a companion class with aBAZEL_REPOSITORY
constant containing the repository name of the target that compiled the class.This requires a small addition to JavaBuilder to parse the repository name out of the target label and pass it to javac as a processor option.
Work towards #16124