Skip to content

bazel-contrib/rules_ruby

Repository files navigation

Ruby Rules for Bazel

Overview

This repository hosts Ruby language ruleset for Bazel.

The ruleset is known to work with:

  • Bazel 8 using WORKSPACE and Bzlmod (tested on CI).
  • Bazel 7 using WORKSPACE and Bzlmod (no longer tested on CI).
  • Bazel 6 using WORKSPACE and Bzlmod (no longer tested on CI).

Getting Started

WORKSPACE

  1. Install the ruleset following WORKSPACE instructions on the latest release.
  2. Download and install Ruby:
# WORKSPACE
load("@rules_ruby//ruby:deps.bzl", "rb_register_toolchains")

rb_register_toolchains(
    version = "3.3.9",
    # alternatively, load version from .ruby-version file
    # version_file = "//:.ruby-version",
)
  1. (Optional) Download and install Bundler dependencies:
# WORKSPACE
load("@rules_ruby//ruby:deps.bzl", "rb_bundle_fetch")

rb_bundle_fetch(
    name = "bundle",
    gemfile = "//:Gemfile",
    gemfile_lock = "//:Gemfile.lock",
)
  1. Start defining your library, binary and test targets in BUILD files.

Bzlmod

  1. Install ruleset following Bzlmod instructions on the latest release.
  2. Download and install Ruby:
# MODULE.bazel
ruby = use_extension("@rules_ruby//ruby:extensions.bzl", "ruby")
ruby.toolchain(
    name = "ruby",
    version = "3.0.6",
    # alternatively, load version from .ruby-version file
    # version_file = "//:.ruby-version",
)
use_repo(ruby, "ruby")
  1. (Optional) Download and install Bundler dependencies:
# MODULE.bazel
ruby.bundle_fetch(
    name = "bundle",
    gemfile = "//:Gemfile",
    gemfile_lock = "//:Gemfile.lock",
)
use_repo(ruby, "bundle", "ruby_toolchains")
  1. Register Ruby toolchains:
# MODULE.bazel
register_toolchains("@ruby_toolchains//:all")
  1. Start defining your library, binary and test targets in BUILD files.

Documentation

  • See repository rules for the documentation of WORKSPACE rules.
  • See rules for the documentation of BUILD rules.
  • See rails for the documentation of Ruby on Rails rules.

Examples

See examples directory for a comprehensive set of examples how to use the ruleset.

Toolchains

The following toolchains are known to work and tested on CI.

Ruby Linux macOS Windows
MRI 3.5 🟩 🟩 πŸŸ₯
MRI 3.4 🟩 🟩 🟩
MRI 3.3 🟩 🟩 🟩
MRI 3.2 🟩 🟩 🟩
JRuby 10.0 🟩 🟩 🟩
TruffleRuby 25.0 🟩 🟩 πŸŸ₯

The following toolchains were previously known to work but no longer tested on CI.

Ruby Linux macOS Windows
MRI 3.1 🟩 🟩 🟩
MRI 3.0 🟩 🟩 🟩
MRI 2.7 🟩 🟩 🟩
JRuby 9.4 🟩 🟩 🟩
JRuby 9.3 🟩 🟩 🟩
TruffleRuby 24.0 🟩 🟩 πŸŸ₯
TruffleRuby 23.0 🟩 🟩 πŸŸ₯
TruffleRuby 22.0 🟩 🟩 πŸŸ₯

MRI

On Linux and macOS, ruby-build is used to install MRI from sources. Keep in mind, that it takes some time for compilation to complete.

On Windows, RubyInstaller is used to install MRI.

Fast Installation with rv-ruby

For faster MRI installation on Linux and macOS, you can use prebuilt Ruby binaries from rv-ruby instead of compiling from source. This significantly reduces installation time and ensures consistent, portable Ruby environments.

Configure rv-ruby Downloads

To securely download and properly cache the Ruby binaries, the ruby.toolchain declaration must be updated with the rv_version and rv_checksums attributes.

We have provided the generate_rv_checksums utility to add/update these attributes for you. The utility needs to know the rv-ruby version to use (https://github.com/spinel-coop/rv-ruby/releases) and the version of Ruby to download. By default, it will use the Ruby version specified in the .ruby-version file.

bazel run @rules_ruby//tools/generate_rv_checksums -- 20251225

After running the utility, the toolchain declaration in your MODULE.bazel should look something like the following:

ruby = use_extension("@rules_ruby//ruby:extensions.bzl", "ruby")
ruby.toolchain(
    name = "ruby",
    version_file = "//:.ruby-version",
    rv_version = "20251225",
    rv_checksums = {
        "linux-arm64": "0c08c35a99f10817643d548f98012268c5433ae25a737ab4d6751336108a941d",
        "linux-x86_64": "f36cef10365d370e0867f0c3ac36e457a26ab04f3cfbbd7edb227a18e6e9b3c3",
        "macos-arm64": "cd9d7a1428076bfcc6c2ca3c0eb69b8e671e9b48afb4c351fa4a84927841ffef",
        "macos-x86_64": "e9da39082d1dd8502d322c850924d929bc45b7a1e35da593a5606c00673218d4",
    },
)

Configure Excluded Gems

When using rv-ruby, you must exclude default gems with C extensions from bundle_fetch as these are pre-compiled in the rv-ruby binary. You may see compilation errors if you do not exclude these gems.

We have provided the generate_excluded_gems utility to update the declaration for you.

bazel run @rules_ruby//tools/generate_excluded_gems

The utility reads the Ruby version being used and checks https://raw.githubusercontent.com/janlelis/stdgems/main/default_gems.json to determine which gems should be excluded. The utility adds/updates the excluded_gems attribute with the correct list of gems. The bundle_fetch declaration will look something like the following:

ruby.bundle_fetch(
    name = "bundle",
    gemfile = "//:Gemfile",
    gemfile_lock = "//:Gemfile.lock",
    excluded_gems = [
        "date", "digest", "etc", "fcntl", "fiddle",
        "io-console", "io-nonblock", "io-wait", "json",
        "openssl", "pathname", "prism", "psych",
        "stringio", "strscan", "zlib",
    ],
)

Note

You can find an HTML-rendered list of the default gems for a Ruby version at https://stdgems.org/\<version> (e.g., https://stdgems.org/3.4.8 for Ruby 3.4.8).

Notes:

  • rv-ruby is only supported on Linux and macOS (x86_64 and arm64).
  • On Windows, the toolchain automatically falls back to RubyInstaller.
  • Find available rv-ruby releases at https://github.com/spinel-coop/rv-ruby/releases
  • The utilities support --name to target specific toolchains/bundles and --module-bazel to specify a custom MODULE.bazel path.
  • Run utilities with --dry-run to preview changes without modifying files.

JRuby

On all operating systems, JRuby is downloaded manually. It uses Bazel runtime Java toolchain as JDK. JRuby is currently the only toolchain that supports Remote Build Execution.

TruffleRuby

On Linux and macOS, ruby-build is used to install TruffleRuby. Windows is not supported.

Other

On Linux and macOS, you can potentially use any Ruby distribution that is supported by ruby-build. However, some are known not to work or work only partially (e.g. mRuby has no bundler support).

Known Issues

About

Ruby ruleset for Bazel

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Contributors 15

Languages