troubles configuring jdks with nojdk build

320 views
Skip to first unread message

Nick Breen

unread,
Sep 23, 2021, 2:00:32 AM9/23/21
to bazel-discuss

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_11

Which 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

Tobias Werth

unread,
Sep 23, 2021, 3:29:45 AM9/23/21
to Nick Breen, Ivo List, bazel-discuss

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/deab838d-3141-4a7a-abbf-f129193bf03en%40googlegroups.com.

Nick Breen

unread,
Sep 24, 2021, 1:19:10 AM9/24/21
to bazel-discuss
I think I've sorted it out, at least for 4.2.0.

I pain-staking trawled through the Command-Line Reference (I notice that many options are duplicated and the hyperlinks are not unique) and found where I'd mis-declared various options.

With the WORKSPACE (unchanged):

local_java_repository(
  name = "optjdk",
  java_home = "%s/opt/jdk11" % __workspace_dir__,
)


... and relevant parts of .bazelrc:

startup --noautodetect_server_javabase
startup --server_javabase=opt/jdk11
build --java_language_version=8
build --tool_java_language_version=8
build --java_runtime_version=8
build --tool_java_runtime_version=8
build --java_toolchain=@bazel_tools//tools/jdk:toolchain_java8
build --javabase=@optjdk//:jdk
build --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_java8
build --host_javabase=@optjdk//:jdk

I'll be sticking with 4.2.0 at least until 5.0.0 is released.

Thanks,

Philipp Wollermann

unread,
Sep 27, 2021, 4:36:39 PM9/27/21
to bazel-discuss
Thank you for posting the solution, Nick! This is very helpful.

Cheers,
Philipp
Reply all
Reply to author
Forward
0 new messages