From 18113577468d04e5c645ab3a510003d841f31657 Mon Sep 17 00:00:00 2001 From: TrapsterDK <56176095+TrapsterDK@users.noreply.github.com> Date: Sat, 5 Dec 2026 16:27:36 +0100 Subject: [PATCH 1/3] fix: resolve a JDK only for JRuby toolchains (#460) rb_binary, rb_test, rb_gem_push and the ruby exec group of rb_gem_build, rb_gem_install and rb_bundle_install required the Java runtime toolchain type, so builds with MRI resolved a JDK too. Add an optional java_runtime attribute to rb_toolchain, set only for JRuby to current_java_runtime, and read the JDK from the Ruby toolchain. --- .github/workflows/ci.yml | 9 ++++++++ ruby/private/binary.bzl | 8 +++---- ruby/private/bundle_install.bzl | 12 ++++------ ruby/private/download.bzl | 2 ++ ruby/private/download/BUILD.tpl | 1 + ruby/private/gem_build.bzl | 17 +++++++------- ruby/private/gem_install.bzl | 12 ++++------ ruby/private/gem_push.bzl | 13 +++++++---- ruby/private/test.bzl | 1 - ruby/private/utils.bzl | 14 ++++++++++++ ruby/tests/BUILD | 3 +++ ruby/tests/java_runtime_tests.bzl | 38 +++++++++++++++++++++++++++++++ ruby/toolchain.bzl | 14 ++++++++++++ 13 files changed, 112 insertions(+), 32 deletions(-) create mode 100644 ruby/tests/java_runtime_tests.bzl diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 07cb481c..044a1140 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -69,6 +69,15 @@ jobs: with: ruby-version: 3.3.12 - run: bazel build ... + # Only JRuby needs a JDK, so targets on any other interpreter must not resolve a Java toolchain. + - if: ${{ !startsWith(matrix.ruby, 'jruby') }} + shell: bash + run: | + deps="$(bazel cquery --output=label 'deps(//...)')" + if grep -E 'remotejdk|tools/jdk:runtime_toolchain_type' <<< "$deps"; then + echo "::error::a target resolved a Java toolchain" + exit 1 + fi - run: bazel run lib/gem:add-numbers 2 - run: bazel run lib/gem:print-version - run: bazel run "@ruby" "--" -e "puts RUBY_VERSION" diff --git a/ruby/private/binary.bzl b/ruby/private/binary.bzl index ecbbf479..490b0f5d 100644 --- a/ruby/private/binary.bzl +++ b/ruby/private/binary.bzl @@ -18,6 +18,7 @@ load( _convert_env_to_script = "convert_env_to_script", _is_windows = "is_windows", _normalize_path = "normalize_path", + _require_java_runtime = "require_java_runtime", _to_rlocation_path = "to_rlocation_path", ) @@ -177,9 +178,9 @@ def rb_binary_impl(ctx): tools.append(ctx.file._runfiles_helper) if ruby_toolchain.version.startswith("jruby"): - java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"] - tools.extend(java_toolchain.java_runtime.files.to_list()) - java_bin = java_toolchain.java_runtime.java_executable_runfiles_path[3:] + java_runtime = _require_java_runtime(ruby_toolchain) + tools.extend(java_runtime.files.to_list()) + java_bin = java_runtime.java_executable_runfiles_path[3:] for dep in transitive_deps.to_list(): if BundlerInfo in dep: @@ -271,7 +272,6 @@ rb_binary = rule( ), toolchains = [ "@rules_ruby//ruby:toolchain_type", - "@bazel_tools//tools/jdk:runtime_toolchain_type", ], doc = """ Runs a Ruby binary. diff --git a/ruby/private/bundle_install.bzl b/ruby/private/bundle_install.bzl index d5212aaa..fd1263d8 100644 --- a/ruby/private/bundle_install.bzl +++ b/ruby/private/bundle_install.bzl @@ -7,6 +7,7 @@ load( _convert_env_to_script = "convert_env_to_script", _is_windows = "is_windows", _normalize_path = "normalize_path", + _require_java_runtime = "require_java_runtime", _resolve_bundle_build_env = "resolve_bundle_build_env", _to_rlocation_path = "to_rlocation_path", ) @@ -74,11 +75,11 @@ def _rb_bundle_install_impl(ctx): jars_home_strip_suffix = "" if toolchain.version.startswith("jruby"): - java_toolchain = ctx.exec_groups["ruby"].toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"] - tools.extend(java_toolchain.java_runtime.files.to_list()) + java_runtime = _require_java_runtime(toolchain) + tools.extend(java_runtime.files.to_list()) env.update({ "JARS_SKIP": "true", # Avoid installing extra dependencies during install. - "JAVA_HOME": java_toolchain.java_runtime.java_home, + "JAVA_HOME": java_runtime.java_home, }) jars_info = _jars_home(ctx, jar_files, ctx.attr.jars_path) jars_home_strip_suffix = jars_info.strip @@ -257,10 +258,7 @@ rb_bundle_install = rule( }, # JRuby runs Ruby and Java tools in the same action, on the same platform. exec_groups = { - "ruby": exec_group(toolchains = [ - "@rules_ruby//ruby:toolchain_type", - "@bazel_tools//tools/jdk:runtime_toolchain_type", - ]), + "ruby": exec_group(toolchains = ["@rules_ruby//ruby:toolchain_type"]), }, doc = """ Installs Bundler dependencies from cached gems. diff --git a/ruby/private/download.bzl b/ruby/private/download.bzl index 416dd943..8d4b4eaa 100644 --- a/ruby/private/download.bzl +++ b/ruby/private/download.bzl @@ -232,6 +232,8 @@ def _rb_download_impl(repository_ctx): "{env}": repr(env), "{bundle_build_env}": repr(bundle_build_env), "{includes}": repr(includes), + # Only JRuby needs a JDK. Leaving it unset elsewhere keeps MRI builds from resolving one. + "{java_runtime}": repr("@bazel_tools//tools/jdk:current_java_runtime" if engine == "jruby" else None), }, ) diff --git a/ruby/private/download/BUILD.tpl b/ruby/private/download/BUILD.tpl index 145bc14d..b2a62a6d 100644 --- a/ruby/private/download/BUILD.tpl +++ b/ruby/private/download/BUILD.tpl @@ -92,6 +92,7 @@ rb_toolchain( "@platforms//os:windows": "dist/bin/{gem_binary_name}.cmd", "//conditions:default": "dist/bin/{gem_binary_name}", }), + java_runtime = {java_runtime}, ruby = ":ruby_file", version = "{version}", ) diff --git a/ruby/private/gem_build.bzl b/ruby/private/gem_build.bzl index f72e5893..963768b2 100644 --- a/ruby/private/gem_build.bzl +++ b/ruby/private/gem_build.bzl @@ -10,14 +10,17 @@ load( "get_transitive_deps", "get_transitive_srcs", ) -load("//ruby/private:utils.bzl", _is_windows = "is_windows") +load( + "//ruby/private:utils.bzl", + _is_windows = "is_windows", + _require_java_runtime = "require_java_runtime", +) def _rb_gem_build_impl(ctx): gem_builder = ctx.actions.declare_file("{}_gem_builder.rb".format(ctx.label.name)) transitive_data = get_transitive_data(ctx.files.data, ctx.attr.deps).to_list() transitive_deps = get_transitive_deps(ctx.attr.deps).to_list() transitive_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps).to_list() - java_toolchain = ctx.exec_groups["ruby"].toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"] ruby_toolchain = ctx.exec_groups["ruby"].toolchains["@rules_ruby//ruby:toolchain_type"] if ctx.attr.ruby != None: ruby_toolchain = ctx.attr.ruby[platform_common.ToolchainInfo] @@ -28,8 +31,9 @@ def _rb_gem_build_impl(ctx): env.update(ruby_toolchain.env) if ruby_toolchain.version.startswith("jruby"): - env["JAVA_HOME"] = java_toolchain.java_runtime.java_home - tools.extend(java_toolchain.java_runtime.files.to_list()) + java_runtime = _require_java_runtime(ruby_toolchain) + env["JAVA_HOME"] = java_runtime.java_home + tools.extend(java_runtime.files.to_list()) if _is_windows(ctx): env["PATH"] = ruby_toolchain.ruby.dirname @@ -117,10 +121,7 @@ rb_gem_build = rule( }, # JRuby runs Ruby and Java tools in the same action, on the same platform. exec_groups = { - "ruby": exec_group(toolchains = [ - "@rules_ruby//ruby:toolchain_type", - "@bazel_tools//tools/jdk:runtime_toolchain_type", - ]), + "ruby": exec_group(toolchains = ["@rules_ruby//ruby:toolchain_type"]), }, doc = """ Builds a Ruby gem. diff --git a/ruby/private/gem_install.bzl b/ruby/private/gem_install.bzl index 17314366..26879ca9 100644 --- a/ruby/private/gem_install.bzl +++ b/ruby/private/gem_install.bzl @@ -6,6 +6,7 @@ load( _convert_env_to_script = "convert_env_to_script", _is_windows = "is_windows", _normalize_path = "normalize_path", + _require_java_runtime = "require_java_runtime", _resolve_bundle_build_env = "resolve_bundle_build_env", ) @@ -25,9 +26,9 @@ def _rb_gem_install_impl(ctx): tools.extend(toolchain.files) if toolchain.version.startswith("jruby"): - java_toolchain = ctx.exec_groups["ruby"].toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"] - tools.extend(java_toolchain.java_runtime.files.to_list()) - env.update({"JAVA_HOME": java_toolchain.java_runtime.java_home}) + java_runtime = _require_java_runtime(toolchain) + tools.extend(java_runtime.files.to_list()) + env.update({"JAVA_HOME": java_runtime.java_home}) if _is_windows(ctx): gem_install = ctx.actions.declare_file("gem_install_{}.cmd".format(ctx.label.name)) @@ -95,10 +96,7 @@ rb_gem_install = rule( }, # JRuby runs Ruby and Java tools in the same action, on the same platform. exec_groups = { - "ruby": exec_group(toolchains = [ - "@rules_ruby//ruby:toolchain_type", - "@bazel_tools//tools/jdk:runtime_toolchain_type", - ]), + "ruby": exec_group(toolchains = ["@rules_ruby//ruby:toolchain_type"]), }, doc = """ Installs a built Ruby gem. diff --git a/ruby/private/gem_push.bzl b/ruby/private/gem_push.bzl index e4a33374..b274139d 100644 --- a/ruby/private/gem_push.bzl +++ b/ruby/private/gem_push.bzl @@ -2,11 +2,14 @@ load("//ruby/private:binary.bzl", "generate_rb_binary_script", BINARY_ATTRS = "ATTRS") load("//ruby/private:library.bzl", LIBRARY_ATTRS = "ATTRS") -load("//ruby/private:utils.bzl", _to_rlocation_path = "to_rlocation_path") +load( + "//ruby/private:utils.bzl", + _require_java_runtime = "require_java_runtime", + _to_rlocation_path = "to_rlocation_path", +) def _rb_gem_push_impl(ctx): env = {} - java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"] ruby_toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"] if ctx.attr.ruby != None: ruby_toolchain = ctx.attr.ruby[platform_common.ToolchainInfo] @@ -16,8 +19,9 @@ def _rb_gem_push_impl(ctx): java_bin = "" if ruby_toolchain.version.startswith("jruby"): - tools.extend(java_toolchain.java_runtime.files.to_list()) - java_bin = java_toolchain.java_runtime.java_executable_runfiles_path[3:] + java_runtime = _require_java_runtime(ruby_toolchain) + tools.extend(java_runtime.files.to_list()) + java_bin = java_runtime.java_executable_runfiles_path[3:] env.update(ruby_toolchain.env) env.update(ctx.attr.env) @@ -69,7 +73,6 @@ Gem file to push to RubyGems. You would usually use an output of `rb_gem_build() ), toolchains = [ "@rules_ruby//ruby:toolchain_type", - "@bazel_tools//tools/jdk:runtime_toolchain_type", ], doc = """ Pushes a built Ruby gem. diff --git a/ruby/private/test.bzl b/ruby/private/test.bzl index efc590da..b3e1378b 100644 --- a/ruby/private/test.bzl +++ b/ruby/private/test.bzl @@ -16,7 +16,6 @@ rb_test = rule( ), toolchains = [ "@rules_ruby//ruby:toolchain_type", - "@bazel_tools//tools/jdk:runtime_toolchain_type", ], doc = """ Runs a Ruby test. diff --git a/ruby/private/utils.bzl b/ruby/private/utils.bzl index 0d03ad08..1fe1ef3c 100644 --- a/ruby/private/utils.bzl +++ b/ruby/private/utils.bzl @@ -232,3 +232,17 @@ def to_rlocation_path(ctx, source): return workspace_name + "/" + source.short_path return source.short_path + +def require_java_runtime(toolchain): + """Returns the JavaRuntimeInfo of a JRuby toolchain, failing if there is none. + + Args: + toolchain: the Ruby `ToolchainInfo` + + Returns: + the `JavaRuntimeInfo` + """ + runtime = toolchain.java_runtime + if runtime == None: + fail("The JRuby toolchain {} has no `java_runtime`; set `java_runtime` on its rb_toolchain().".format(toolchain.version)) + return runtime diff --git a/ruby/tests/BUILD b/ruby/tests/BUILD index cf11ee92..5105b986 100644 --- a/ruby/tests/BUILD +++ b/ruby/tests/BUILD @@ -1,5 +1,6 @@ load(":download_tests.bzl", "download_test_suite") load(":jars_downloader_tests.bzl", "jars_downloader_test_suite") +load(":java_runtime_tests.bzl", "java_runtime_test_suite") load(":repository_proxy_tests.bzl", "repository_proxy_test_suite") load(":toolchain_selection_tests.bzl", "toolchain_selection_test_suite") load(":utils_tests.bzl", "utils_test_suite") @@ -13,3 +14,5 @@ utils_test_suite() toolchain_selection_test_suite() download_test_suite() + +java_runtime_test_suite(name = "java_runtime_tests") diff --git a/ruby/tests/java_runtime_tests.bzl b/ruby/tests/java_runtime_tests.bzl new file mode 100644 index 00000000..d6e929cb --- /dev/null +++ b/ruby/tests/java_runtime_tests.bzl @@ -0,0 +1,38 @@ +"""Analysis tests for the Java runtime of Ruby toolchains.""" + +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") +load("//ruby:toolchain.bzl", "rb_toolchain") +load("//ruby/private:utils.bzl", "require_java_runtime") + +def _subject_impl(ctx): + require_java_runtime(ctx.attr.toolchain[platform_common.ToolchainInfo]) + return [] + +_subject = rule( + implementation = _subject_impl, + attrs = {"toolchain": attr.label(providers = [platform_common.ToolchainInfo])}, +) + +def _missing_java_runtime_test_impl(ctx): + env = analysistest.begin(ctx) + asserts.expect_failure(env, "has no `java_runtime`; set `java_runtime` on its rb_toolchain()") + return analysistest.end(env) + +_missing_java_runtime_test = analysistest.make(_missing_java_runtime_test_impl, expect_failure = True) + +def java_runtime_test_suite(name): + rb_toolchain( + name = name + "_toolchain", + ruby = "java_runtime_tests.bzl", + version = "jruby-10.1.2.0", + tags = ["manual"], + ) + _subject( + name = name + "_subject", + toolchain = ":" + name + "_toolchain", + tags = ["manual"], + ) + _missing_java_runtime_test( + name = name + "_missing_test", + target_under_test = ":" + name + "_subject", + ) diff --git a/ruby/toolchain.bzl b/ruby/toolchain.bzl index a9d21fac..d797a2b1 100644 --- a/ruby/toolchain.bzl +++ b/ruby/toolchain.bzl @@ -1,5 +1,10 @@ "Define a Bazel toolchain for a Ruby interpreter" +load("@rules_java//java:defs.bzl", "java_common") + +def _runtime(target): + return target[java_common.JavaRuntimeInfo] if target else None + def _rb_toolchain_impl(ctx): return platform_common.ToolchainInfo( ruby = ctx.executable.ruby, @@ -9,6 +14,7 @@ def _rb_toolchain_impl(ctx): env = ctx.attr.env, bundle_build_env = ctx.attr.bundle_build_env, files = ctx.files.files, + java_runtime = _runtime(ctx.attr.java_runtime), ) rb_toolchain = rule( @@ -51,5 +57,13 @@ runs, so no absolute path is stored in the toolchain. allow_files = True, doc = "All files necessary for toolchain", ), + "java_runtime": attr.label( + doc = """ +JDK that JRuby uses, for example `@bazel_tools//tools/jdk:current_java_runtime`. +Required for JRuby toolchains. Leave it unset for interpreters that do not need Java, +so that builds using them do not resolve a Java toolchain. +""", + providers = [java_common.JavaRuntimeInfo], + ), }, ) From 19bc3110f2f20873b1bc138d59aee4c4511b3195 Mon Sep 17 00:00:00 2001 From: TrapsterDK <56176095+TrapsterDK@users.noreply.github.com> Date: Sat, 5 Dec 2026 16:27:36 +0100 Subject: [PATCH 2/3] Drop the JDK check from CI --- .github/workflows/ci.yml | 9 --------- 1 file changed, 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 044a1140..07cb481c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -69,15 +69,6 @@ jobs: with: ruby-version: 3.3.12 - run: bazel build ... - # Only JRuby needs a JDK, so targets on any other interpreter must not resolve a Java toolchain. - - if: ${{ !startsWith(matrix.ruby, 'jruby') }} - shell: bash - run: | - deps="$(bazel cquery --output=label 'deps(//...)')" - if grep -E 'remotejdk|tools/jdk:runtime_toolchain_type' <<< "$deps"; then - echo "::error::a target resolved a Java toolchain" - exit 1 - fi - run: bazel run lib/gem:add-numbers 2 - run: bazel run lib/gem:print-version - run: bazel run "@ruby" "--" -e "puts RUBY_VERSION" From 64f0aff28cdc430d096367b7be3cb7c91fec21a9 Mon Sep 17 00:00:00 2001 From: TrapsterDK <56176095+TrapsterDK@users.noreply.github.com> Date: Sat, 5 Dec 2026 20:11:32 +0100 Subject: [PATCH 3/3] Trim comments in the JRuby JDK change --- ruby/private/download.bzl | 1 - ruby/private/utils.bzl | 2 +- ruby/toolchain.bzl | 6 +----- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/ruby/private/download.bzl b/ruby/private/download.bzl index 8d4b4eaa..d6f0f9d3 100644 --- a/ruby/private/download.bzl +++ b/ruby/private/download.bzl @@ -232,7 +232,6 @@ def _rb_download_impl(repository_ctx): "{env}": repr(env), "{bundle_build_env}": repr(bundle_build_env), "{includes}": repr(includes), - # Only JRuby needs a JDK. Leaving it unset elsewhere keeps MRI builds from resolving one. "{java_runtime}": repr("@bazel_tools//tools/jdk:current_java_runtime" if engine == "jruby" else None), }, ) diff --git a/ruby/private/utils.bzl b/ruby/private/utils.bzl index 1fe1ef3c..8c40cfee 100644 --- a/ruby/private/utils.bzl +++ b/ruby/private/utils.bzl @@ -234,7 +234,7 @@ def to_rlocation_path(ctx, source): return source.short_path def require_java_runtime(toolchain): - """Returns the JavaRuntimeInfo of a JRuby toolchain, failing if there is none. + """Returns the JavaRuntimeInfo of a JRuby toolchain or fails. Args: toolchain: the Ruby `ToolchainInfo` diff --git a/ruby/toolchain.bzl b/ruby/toolchain.bzl index d797a2b1..1dcc995d 100644 --- a/ruby/toolchain.bzl +++ b/ruby/toolchain.bzl @@ -58,11 +58,7 @@ runs, so no absolute path is stored in the toolchain. doc = "All files necessary for toolchain", ), "java_runtime": attr.label( - doc = """ -JDK that JRuby uses, for example `@bazel_tools//tools/jdk:current_java_runtime`. -Required for JRuby toolchains. Leave it unset for interpreters that do not need Java, -so that builds using them do not resolve a Java toolchain. -""", + doc = "JDK for JRuby, e.g. `@bazel_tools//tools/jdk:current_java_runtime`. Required for JRuby only.", providers = [java_common.JavaRuntimeInfo], ), },