Repository navigation
Description
We found that Bazel does not use the java_runtime specified via --javabase for "linking", but instead seems to use the --host_javabase. Below is a simple reproducer.
$ bazel version
Build label: 0.16.1
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Mon Aug 13 13:43:36 2018 (1534167816)
Build timestamp: 1534167816
Build timestamp as int: 1534167816
$ cat BUILD
java_binary(
name = "main",
srcs = ["Main.java"],
main_class = "app.Main",
)
java_runtime(
name="jdk8",
java_home="<PATH TO JDK8 INSTALL>",
visibility = ["//visibility:public"],
)
$ cat Main.java
package app;
import java.nio.ByteBuffer;
public class Main {
public static void main(String... args) {
byte[] bytes = new byte[10];
ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.limit(5);
}
}
$ bazel run :main --javabase=:jdk8
INFO: Analysed target //:main (0 packages loaded).
INFO: Found 1 target...
Target //:main up-to-date:
bazel-bin/main.jar
bazel-bin/main
INFO: Elapsed time: 0.178s, Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Exception in thread "main" java.lang.NoSuchMethodError: java.nio.ByteBuffer.limit(I)Ljava/nio/ByteBuffer;
at app.Main.main(Main.java:10)
Bazel generates bytecode based on the --host_javabase which is JDK9 and then tries to run it under JDK8. Below is the byte code that Bazel generates for invoking ByteBuffer#limit(int). Note that ByteBuffer limit(int) only exists since JDK9 https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#limit-int-. In JDK8 the method is inherited from java.nio.Buffer and had the signature Buffer limit(int) https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html
invokevirtual #3 // Method java/nio/ByteBuffer.limit:(I)Ljava/nio/ByteBuffer;
Activity
Disable failing tests until bazelbuild#5888 is fixed.
[-]--javabase is not used for compilation[/-][+]--javabase is not used for linking[/+]iirina commentedon Aug 14, 2018
Is this a new bug? Can it be reproduced with previous Bazel releases?
iirina commentedon Aug 14, 2018
The
--javabasedocumentation says that the flag sets the base Java installation forbazel run,bazel testand for Java binaries built byjava_binary/java_test. So I think the behavior here is correct.AFAIU one should set
--java_toolchainto change how the Java source files are compiled.davido commentedon Aug 14, 2018
This this how we instruct Gerrit Code Review users to (optionaly) build with JDK9 [1]:
https://gerrit.googlesource.com/gerrit/+/master/Documentation/dev-bazel.txt#25
iirina commentedon Aug 14, 2018
@davido I don't think the
--host_java_toolchainis necessary, since it defaults to the value of--java_toolchain, unless there's a bug. Do you get any errors when you don't set the host toolchain?iirina commentedon Aug 14, 2018
@buchgr This doesn't look like a bug or a release blocker to me.
cushon commentedon Aug 14, 2018
The behaviour here changed related due to #5594 and #5744. Previously the compile-time bootclasspath was coming from the
@local_jdk, and the only@local_jdkthat worked with the default configuration was JDK 8.cushon commentedon Aug 14, 2018
I think we should consider it a bug that the default toolchain uses
-source 8 -target 8and the bootclasspath from the host JDK (which may not be JDK 8). The versions of source/target/bootclasspath should match.I'm not sure about the severity, though.
The medium-term solution is to stop using the javac
-bootclasspathflag and use--releaseinstead: http://openjdk.java.net/jeps/247. That mostly works today aside from a couple of places the Android rules rely on customizing the bootclasspath.meisterT commentedon Aug 14, 2018
I consider this a serious bug that we should fix ASAP.
If you look at #5886 then you'll see that we had to do awkward code changes. For the android ziputils test we would have to do > 100 such awkward changes as in fec53fb, that's why we rather disabled the test.
This behavior is a regression and quite confusing for users who will also run into the same issues that we ran into on our CI.
iirina commentedon Aug 14, 2018
It looks like we can create a bootclasspath for the target JDK and use it for the default toolchain. Would that be correct? Although that also doesn't guarantee that the target JDK is 8.
iirina commentedon Aug 14, 2018
@cushon is #5889 enough to fix this issue?
https://github.com/bazelbuild/bazel/issues/5888
cushon commentedon Aug 14, 2018
@iirina I don't think so: java_toolchain.bootclasspath is resolved in the host config, and genrule.tools is also resolved in the host config.
Here's a hack that does work around this issue: cushon@ad1be7e. But that isn't a principled fix: executing the JDK from genrule.srcs only works if the host and target arch are the same.
Use a JDK 8 bootclasspath by default
Use a JDK 8 bootclasspath by default
cushon commentedon Aug 15, 2018
I mailed a fix that causes the default toolchain to always use JDK 8 APIs together with the Java 8 language level, instead of defaulting to the host JDK's bootclasspath: #5894.
It addresses this issue and shouldn't regress any of the other recent improvements in this area.
davido commentedon Aug 15, 2018
Trying to build Gerrit on master (b0e82956b1e8b4b04d187c26ec6e055beeaedb82) on JDK9 with this PR is failing on both Java8 und Java9 with the same error:
davido commentedon Aug 15, 2018
It turns out that building Gerrit on JDK9 is failing on Bazel master without #5894 as well. I filed: #5896.
Use a JDK 8 bootclasspath by default
Use a JDK 8 bootclasspath by default
Use a JDK 8 bootclasspath by default
Use a JDK 8 bootclasspath by default