repository_rules.md

June 3, 2026 · View on GitHub

Public API for repository rules

rb_bundle

load("@rules_ruby//ruby:deps.bzl", "rb_bundle")

rb_bundle(toolchain, **kwargs)

Wraps rb_bundle_rule() providing default toolchain name.

PARAMETERS

NameDescriptionDefault Value
toolchaindefault Ruby toolchain BUILD"@ruby//:BUILD"
kwargsunderlying attrs passed to rb_bundle_rule()none

rb_register_toolchains

load("@rules_ruby//ruby:deps.bzl", "rb_register_toolchains")

rb_register_toolchains(name, version, version_file, msys2_packages, portable_ruby,
                       portable_ruby_release_suffix, portable_ruby_checksums, resolved_version,
                       register, **kwargs)

Register a Ruby toolchain and lazily download the Ruby Interpreter.

  • (For MRI on Linux and macOS) Installed using ruby-build.
  • (For MRI on Windows) Installed using RubyInstaller.
  • (For JRuby on any OS) Downloaded and installed directly from official website.
  • (For TruffleRuby on Linux and macOS) Installed using ruby-build.
  • (With portable_ruby) Portable Ruby downloaded from bazel-contrib/portable-ruby.
  • (For "system") Ruby found on the PATH is used. Please note that builds are not hermetic in this case.

When portable_ruby = True, this function registers a Bazel toolchain per supported execution platform so that builds resolve to the right interpreter on remote execution and cross-platform setups. Per-platform repositories @<name>_<platform> are created lazily — Bazel only fetches the one matching the resolved exec platform. A hub repository @<name> aliases the canonical targets (:bundle, :gem, :ruby, :headers, :jars, etc.) via select(), preserving direct references like @ruby//:bundle.

JRuby's archive is platform-independent (JVM-based), so it is registered as a single unconstrained toolchain — no per-platform repos needed.

Other modes (ruby-build for MRI source compile, TruffleRuby, RubyInstaller, system) remain single-platform host-only.

WORKSPACE:

load("@rules_ruby//ruby:deps.bzl", "rb_register_toolchains")

rb_register_toolchains(
    version = "3.0.6"
)

Once registered, you can use the toolchain directly as it provides all the binaries:

$ bazel run @ruby -- -e "puts RUBY_VERSION"
$ bazel run @ruby//:bundle -- update
$ bazel run @ruby//:gem -- install rails

You can also use Ruby engine targets to select() depending on installed Ruby interpreter:

BUILD:

rb_library(
    name = "my_lib",
    srcs = ["my_lib.rb"],
    deps = select({
        "@ruby//engine:jruby": [":my_jruby_lib"],
        "@ruby//engine:truffleruby": ["//:my_truffleruby_lib"],
        "@ruby//engine:ruby": ["//:my__lib"],
        "//conditions:default": [],
    }),
)

PARAMETERS

NameDescriptionDefault Value
namebase name of resulting repositories, by default "ruby""ruby"
versiona semver version of MRI, or a string like [interpreter type]-[version], or "system"None
version_file.ruby-version or .tool-versions file to read version fromNone
msys2_packagesextra MSYS2 packages to install["libyaml"]
portable_rubywhen True, downloads portable Ruby from bazel-contrib/portable-ruby instead of compiling via ruby-build. Has no effect on JRuby, TruffleRuby, or Windows.False
portable_ruby_release_suffixrelease suffix for portable Ruby (default "1", e.g. "2" downloads X.Y.Z-2).""
portable_ruby_checksumsplatform checksums for portable Ruby downloads, overriding built-in checksums.{}
resolved_versionthe version string resolved from version_file by the module extension. Used to detect JRuby (which skips the multi-platform portable_ruby path since its archive is platform-independent).None
registerwhether to register the resulting toolchains, should be False under bzlmodTrue
kwargsadditional parameters to the downloader for this interpreter typenone

rb_bundle_fetch

load("@rules_ruby//ruby:deps.bzl", "rb_bundle_fetch")

rb_bundle_fetch(name, srcs, auth_patterns, bundler_checksums, bundler_remote, env, gem_checksums,
                gemfile, gemfile_lock, jar_checksums, netrc, repo_mapping, ruby)

