bazelbuild / rules_java

Java rules for Bazel
Apache License 2.0
72 stars 65 forks source link

Uses `--system <java11>` when source_version set to "21" #227

Open jeffalder opened 1 month ago

jeffalder commented 1 month ago

Setup: rules_java 7.3.2 Bazel 6.5.0

Our codebase today compiles fine on Java 17. There are no issues. I am trying to switch my codebase to compile on Java 21 instead of Java 17. I've found most of the conversions, but one is lingering that I cannot decipher.

    default_java_toolchain(
        # elided
        source_version = java_version, # definitely 21
        target_version = java_version,
        # elided
    )

WORKSPACE:

load("@rules_java//java:repositories.bzl", "rules_java_dependencies", "rules_java_toolchains")
rules_java_dependencies()

# Register our toolchains first
register_toolchains(
    "//toolchains:javabase21_macos_x86_64",
    "//toolchains:javabase21_macos_aarch64",
    "//toolchains:javabase21_linux_x86_64",
    "//toolchains:java21_toolchain",
)

rules_java_toolchains()

# later

http_archive(
    name = "jdk21_linux_x86_64",
    build_file = "//toolchains:jdk_linux.BUILD", # this just extracts the files
    sha256 = "51fb4d03a4429c39d397d3a03a779077159317616550e4e71624c9843083e7b9",
    strip_prefix = "jdk-21.0.4+7",
    urls = ["https://github.com/adoptium/temurin21-binaries/releases/download/jdk-21.0.4%2B7/OpenJDK21U-jdk_x64_linux_hotspot_21.0.4_7.tar.gz"],
)

The javabase21_* and java21 are just renames of the toolchains that we defined using Java 17. I verified that the names link up corretctly in other code (omitted here).

When I attempt to compile anything on Java 21, the javac task adds the --system <java11> command-line parameter, where <java11> is the downloaded Zulu JDK 11. This means that compilation fails because it can't find JRE classes that were added in Java 17 (and working fine up until now). Let's ignore macos and just focus on Linux.

How can I "encourage" this to only use a Java 21 toolchain, either the local toolchain (if appropriate) or to download/install a toolchain?

hvadehra commented 1 week ago

Toolchain configuration and selection is tricky to get right and debug. Could you please create and share a reproducer repository for this?

jeffalder commented 1 week ago

@hvadehra - I will be happy to try. Do you have any quick tips on the best way to debug and troubleshoot this? Are there any specific diagnostic commands, debug dumps, or flags I can use to investigate? I'm happy to examine the output if it might be enlightening. Reconstructing our project to the point of failure will be extremely difficult due to the amount of Bazel code in the project.

hvadehra commented 1 week ago

You can try blaze build --nobuild --toolchain_resolution_debug=.* //foo:bar and see which toolchain gets selected and why the one you were expecting don't.