rules_java not respecting --tool_java_language_version/--tool_java_runtime_version for test_jar's as of at least 7.1.0
When upgrading to Bazel 7.0.0, which upgraded my version of rules_java to 7.1.0, I have found that when building a test jar it has upgraded to using Java 21 despite passing the following in my .bazelrc file:
build --java_language_version=11
build --java_runtime_version=remotejdk_11
build --tool_java_language_version=11
build --tool_java_runtime_version=remotejdk_11
When building I can see that Java 21 is being used to compile the test jar in the error output with the path to java being external/remotejdk21_macos_aarch64/bin/java. I would expect this path to be a Java 11 JDK with the above options in the .bazelrc.
I have also tried upgrading to rules_java 7.3.2 and that also fails with the same issue.
Attached is a minimum sample project to recreate the issue.
If you attempt to build the project you should get an error somewhere along the lines of:
ERROR: /home/cjohnstoniv/helm-farm/sample-java/BUILD:6:10: Building test-unit.jar (1 source file) failed: (Exit 1): java failed: error executing Javac command (from target //:test-unit) external/rules_java~7.1.0~toolchains~remotejdk21_linux/bin/java '--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED' '--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED' ... (remaining 19 arguments skipped)
src/test/java/hello/TestTest.java:10: error: ';' expected
Fail me
^
The key piece of the error message is the: external/rules_java~7.1.0~toolchains~remotejdk21_linux/bin/java
I would expect this to be remotejdk11 based on the .bazelrc file.
This has come up before in https://github.com/bazelbuild/rules_java/issues/148 and https://github.com/bazelbuild/bazel/issues/19934
The behavior changed in https://github.com/bazelbuild/bazel/commit/37eb1d79ecb019220073b62321f9eb88aebaa4e4 during platformization of the java rules. From what I've gathered, the motivation for fixing the java_runtime version was to avoid issues in the default case, i.e. when a local jdk is in use. cc @comius to correct me / add more colour.
As explained in https://github.com/bazelbuild/bazel/issues/19934#issuecomment-1779674035 using a newer runtime shouldn't matter in practice, as we still correctly use the source/target language level. If you must use a specific runtime version, please declare a custom default_java_toolchain specifying the needed value for java_runtime.