rules_kotlin icon indicating copy to clipboard operation
rules_kotlin copied to clipboard

Running java_plugin in compile_phase

Open Kvarnefalk opened this issue 2 years ago • 7 comments

Hey there!

Summary

I'm having issues getting a java_plugin run in the compile_phase of a mixed-mode kt_jvm_library on the Java sources. The only examples I can see are java plugins applying in the annotation phase. I've setup a demo project here. You can see how I defined my rules here.

Running below:

 bazel build  //src/main/java/com/mixed/example:libkt

and then

jar tf bazel-bin/src/main/java/com/mixed/example/libkt.jar

Indicates that the compiler plugin didn't run. However, passing default javacoptions to the toolchain from the command line invocation makes it work:

bazel build //src/main/java/com/mixed/example:libkt --javacopt="'-Xplugin:semanticdb -build-tool:bazel'"^C

That approach works on this demo example, however, as soon as you have java_proto_library etc, it fails, because we can't pass a plugin jar to those rules, and the execution fails with: error: plug-in not found: semanticdb.

I've also tried wrapping my java_plugin as a kt_compiler_plugin but that doesn't seem to work neither.

Next steps

Modify kt_javac_options

Could we add another option to the kt_javac_options. Allowing you to do something like:

kt_javac_options(
  name = "plugin_opts",
  x_plugins = ["semanticdb"]
)

Pass javac flags directly

I assume this is not preferable, but could you imagine passing these javac flags directly to the kt_jvm_library? Similar to how it's done on java_library.

Other Workarounds?

Since it seems like the kt_jvm_library is failing to pass the correct javacopts flags to the compiler, is there any way to make that happen, without patching rules_kotlin or passing flags from the command line? Any idea why this is happening?

Kvarnefalk avatar Sep 20 '22 11:09 Kvarnefalk

@Kvarnefalk This currently does not work in rules_kotlin and it's something that we probably need to support, and we'll need to do some further investigation to figure out how to best handle this one.

You might be able to work around it with some clever usages of java_package_configuration to opt specific packages into the --javacopt="'-Xplugin:semanticdb opts without enabling it everywhere.

Bencodes avatar Sep 23 '22 06:09 Bencodes

Hey! I see. Thanks for the pointer. Let me try to work around this by using the java_package_configuration

Kvarnefalk avatar Sep 28 '22 14:09 Kvarnefalk

@Kvarnefalk were you able to get this working?

Bencodes avatar Oct 14 '22 20:10 Bencodes

Hey @Bencodes. Yes! I'm able to propagate the compiler flags by doing as you said, depending on a java_library that exports the plugin and then setting the javacopts on the java_package_configuration for the specific mixed-mode targets.

I'm running into some other issues though, unclear if they are related to rules_kotlin. But the compiler plugin gets unhappy when a java source file is accessing a symbol defined in a kt file within the same compilation unit. The plugin throws an error that the symbol can't be found. Is this something you've seen before? Otherwise I'll keep digging and you can close this. Thanks a lot for the help!

Kvarnefalk avatar Oct 17 '22 15:10 Kvarnefalk

After some more digging it seems to be happening when a java file is referencing a kotlin symbol from a file located in the same package.

Kvarnefalk avatar Oct 17 '22 18:10 Kvarnefalk

I haven't had luck with the above options, is there any new way to get arbitrary java plugin options into the KT build chain?

We have a kapt plugin (the android room compiler) that we need to pass an annotation processor argument to, but the build chain seems very tightly isolated as far as injecting new arguments.

colinrgodsey avatar Jan 23 '23 17:01 colinrgodsey

@Kvarnefalk @colinrgodsey : we're hitting the same issue, when trying to configure custom error prone rules on kotlin mixed sourceset modules. Have you found a workaround, or did you end patching kotlin rules?

oliviernotteghem avatar Mar 26 '24 04:03 oliviernotteghem