diff --git a/docs/repository_rules.md b/docs/repository_rules.md index b6cdde41..ec9c2e00 100644 --- a/docs/repository_rules.md +++ b/docs/repository_rules.md @@ -74,7 +74,7 @@ $ bazel run @ruby//:gem -- install rails
 rb_bundle_fetch(name, srcs, bundler_checksums, bundler_remote, env, gem_checksums, gemfile,
-                gemfile_lock, repo_mapping)
+                gemfile_lock, repo_mapping, ruby)
 
Fetches Bundler dependencies to be automatically installed by other targets. @@ -143,6 +143,7 @@ rb_test( | gemfile | Gemfile to install dependencies from. | Label | required | | | gemfile_lock | Gemfile.lock to install dependencies from. | Label | required | | | repo_mapping | In `WORKSPACE` context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.

For example, an entry `"@foo": "@bar"` declares that, for any time this repository depends on `@foo` (such as a dependency on `@foo//some:target`, it should actually resolve that dependency within globally-declared `@bar` (`@bar//some:target`).

This attribute is _not_ supported in `MODULE.bazel` context (when invoking a repository rule inside a module extension's implementation function). | Dictionary: String -> String | optional | | +| ruby | Override Ruby toolchain to use for installation. | Label | optional | `None` | diff --git a/docs/rules.md b/docs/rules.md index 12162809..0af4d91c 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -7,7 +7,7 @@ Public API for rules ## rb_binary
-rb_binary(name, deps, srcs, data, env, env_inherit, main)
+rb_binary(name, deps, srcs, data, env, env_inherit, main, ruby)
 
Runs a Ruby binary. @@ -134,6 +134,7 @@ rake, version 13.1.0 | env | Environment variables to use during execution.

Supports `$(location)` expansion for targets from `srcs`, `data` and `deps`. | Dictionary: String -> String | optional | `{}` | | env_inherit | List of environment variable names to be inherited by the test runner. | List of strings | optional | `[]` | | main | Ruby script to run. It may also be a binary stub generated by Bundler. If omitted, it defaults to the Ruby interpreter.

Use a built-in `args` attribute to pass extra arguments to the script. | Label | optional | `None` | +| ruby | Override Ruby toolchain to use when running the script. | Label | optional | `None` | @@ -141,7 +142,7 @@ rake, version 13.1.0 ## rb_bundle_install
-rb_bundle_install(name, srcs, env, gemfile, gemfile_lock, gems)
+rb_bundle_install(name, srcs, env, gemfile, gemfile_lock, gems, ruby)
 
Installs Bundler dependencies from cached gems. @@ -160,6 +161,7 @@ used by `rb_bundle_fetch()`. | gemfile | Gemfile to install dependencies from. | Label | required | | | gemfile_lock | Gemfile.lock to install dependencies from. | Label | required | | | gems | List of gems in vendor/cache that are used to install dependencies from. | List of labels | required | | +| ruby | Override Ruby toolchain to use when installing the gem. | Label | optional | `None` | @@ -286,7 +288,7 @@ $ bazel build :gem-build ## rb_gem_install
-rb_gem_install(name, gem)
+rb_gem_install(name, gem, ruby)
 
Installs a built Ruby gem. @@ -345,6 +347,7 @@ Successfully installed example-0.1.0 | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | | gem | Gem file to install. | Label | required | | +| ruby | Override Ruby toolchain to use when installing the gem. | Label | optional | `None` | @@ -352,7 +355,7 @@ Successfully installed example-0.1.0 ## rb_gem_push
-rb_gem_push(name, deps, srcs, data, bundle_env, env, env_inherit, gem)
+rb_gem_push(name, deps, srcs, data, bundle_env, env, env_inherit, gem, ruby)
 
Pushes a built Ruby gem. @@ -416,6 +419,7 @@ Successfully registered gem: example (0.1.0) | env | Environment variables to use during execution.

Supports `$(location)` expansion for targets from `srcs`, `data` and `deps`. | Dictionary: String -> String | optional | `{}` | | env_inherit | List of environment variable names to be inherited by the test runner. | List of strings | optional | `[]` | | gem | Gem file to push to RubyGems. You would usually use an output of `rb_gem_build()` target here. | Label | required | | +| ruby | Override Ruby toolchain to use when running the script. | Label | optional | `None` | @@ -519,7 +523,7 @@ using other rules. ## rb_test
-rb_test(name, deps, srcs, data, env, env_inherit, main)
+rb_test(name, deps, srcs, data, env, env_inherit, main, ruby)
 
Runs a Ruby test. @@ -653,5 +657,6 @@ root = File.expand_path(__dir__) | env | Environment variables to use during execution.

Supports `$(location)` expansion for targets from `srcs`, `data` and `deps`. | Dictionary: String -> String | optional | `{}` | | env_inherit | List of environment variable names to be inherited by the test runner. | List of strings | optional | `[]` | | main | Ruby script to run. It may also be a binary stub generated by Bundler. If omitted, it defaults to the Ruby interpreter.

Use a built-in `args` attribute to pass extra arguments to the script. | Label | optional | `None` | +| ruby | Override Ruby toolchain to use when running the script. | Label | optional | `None` | diff --git a/ruby/private/binary.bzl b/ruby/private/binary.bzl index d11a171c..7f4521f7 100644 --- a/ruby/private/binary.bzl +++ b/ruby/private/binary.bzl @@ -43,6 +43,10 @@ Supports `$(location)` expansion for targets from `srcs`, `data` and `deps`. "env_inherit": attr.string_list( doc = "List of environment variable names to be inherited by the test runner.", ), + "ruby": attr.label( + doc = "Override Ruby toolchain to use when running the script.", + providers = [platform_common.ToolchainInfo], + ), "_binary_cmd_tpl": attr.label( allow_single_file = True, default = "@rules_ruby//ruby/private/binary:binary.cmd.tpl", @@ -63,6 +67,8 @@ Supports `$(location)` expansion for targets from `srcs`, `data` and `deps`. # buildifier: disable=function-docstring def generate_rb_binary_script(ctx, binary, bundler = False, args = [], env = {}, java_bin = ""): toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"] + if ctx.attr.ruby != None: + toolchain = ctx.attr.ruby[platform_common.ToolchainInfo] binary_path = "" locate_binary_in_runfiles = "" diff --git a/ruby/private/bundle_fetch.bzl b/ruby/private/bundle_fetch.bzl index 933250c5..4987fcc3 100644 --- a/ruby/private/bundle_fetch.bzl +++ b/ruby/private/bundle_fetch.bzl @@ -23,6 +23,7 @@ _GEM_INSTALL_BUILD_FRAGMENT = """ rb_gem_install( name = "{name}", gem = "{cache_path}/{gem}", + ruby = {ruby}, ) """ @@ -31,6 +32,7 @@ rb_binary( name = "{name}", main = "{path}/{name}", deps = ["//:{repository_name}"], + ruby = {ruby}, ) """ @@ -136,6 +138,7 @@ def _rb_bundle_fetch_impl(repository_ctx): gem_fragments = [] gem_install_fragments = [] gem_checksums = {} + ruby_toolchain_attr = "None" if repository_ctx.attr.ruby == None else '"{}"'.format(repository_ctx.attr.ruby) repository_name = _normalize_bzlmod_repository_name(repository_ctx.name) # Fetch gems and expose them as `rb_gem()` targets. @@ -164,6 +167,7 @@ def _rb_bundle_fetch_impl(repository_ctx): name = gemfile_lock.bundler.full_name, gem = gemfile_lock.bundler.filename, cache_path = cache_path, + ruby = ruby_toolchain_attr, ), ) @@ -178,6 +182,7 @@ def _rb_bundle_fetch_impl(repository_ctx): name = executable, repository_name = repository_name, path = BINSTUBS_LOCATION.partition("/")[-1], + ruby = ruby_toolchain_attr, ), ) repository_ctx.template( @@ -203,6 +208,7 @@ def _rb_bundle_fetch_impl(repository_ctx): "{gem_fragments}": "".join(gem_fragments), "{gem_install_fragments}": "".join(gem_install_fragments), "{env}": repr(repository_ctx.attr.env), + "{ruby}": ruby_toolchain_attr, }, ) @@ -250,6 +256,10 @@ rb_bundle_fetch = repository_rule( default = {}, doc = "SHA-256 checksums for remote gems. Keys are gem names (e.g. foobar-1.2.3), values are SHA-256 checksums.", ), + "ruby": attr.label( + doc = "Override Ruby toolchain to use for installation.", + providers = [platform_common.ToolchainInfo], + ), "_build_tpl": attr.label( allow_single_file = True, default = "@rules_ruby//:ruby/private/bundle_fetch/BUILD.tpl", diff --git a/ruby/private/bundle_fetch/BUILD.tpl b/ruby/private/bundle_fetch/BUILD.tpl index 9ff35853..c372f749 100644 --- a/ruby/private/bundle_fetch/BUILD.tpl +++ b/ruby/private/bundle_fetch/BUILD.tpl @@ -11,6 +11,7 @@ rb_bundle_install( gemfile = "{gemfile_path}", gemfile_lock = "{gemfile_lock_path}", gems = {gems}, + ruby = {ruby}, ) {gem_fragments} diff --git a/ruby/private/bundle_install.bzl b/ruby/private/bundle_install.bzl index 39a29947..82ab0445 100644 --- a/ruby/private/bundle_install.bzl +++ b/ruby/private/bundle_install.bzl @@ -11,6 +11,8 @@ load( def _rb_bundle_install_impl(ctx): toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"] + if ctx.attr.ruby != None: + toolchain = ctx.attr.ruby[platform_common.ToolchainInfo] tools = [] tools.extend(toolchain.files) @@ -137,6 +139,10 @@ rb_bundle_install = rule( "env": attr.string_dict( doc = "Environment variables to use during installation.", ), + "ruby": attr.label( + doc = "Override Ruby toolchain to use when installing the gem.", + providers = [platform_common.ToolchainInfo], + ), "_runfiles_library": attr.label( allow_single_file = True, default = "@bazel_tools//tools/bash/runfiles", diff --git a/ruby/private/gem_install.bzl b/ruby/private/gem_install.bzl index 31ece11e..25376dfa 100644 --- a/ruby/private/gem_install.bzl +++ b/ruby/private/gem_install.bzl @@ -12,6 +12,8 @@ def _rb_gem_install_impl(ctx): gem = ctx.file.gem install_dir = ctx.actions.declare_directory(gem.basename[:-4]) toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"] + if ctx.attr.ruby != None: + toolchain = ctx.attr.ruby[platform_common.ToolchainInfo] env = {} env.update(toolchain.env) @@ -71,6 +73,10 @@ rb_gem_install = rule( mandatory = True, doc = "Gem file to install.", ), + "ruby": attr.label( + doc = "Override Ruby toolchain to use when installing the gem.", + providers = [platform_common.ToolchainInfo], + ), "_gem_install_cmd_tpl": attr.label( allow_single_file = True, default = "@rules_ruby//ruby/private/gem_install:gem_install.cmd.tpl", diff --git a/ruby/private/gem_push.bzl b/ruby/private/gem_push.bzl index 5c4726e9..c9259355 100644 --- a/ruby/private/gem_push.bzl +++ b/ruby/private/gem_push.bzl @@ -49,6 +49,7 @@ Gem file to push to RubyGems. You would usually use an output of `rb_gem_build() ), env = BINARY_ATTRS["env"], env_inherit = BINARY_ATTRS["env_inherit"], + ruby = BINARY_ATTRS["ruby"], _binary_cmd_tpl = BINARY_ATTRS["_binary_cmd_tpl"], _binary_sh_tpl = BINARY_ATTRS["_binary_sh_tpl"], _windows_constraint = BINARY_ATTRS["_windows_constraint"],