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 7 using WORKSPACE and Bzlmod (tested on CI);
  • Bazel 6 using WORKSPACE and Bzlmod (no longer tested on CI);
  • Bazel 5 using WORKSPACE (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.0.6",
    # 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.

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.3 🟩 🟩 🟩
MRI 3.2 🟩 🟩 🟩
MRI 3.1 🟩 🟩 🟩
JRuby 9.4 🟩 🟩 🟩
TruffleRuby 24.0 🟩 🟩 🟥

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

Ruby Linux macOS Windows
MRI 2.7 🟩 🟩 🟩
MRI 3.0 🟩 🟩 🟩
JRuby 9.3 🟩 🟩 🟩
TruffleRuby 22.0 🟩 🟩 🟥
TruffleRuby 23.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.

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