rules_scala icon indicating copy to clipboard operation
rules_scala copied to clipboard

Do not provide default_javac_opts for `java_common.compile`

Open fzakaria opened this issue 9 months ago • 0 comments
trafficstars

I've noticed a very odd thing where the javacopts are duplicated if any javacopts are provided for a scala_library. This only affects if there are Java files in the target

Looking at the code, I can see that the default_javac_opts are being provided to the compile method (https://github.com/bazelbuild/rules_scala/blob/a8ae50ef8c6f9b4bf551e9d6ccf0b796dd07539d/scala/private/rule_impls.bzl#L160)

I can confirm this by looking at the params file (bazel-out/k8-fastbuild/bin/liba_java.jar-0.params) ) created for each JAR and I notice that the default javacopts are duplicated.

example

I created a scala_library

scala_library(
    name = "liba",
    srcs = [
        "LibraryA.scala",
        "LibraryB.java",
    ],
    javacopts = [
        "--release 21",
    ],
)

You can see the duplicate here.

--javacopts
-source
17
-target
17
-XDskipDuplicateBridges=true
-XDcompilePolicy=simple
--should-stop=ifError=FLOW
-g
-parameters
-Xep:ReturnValueIgnored:OFF
-Xep:IgnoredPureGetter:OFF
-Xep:EmptyTopLevelDeclaration:OFF
-Xep:LenientFormatStringValidation:OFF
-Xep:ReturnMissingNullable:OFF
-Xep:UseCorrectAssertInTests:OFF
--release
21
-source
17
-target
17
-XDskipDuplicateBridges=true
-XDcompilePolicy=simple
--should-stop=ifError=FLOW
-g
-parameters
-Xep:ReturnValueIgnored:OFF
-Xep:IgnoredPureGetter:OFF
-Xep:EmptyTopLevelDeclaration:OFF
-Xep:LenientFormatStringValidation:OFF
-Xep:ReturnMissingNullable:OFF
-Xep:UseCorrectAssertInTests:OFF

If I remove the javacopts from the scala_library we get (notice no duplicate)

scala_library(
    name = "liba",
    srcs = [
        "LibraryA.scala",
        "LibraryB.java",
    ],
    javacopts = [
    ],
)
--javacopts
-source
17
-target
17
-XDskipDuplicateBridges=true
-XDcompilePolicy=simple
--should-stop=ifError=FLOW
-g
-parameters
-Xep:ReturnValueIgnored:OFF
-Xep:IgnoredPureGetter:OFF
-Xep:EmptyTopLevelDeclaration:OFF
-Xep:LenientFormatStringValidation:OFF
-Xep:ReturnMissingNullable:OFF
-Xep:UseCorrectAssertInTests:OFF

How I am very stumped, is that given the code I linked, I would expect the duplicate javacopts to always be duplicated, yet if the list is empty, it's only present once.

I suspect this has something to do with depsets and strings but I'm not sure quite yet. 🤷

I've fixed it locally by removing the default inclusion. I think that's a worthwhile fix but I would sure love to understand what's causing this. java_library doesn't suffer from the same symptom but it's calling into the same code in rules_java.

Why is this all a problem? By duplicating the default javacopts, we are having trouble overwriting toolchain values.

I think there is some similarity to https://github.com/bazelbuild/rules_scala/issues/1550 but the duplication seems to be new/different.

fzakaria avatar Jan 24 '25 06:01 fzakaria