Configuring JDK toolchains is rather confusing, especially with incompatible_use_toolchain_resolution_for_java_rules and deprecation of --[host_]java_toolchain and --[host_]javabase.
We've tried run the nojdk bazel (4.2.0, 4.2.1, 5.0.0.something) with our own in-repo JDK 11.
For the sake of brevity I'll omit my attempts using the deprecated arguments.
WORKSPACE:
local_java_repository(
name = "optjdk",
java_home = "%s/opt/jdk11" % __workspace_dir__,
)
These startup args are accepted but fail when building:
.bazelrc
startup --noautodetect_server_javabase
startup --server_javabase=opt/jdk11
--java_language_version=optjdk_8
--java_runtime_version=optjdk_8
--tool_java_language_version=optjdk_11
--tool_java_runtime_version=optjdk_11
Results in:
Error in fail: Auto-Configuration Error: Cannot find Java binary bin/java in /home/breenn/.cache/bazel/_bazel_breenn/install/2c5e834aaa23a251af924b840dbbc8bc/embedded_tools/tools/jdk/nosystemjdk; either correct your JAVA_HOME, PATH or specify Java from remote repository (e.g. --java_runtime_version=remotejdk_11Which is confusing as
--java_runtime_version is indeed specified!
Issue: when removing the 'startup' word from the arguments, they are still accepted! If I'm specfying
--[noautodetect_]server_javabase incorrectly I was expecting an error consistent with that from mis-specifying other arguments (e.g.
--host_jvm_args vs
startup --host_jvm_args)
.bazelrc
--noautodetect_server_javabase
--server_javabase=opt/jdk11
--java_language_version=8
--java_runtime_version=8
--tool_java_language_version=11
--tool_java_runtime_version=11
This confguration appears to work, though when run with --toolchain_resolution_debug '.*jdk.*'
, it's a little confusing seeing other JDK's "selected":
INFO: ToolchainResolution: Type @bazel_tools//tools/jdk:toolchain_type: target platform @local_config_platform//:host: execution @local_config_platform//:host: Selected toolchain @optjdk//:optjdk_toolchain_java8
INFO: ToolchainResolution: Type @bazel_tools//tools/jdk:toolchain_type: target platform @local_config_platform//:host: execution @local_config_platform//:host: Selected toolchain @optjdk//:optjdk_toolchain_java8
INFO: ToolchainResolution: Target platform @local_config_platform//:host: Selected execution platform @local_config_platform//:host, type @bazel_tools//tools/jdk:toolchain_type -> toolchain @optjdk//:optjdk_toolchain_java8
INFO: ToolchainResolution: Type @bazel_tools//tools/jdk:runtime_toolchain_type: target platform @local_config_platform//:host: execution @local_config_platform//:host: Selected toolchain @remotejdk11_linux//:jdk
INFO: ToolchainResolution: Type @bazel_tools//tools/jdk:toolchain_type: target platform @local_config_platform//:host: execution @local_config_platform//:host: Selected toolchain @optjdk//:optjdk_toolchain_java8
INFO: ToolchainResolution: Type @bazel_tools//tools/jdk:runtime_toolchain_type: target platform @local_config_platform//:host: execution @local_config_platform//:host: Selected toolchain @local_jdk//:jdk
INFO: ToolchainResolution: Target platform @local_config_platform//:host: Selected execution platform @local_config_platform//:host, type @bazel_tools//tools/jdk:runtime_toolchain_type -> toolchain @local_jdk//:jdk
INFO: ToolchainResolution: Target platform @local_config_platform//:host: Selected execution platform @local_config_platform//:host, type @bazel_tools//tools/jdk:runtime_toolchain_type -> toolchain @remotejdk11_linux//:jdk
INFO: ToolchainResolution: Target platform @local_config_platform//:host: Selected execution platform @local_config_platform//:host, type @bazel_tools//tools/jdk:toolchain_type -> toolchain @optjdk//:optjdk_toolchain_java8
INFO: ToolchainResolution: Target platform @local_config_platform//:host: Selected execution platform @local_config_platform//:host, type @bazel_tools//tools/jdk:toolchain_type -> toolchain @optjdk//:optjdk_toolchain_java8
INFO: ToolchainResolution: Type @bazel_tools//tools/cpp:toolchain_type: target platform @local_config_platform//:host: execution @local_config_platform//:host: Selected toolchain @local_config_cc//:cc-compiler-k8
INFO: ToolchainResolution: Type @bazel_tools//tools/jdk:runtime_toolchain_type: target platform @local_config_platform//:host: execution @local_config_platform//:host: Selected toolchain @remotejdk11_linux//:jdk
INFO: ToolchainResolution: Type @bazel_tools//tools/jdk:toolchain_type: target platform @local_config_platform//:host: execution @local_config_platform//:host: Selected toolchain @optjdk//:optjdk_toolchain_java8
INFO: ToolchainResolution: Target platform @local_config_platform//:host: Selected execution platform @local_config_platform//:host, type @bazel_tools//tools/jdk:runtime_toolchain_type -> toolchain @remotejdk11_linux//:jdk
INFO: ToolchainResolution: Target platform @local_config_platform//:host: Selected execution platform @local_config_platform//:host, type @bazel_tools//tools/jdk:toolchain_type -> toolchain @optjdk//:optjdk_toolchain_java8
I can see @local_jdk//:jdk AND @remotejdk11_linux//:jdk unexpectedly selected for runtime_toolchain_type (was expecting @optjdk//:optjdk_toolchain_java11) and @optjdk//:optjdk_toolchain_java8 selected as toolchain_type as expected
I can see that @local_jdk is ultimately symlinked to the JDK specified by --server_javabase, but am not sure how to confirm what @bazel_tools//tools/jdk:* is.
I was reasonably sure that the remotejdk selection was garbage as
--experimental_repository_disable_download is set until running in our air-gapped CI environment it failed to download zulu11.
Issue: how do I configure bazel to use my in-repo JDK and only my in-repo JDK?
Follow Up: how does an in-repo JDK work with remote execution?
Thanks,
Nick Breen