Skip to content

--javabase is not used for linking #5888

Closed

Description

@buchgr

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

  1. added
    P0
    on Aug 14, 2018
  2. added a commit that references this issue on Aug 14, 2018
  3. changed the title [-]--javabase is not used for compilation[/-] [+]--javabase is not used for linking[/+] on Aug 14, 2018
  4. iirina commented on Aug 14, 2018

    @iirina
    Contributor

    Is this a new bug? Can it be reproduced with previous Bazel releases?

  5. iirina commented on Aug 14, 2018

    @iirina
    Contributor

    The --javabase documentation says that the flag sets the base Java installation for bazel run, bazel test and for Java binaries built by java_binary/java_test. So I think the behavior here is correct.

    AFAIU one should set --java_toolchain to change how the Java source files are compiled.

  6. davido commented on Aug 14, 2018

    @davido
    Contributor

    This this how we instruct Gerrit Code Review users to (optionaly) build with JDK9 [1]:

    $ bazel build \
      --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_java9 \
      --java_toolchain=@bazel_tools//tools/jdk:toolchain_java9 \
      :release
    

    https://gerrit.googlesource.com/gerrit/+/master/Documentation/dev-bazel.txt#25

  7. iirina commented on Aug 14, 2018

    @iirina
    Contributor

    @davido I don't think the --host_java_toolchain is 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?

  8. iirina commented on Aug 14, 2018

    @iirina
    Contributor

    @buchgr This doesn't look like a bug or a release blocker to me.

  9. cushon commented on Aug 14, 2018

    @cushon
    Contributor

    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_jdk that worked with the default configuration was JDK 8.

  10. cushon commented on Aug 14, 2018

    @cushon
    Contributor

    I think we should consider it a bug that the default toolchain uses -source 8 -target 8 and 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 -bootclasspath flag and use --release instead: http://openjdk.java.net/jeps/247. That mostly works today aside from a couple of places the Android rules rely on customizing the bootclasspath.

  11. meisterT commented on Aug 14, 2018

    @meisterT
    Member

    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.

  12. iirina commented on Aug 14, 2018

    @iirina
    Contributor

    I think we should consider it a bug that the default toolchain uses -source 8 -target 8 and the bootclasspath from the host JDK (which may not be JDK 8). The versions of source/target/bootclasspath should match.

    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.

  13. iirina commented on Aug 14, 2018

    @iirina
    Contributor

    @cushon is #5889 enough to fix this issue?

  14. cushon commented on Aug 14, 2018

    @cushon
    Contributor

    @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.

  15. cushon commented on Aug 15, 2018

    @cushon
    Contributor

    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.

  16. davido commented on Aug 15, 2018

    @davido
    Contributor

    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.

    Trying to build Gerrit on master (b0e82956b1e8b4b04d187c26ec6e055beeaedb82) on JDK9 with this PR is failing on both Java8 und Java9 with the same error:

    $ bazel build --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_java9 --java_toolchain=@bazel_tools//tools/jdk:toolchain_java9 :release
    Starting local Bazel server and connecting to it...
    DEBUG: /home/davido/.cache/bazel/_bazel_davido/27a001f4182820ef315d8d2d4f1edafe/external/bazel_skylib/lib/versions.bzl:98:7: 
    Current Bazel is not a release version, cannot check for compatibility.
    DEBUG: /home/davido/.cache/bazel/_bazel_davido/27a001f4182820ef315d8d2d4f1edafe/external/bazel_skylib/lib/versions.bzl:99:7: Make sure that you are running at least Bazel 0.14.0.
    INFO: Analysed target //:release (382 packages loaded).
    INFO: Found 1 target...
    ERROR: /home/davido/projects/gerrit/java/org/eclipse/jgit/BUILD:19:1: Building java/org/eclipse/jgit/libEdit.jar (1 source jar) failed (Exit 1)
    Fatal Error: Unable to find package java.lang in classpath or bootclasspath
    Target //:release failed to build
    Use --verbose_failures to see the command lines of failed build steps.
    INFO: Elapsed time: 17.248s, Critical Path: 2.92s
    INFO: 136 processes: 23 remote cache hit, 111 linux-sandbox, 2 worker.
    FAILED: Build did NOT complete successfully
    
  17. davido commented on Aug 15, 2018

    @davido
    Contributor

    It turns out that building Gerrit on JDK9 is failing on Bazel master without #5894 as well. I filed: #5896.

  18. added a commit that references this issue on Aug 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P0breakage

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions