Skip to content
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

"error: [RefersToDaggerCodegen] Don't refer to Dagger's internal or generated code" flagged by error prone 2.3.4 #321

Closed
davido opened this issue May 1, 2020 · 7 comments · May be fixed by #332

Comments

@davido
Copy link
Contributor

davido commented May 1, 2020

We cannot upgrade Error Prone in Bazel, because of this issue. The last attempt to bump EP in Bazel to 2.3.3: [1], [2], [3] was reverted because of this breakage.

Recent EP releases added new bug pattern checks that Bazel eco system would like to benefit from.

I've re-done EP version upgrade in Bazel in this PR: [4].

However, the same issue as in EP 2.3.3 shows up, when trying to build src/main/... target.

I uploaded a reproducer to demonstrate the breakage: [5]. Note though, you need Linux machine, as I only published draft java_tool release with bumped EP version 2.3.4 on Linux platform:

  $ bazel build --java_toolchain=@remote_java_tools_linux//:toolchain --host_java_toolchain=@remote_java_tools_linux//:toolchain src/main/kotlin/...
DEBUG: /home/davido/.cache/bazel/_bazel_davido/17e96fe2f67c13d592b106b04a8a5443/external/bazel_toolchains/rules/version_check.bzl:45:14: 
Current running Bazel is not a release version and one was not defined explicitly in rbe_autoconfig target. Falling back to '0.23.0'
INFO: Analyzed 23 targets (38 packages loaded, 1391 targets configured).
INFO: Found 23 targets...
INFO: From Building external/com_google_protobuf/libprotobuf_java.jar (123 source files, 1 source jar):
warning: -parameters is not supported for target value 1.7. Use 1.8 or later.
INFO: From Building external/com_google_protobuf/libprotobuf_java.jar (123 source files, 1 source jar) [for host]:
warning: -parameters is not supported for target value 1.7. Use 1.8 or later.
ERROR: /home/davido/projects/rules_kotlin/src/main/kotlin/io/bazel/kotlin/builder/BUILD:16:13: Building src/main/kotlin/io/bazel/kotlin/builder/libbuilder.jar (2 source files) and running annotation processors (ComponentProcessor) failed (Exit 1): java failed: error executing command 
  (cd /home/davido/.cache/bazel/_bazel_davido/17e96fe2f67c13d592b106b04a8a5443/execroot/io_bazel_rules_kotlin && \
  exec env - \
    LC_CTYPE=en_US.UTF-8 \
    PATH=/home/davido/.yarn/bin:/home/davido/.config/yarn/global/node_modules/.bin:/home/davido/pgm/apache-maven-3.5.2/bin:/home/davido/bin:/home/davido/.yarn/bin:/home/davido/.config/yarn/global/node_modules/.bin:/home/davido/pgm/google-cloud-sdk/bin:/home/davido/pgm/apache-maven-3.5.2/bin:/home/davido/bin:/home/davido/bin:/usr/local/bin:/usr/bin:/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin \
  external/remotejdk11_linux/bin/java -XX:+UseParallelOldGC -XX:-CompactStrings '--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED' '--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED' '--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED' '--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED' '--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED' '--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED' '--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED' '--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED' '--patch-module=java.compiler=external/remote_java_tools_linux/java_tools/java_compiler.jar' '--patch-module=jdk.compiler=external/remote_java_tools_linux/java_tools/jdk_compiler.jar' '--add-opens=java.base/java.nio=ALL-UNNAMED' '--add-opens=java.base/java.lang=ALL-UNNAMED' -jar external/remote_java_tools_linux/java_tools/JavaBuilder_deploy.jar @bazel-out/host/bin/src/main/kotlin/io/bazel/kotlin/builder/libbuilder.jar-0.params @bazel-out/host/bin/src/main/kotlin/io/bazel/kotlin/builder/libbuilder.jar-1.params)