Fetches Bundler dependencies to be automatically installed by other targets.

Currently doesn't support installing gems from Git repositories, see https://github.com/bazel-contrib/rules_ruby/issues/62.

WORKSPACE:

load("@rules_ruby//ruby:deps.bzl", "rb_bundle_fetch")

rb_bundle_fetch(
    name = "bundle",
    gemfile = "//:Gemfile",
    gemfile_lock = "//:Gemfile.lock",
    srcs = [
        "//:gem.gemspec",
        "//:lib/gem/version.rb",
    ]
)

Checksums for gems in Gemfile.lock are printed by the ruleset during the build. It's recommended to add them to gem_checksums attribute.

WORKSPACE:

rb_bundle_fetch(
    name = "bundle",
    gemfile = "//:Gemfile",
    gemfile_lock = "//:Gemfile.lock",
    gem_checksums = {
        "ast-2.4.2": "1e280232e6a33754cde542bc5ef85520b74db2aac73ec14acef453784447cc12",
        "concurrent-ruby-1.2.2": "3879119b8b75e3b62616acc256c64a134d0b0a7a9a3fcba5a233025bcde22c4f",
    },
)

All the installed gems can be accessed using @bundle target and additionally gems binary files can also be used via BUILD rules or directly with bazel run:

BUILD:

load("@rules_ruby//ruby:defs.bzl", "rb_test")

package(default_visibility = ["//:__subpackages__"])

rb_test(
    name = "rubocop",
    main = "@bundle//bin:rubocop",
    deps = ["@bundle"],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
srcsList of Ruby source files necessary during installation.List of labelsoptional[]
auth_patternsA list of patterns to match against urls for which the auth object should be used.Dictionary: String -> Stringoptional{}
bundler_checksumsCustom map from Bundler version to its SHA-256 checksum.Dictionary: String -> Stringoptional{}
bundler_remoteRemote to fetch the bundler gem from.Stringoptional"https://rubygems.org/"
envEnvironment variables to use during installation.Dictionary: String -> Stringoptional{}
gem_checksumsSHA-256 checksums for remote gems. Keys are gem names (e.g. foobar-1.2.3), values are SHA-256 checksums.Dictionary: String -> Stringoptional{}
gemfileGemfile to install dependencies from.Labelrequired
gemfile_lockGemfile.lock to install dependencies from.Labelrequired
jar_checksumsSHA-256 checksums for JAR dependencies. Keys are Maven coordinates (e.g. org.yaml:snakeyaml:1.33), values are SHA-256 checksums.Dictionary: String -> Stringoptional{}
netrcPath to .netrc file to read credentials fromStringoptional""
repo_mappingIn 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 -> Stringoptional
rubyOverride Ruby toolchain to use for installation.LabeloptionalNone

rb_bundle_rule

load("@rules_ruby//ruby:deps.bzl", "rb_bundle_rule")

rb_bundle_rule(name, srcs, env, gemfile, repo_mapping, toolchain)

(Deprecated) Use rb_bundle_fetch() instead.

Installs Bundler dependencies and registers an external repository that can be used by other targets.

WORKSPACE:

load("@rules_ruby//ruby:deps.bzl", "rb_bundle")

rb_bundle(
    name = "bundle",
    gemfile = "//:Gemfile",
    srcs = [
        "//:gem.gemspec",
        "//:lib/gem/version.rb",
    ]
)

All the installed gems can be accessed using @bundle target and additionally gems binary files can also be used:

BUILD:

load("@rules_ruby//ruby:defs.bzl", "rb_binary")

package(default_visibility = ["//:__subpackages__"])

rb_binary(
    name = "rubocop",
    main = "@bundle//:bin/rubocop",
    deps = ["@bundle"],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
srcsList of Ruby source files used to build the library.List of labelsoptional[]
envEnvironment variables to use during installation.Dictionary: String -> Stringoptional{}
gemfileGemfile to install dependencies from.LabeloptionalNone
repo_mappingIn 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 -> Stringoptional
toolchain-Labelrequired