Execution platform: @local_config_platform//:host
bazel-out/host/bin/src/main/kotlin/io/bazel/kotlin/builder/_javac/builder/libbuilder_sourcegenfiles/io/bazel/kotlin/builder/KotlinBuilderComponent_Module_ProvidePluginArgEncoderFactory.java:27: error: [RefersToDaggerCodegen] Don't refer to Dagger's internal or generated code
    return providePluginArgEncoder(module, toolchainProvider.get());
                                  ^
    (see https://errorprone.info/bugpattern/RefersToDaggerCodegen)
bazel-out/host/bin/src/main/kotlin/io/bazel/kotlin/builder/_javac/builder/libbuilder_sourcegenfiles/io/bazel/kotlin/builder/KotlinBuilderComponent_Module_ProvidePluginArgEncoderFactory.java:37: error: [RefersToDaggerCodegen] Don't refer to Dagger's internal or generated code
    return Preconditions.checkNotNull(instance.providePluginArgEncoder(toolchain), "Cannot return null from a non-@Nullable @Provides method");
                                     ^
    (see https://errorprone.info/bugpattern/RefersToDaggerCodegen)
INFO: Elapsed time: 85.065s, Critical Path: 74.15s
INFO: 37 processes: 24 linux-sandbox, 13 worker.
FAILED: Build did NOT complete successfully

I also wrote this issue upstream: [5].

[1] bazelbuild/bazel#9286
[2] bazelbuild/bazel#10023
[3] bazelbuild/bazel@d990746
[4] bazelbuild/bazel#11271
[5] #320
[6] google/error-prone#1599

@davido
Copy link
Contributor Author

davido commented May 1, 2020

Interestingly, If I apply this patch: [1] to dagger@HEAD repository itself, I can reproduce exactly the same breakage, but only because Dagger is using rules_kotlin for unit tests:

  $ cd dagger
  $ bazel build --java_toolchain=@remote_java_tools_linux//:toolchain --host_java_toolchain=@remote_java_tools_linux//:toolchain javatests/...
  [...]
  NFO: Analyzed 262 targets (0 packages loaded, 0 targets configured).
INFO: Found 262 targets...
ERROR: /home/davido/.cache/bazel/_bazel_davido/c102f8eec66d1f794b7036f88f33298b/external/io_bazel_rules_kotlin/src/main/kotlin/io/bazel/kotlin/builder/BUILD:35:13: Building external/io_bazel_rules_kotlin/src/main/kotlin/io/bazel/kotlin/builder/libbuilder.jar (2 source files) and running annotation processors (ComponentProcessor) failed (Exit 1)
bazel-out/host/bin/external/io_bazel_rules_kotlin/src/main/kotlin/io/bazel/kotlin/builder/_javac/builder/libbuilder_sourcegenfiles/io/bazel/kotlin/builder/KotlinBuilderComponent_Module_ProvidePluginArgEncoderFactory.java:23: error: [RefersToDaggerCodegen] Don't refer to Dagger's internal or generated code
    return providePluginArgEncoder(module, toolchainProvider.get());
                                  ^
    (see https://errorprone.info/bugpattern/RefersToDaggerCodegen)
bazel-out/host/bin/external/io_bazel_rules_kotlin/src/main/kotlin/io/bazel/kotlin/builder/_javac/builder/libbuilder_sourcegenfiles/io/bazel/kotlin/builder/KotlinBuilderComponent_Module_ProvidePluginArgEncoderFactory.java:33: error: [RefersToDaggerCodegen] Don't refer to Dagger's internal or generated code
    return Preconditions.checkNotNull(instance.providePluginArgEncoder(toolchain), "Cannot return null from a non-@Nullable @Provides method");
                                     ^
    (see https://errorprone.info/bugpattern/RefersToDaggerCodegen)
INFO: Elapsed time: 3.897s, Critical Path: 3.64s
INFO: 16 processes: 1 linux-sandbox, 15 worker.
FAILED: Build did NOT complete successfully

//CC @ronshapiro

[1] http://paste.openstack.org/show/792979

@netdpb
Copy link

netdpb commented May 1, 2020

That EP rule looks for @Generated("dagger.internal.codegen.ComponentProcessor"), which Dagger emits when generating code unless neither @Generated annotation appears to be available—in which case Dagger at least generates a comment. But your generated code doesn't have a @Generated annotation. Do you know why that would be?

@davido
Copy link
Contributor Author

davido commented May 1, 2020

@netdpb No idea. I wrote this issue upstream google/dagger#1831.

@davido
Copy link
Contributor Author

davido commented May 1, 2020

What happens is that "javax.annotation" dependency is erroneously missing in kotlin build tool chain. As the consequence, Dagger cannot emit "Generated" annotation, and new EP version is flagging generated code as broken, as it cannot realized that the code itself is generated code.

Will upload a fix in a moment.

davido added a commit to davido/rules_kotlin that referenced this issue May 1, 2020
Fixes bazelbuild#321.

Bazel/Dagger integration requires javax.annotation to be on the
classpath to generate @generated annotation.

Recent ErrorProne releases (started from 2.3.3) added new bug pattern:
RefersToDaggerCodegen that is flagging the code as invalid, as it
doesn't realized that the code is generated.

Due to this problem, a previous attemt to bump EP version in Bazel was
reverted. Fixing it would allow us to make another attempt to update
EP version in Bazel.
davido added a commit to davido/rules_kotlin that referenced this issue May 1, 2020
Fixes bazelbuild#321.

Bazel/Dagger integration requires javax.annotation to be on the
classpath to generate @generated annotation.

Recent ErrorProne releases (started from 2.3.3) added new bug pattern:
RefersToDaggerCodegen that is flagging the code as invalid, as it
doesn't realize that the code is generated.

Due to this problem, a previous attemt to bump EP version in Bazel was
reverted. Fixing it would allow us to make another attempt to update
EP version in Bazel.
davido added a commit to davido/rules_kotlin that referenced this issue May 1, 2020
Fixes bazelbuild#321.

Bazel/Dagger integration requires javax.annotation to be on the
classpath to generate @generated annotation.

Recent ErrorProne releases (started from 2.3.3) added new bug pattern:
RefersToDaggerCodegen that is flagging the code as invalid, as it
doesn't realize that the code is generated.

Due to this problem, a previous attemt to bump EP version in Bazel was
reverted. Fixing it would allow us to make another attempt to update
EP version in Bazel.

Test Plan:

A. Involved approach:

1. Bump EP version to 2.3.4
2. Conduct custom java tools release with new EP version
3. Consume custom java tools release in WORKSPACE
4. Try to bulid rule_kotlin would fail with RefersToDaggerCodegen error

B. Simple approach

1. Apply this patch
2. Build rules_kotlin
3. Confirm that this class:
   KotlinBuilderComponent_Module_ProvidePluginArgEncoderFactory
   is annotated with @generated annotation:

Generated(
    value = "dagger.internal.codegen.ComponentProcessor",
    comments = "https://dagger.dev"
)
@SuppressWarnings({
    "unchecked",
    "rawtypes"
})
public final class KotlinBuilderComponent_Module_ProvidePluginArgEncoderFactory
[...]
davido added a commit to davido/rules_kotlin that referenced this issue May 1, 2020
Fixes bazelbuild#321.

Bazel/Dagger integration requires javax.annotation to be on the
classpath to generate @generated annotation.

Recent ErrorProne releases (started from 2.3.3) added new bug pattern:
RefersToDaggerCodegen that is flagging the code as invalid, as it
doesn't realize that the code is generated.

Due to this problem, a previous attemt to bump EP version in Bazel was
reverted. Fixing it would allow us to make another attempt to update
EP version in Bazel.

Test Plan:

A. Involved approach:

1. Bump EP version to 2.3.4
2. Conduct custom java tools release with new EP version
3. Consume custom java tools release in WORKSPACE
4. Try to bulid rule_kotlin would fail with RefersToDaggerCodegen error

B. Simple approach

1. Apply this patch
2. Build rules_kotlin
3. Confirm that this class:
   KotlinBuilderComponent_Module_ProvidePluginArgEncoderFactory
   is annotated with @generated annotation:

@generated(
    value = "dagger.internal.codegen.ComponentProcessor",
    comments = "https://dagger.dev"
)
@SuppressWarnings({
    "unchecked",
    "rawtypes"
})
public final class KotlinBuilderComponent_Module_ProvidePluginArgEncoderFactory
[...]
davido added a commit to davido/rules_kotlin that referenced this issue May 1, 2020
Fixes bazelbuild#321.

Bazel/Dagger integration requires javax.annotation to be on the
classpath to generate @generated annotation.

Recent ErrorProne releases (started from 2.3.3) added new bug pattern:
RefersToDaggerCodegen that is flagging the code as invalid, as it
doesn't realize that the code is generated.

Due to this problem, a previous attemt to bump EP version in Bazel was
reverted. Fixing it would allow us to make another attempt to update
EP version in Bazel.

Test Plan:

A. Involved approach:

1. Bump EP version to 2.3.4 in Bazel
2. Conduct custom java tools release with new EP version
3. Consume custom java tools release in WORKSPACE in rules_kotlin
4. Trying to bulid rules_kotlin would fail with RefersToDaggerCodegen
   error

B. Simple approach:

1. Apply this patch
2. Build rules_kotlin
3. Confirm that this class:
   KotlinBuilderComponent_Module_ProvidePluginArgEncoderFactory
   is annotated with @generated annotation:

@generated(
    value = "dagger.internal.codegen.ComponentProcessor",
    comments = "https://dagger.dev"
)
@SuppressWarnings({
    "unchecked",
    "rawtypes"
})
public final class KotlinBuilderComponent_Module_ProvidePluginArgEncoderFactory
[...]
@cgruber
Copy link
Collaborator

cgruber commented May 6, 2020

I see - so this is an issue with building the builder itself. We can add this, but your issue might be resolved automatically by upgrading to 1.4.0 (and its release candidates) as the builder is shipped as a pre-built binary, so you should never execute the code path you're hitting.

What version of the rules were you using?

@davido
Copy link
Contributor Author

davido commented May 7, 2020

What version of the rules were you using?

I don't use rules_kotlin. I try to upgrade EP in Bazel.

For that I bumped EP version to 2.3.4, conducted new java_tools release, upgraded java_tools in rules_kotlin and building with custom javatoolchain rules_kotlin from HEAD with this new EP and the build is failing.

Let me know if you need a reproducer.

@cgruber cgruber closed this as completed in d59d27b May 9, 2020
davido added a commit to davido/rules_kotlin that referenced this issue May 16, 2020
Fixes bazelbuild#321.

Dagger transitively depends on this depedency and it is already fetched
by rules_jvm_external, so that it can be used and recently added
explicit dependency on javax.annotation can be removed.
@davido
Copy link
Contributor Author

davido commented May 16, 2020

@netdpb

But your generated code doesn't have a @generated annotation. Do you know why that would be?

  Execution platform: @local_config_platform//:host

I figured out what happens. kotlin builder is built for host configuration. Bazel is using remote Java 11. That why bootclasspath does not contain java.annotation.Generated:

  $ unzip -t bazel-out/host/bin/external/bazel_tools/tools/jdk/platformclasspath.jar | grep javax.annotation
    testing: javax/annotation/processing/AbstractProcessor.class   OK
    testing: javax/annotation/processing/Completion.class   OK
    testing: javax/annotation/processing/Completions$SimpleCompletion.class   OK
    testing: javax/annotation/processing/Completions.class   OK
    testing: javax/annotation/processing/Filer.class   OK
    testing: javax/annotation/processing/FilerException.class   OK
    testing: javax/annotation/processing/Generated.class   OK
    testing: javax/annotation/processing/Messager.class   OK
    testing: javax/annotation/processing/ProcessingEnvironment.class   OK
    testing: javax/annotation/processing/Processor.class   OK
    testing: javax/annotation/processing/RoundEnvironment.class   OK
    testing: javax/annotation/processing/SupportedAnnotationTypes.class   OK
    testing: javax/annotation/processing/SupportedOptions.class   OK
    testing: javax/annotation/processing/SupportedSourceVersion.class   OK

The failing compilation is using -source 8 -target 8 with a JDK 11 -bootclasspath (Bazel remote JDK 11 for host configuration). So AutoAnnotation is trying to use javax.annotation.Generated based on the source level, but it isn't available because the JDK 11 bootclasspath only contains javax.annotation.processing.Generated .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants