Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 29 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,92 +129,58 @@ Keep in mind, that it takes some time for compilation to complete.

On Windows, [RubyInstaller][6] is used to install MRI.

#### Fast Installation with rv-ruby
#### Fast Installation with Portable Ruby

For faster MRI installation on Linux and macOS, you can use prebuilt Ruby
binaries from [rv-ruby][19] instead of compiling from source. This significantly
For faster MRI installation on Linux and macOS, you can use portable Ruby
binaries from [jdx/ruby][19] 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.

```bash
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:
To enable portable Ruby, set `portable_ruby = True` in your toolchain declaration:

```bazel
ruby = use_extension("@rules_ruby//ruby:extensions.bzl", "ruby")
ruby.toolchain(
name = "ruby",
portable_ruby = True,
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**
This ruleset ships with [default checksums][20] to securely download and properly cache
the Ruby binaries. If you want to use Ruby version not available with ruleset release,
you should use `portable_ruby_checksums` attribute.

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.
We have provided the `generate_portable_ruby_checksums` utility to add/update these
attributes for you. The utility needs to know the version of Ruby to download.
By default, it will use the Ruby version specified in the `.ruby-version` file.

```bash
bazel run @rules_ruby//tools/generate_excluded_gems
bazel run @rules_ruby//tools/generate_portable_ruby_checksums -- 3.4.8
```

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:
After running the utility, the toolchain declaration in your `MODULE.bazel`
should look something like the following:

```bazel
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",
],
ruby = use_extension("@rules_ruby//ruby:extensions.bzl", "ruby")
ruby.toolchain(
name = "ruby",
version_file = "//:.ruby-version",
portable_ruby = True,
portable_ruby_checksums = {
"ruby-3.4.8.x86_64_linux.tar.gz": "e1c5ed91dc8b05e516cb5386a695e5ffed7b585fd577b93880b7eb61d20092e7",
"ruby-3.4.8.macos.tar.gz": "46c48fceb34d11b848f1fd7456ac77df49406f355de4f7d5667f254ea9da2f84",
"ruby-3.4.8.arm64_linux.tar.gz": "fdf6833e7ebe0b9c26a151a6f7481d81e178372046ad2b3f54ae56d159da8b1e",
},
)
```

> [!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).
- Portable Ruby is only supported on Linux (arm64, x86_64) and macOS (arm64).
- Setting `portable_ruby = True` has no effect on JRuby, TruffleRuby, or Windows.
- 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.
- Find available portable Ruby releases at https://github.com/jdx/ruby/releases

### JRuby

Expand Down Expand Up @@ -267,4 +233,5 @@ However, some are known not to work or work only partially (e.g. mRuby has no bu
[16]: https://bazel.build/reference/command-line-reference#flag--experimental_inprocess_symlink_creation
[17]: https://github.com/bazelbuild/bazel/issues/4327
[18]: docs/rails.md
[19]: https://github.com/spinel-coop/rv-ruby
[19]: https://github.com/jdx/ruby
[20]: ruby/private/portable_ruby_checksums.bzl
15 changes: 7 additions & 8 deletions docs/repository_rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/deep_gem/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local_path_override(
ruby = use_extension("@rules_ruby//ruby:extensions.bzl", "ruby")
ruby.toolchain(
name = "ruby",
portable_ruby = True,
version_file = "//:.ruby-version",
)
ruby.bundle_fetch(
Expand Down
5 changes: 1 addition & 4 deletions examples/gem/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ source 'https://rubygems.org'
gemspec

gem 'debug', '>= 1.0.0', platforms: %i[mri mswin64]
# Gem with native extensions.
# Use 5.0.1 to test for Jars fetching on JRuby.
# https://github.com/bazel-contrib/rules_ruby/issues/218
gem 'psych', '5.0.1'
gem 'psych', '~> 5.3' # native extensions
gem 'rspec', '~> 3.0'
gem 'rubocop', '~> 1.64'
18 changes: 9 additions & 9 deletions examples/gem/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ GEM
specs:
ast (2.4.2)
concurrent-ruby (1.3.5)
date (3.5.1)
date (3.5.1-java)
debug (1.10.0)
irb (~> 1.10)
reline (>= 0.3.8)
Expand All @@ -33,9 +35,11 @@ GEM
pp (0.6.2)
prettyprint
prettyprint (0.2.0)
psych (5.0.1)
psych (5.3.1)
date
stringio
psych (5.0.1-java)
psych (5.3.1-java)
date
jar-dependencies (>= 0.1.7)
racc (1.8.1)
racc (1.8.1-java)
Expand Down Expand Up @@ -78,21 +82,17 @@ GEM
unicode-emoji (4.2.0)

PLATFORMS
arm64-darwin-23
arm64-darwin-24
arm64-darwin
java
ruby
universal-java
universal-java-1.8
universal-java-11
universal-java-17
universal-java-18
universal-java-21
x64-mingw32

DEPENDENCIES
debug (>= 1.0.0)
example!
psych (= 5.0.1)
psych (~> 5.3)
rspec (~> 3.0)
rubocop (~> 1.64)

Expand Down
9 changes: 6 additions & 3 deletions examples/gem/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local_path_override(
ruby = use_extension("@rules_ruby//ruby:extensions.bzl", "ruby")
ruby.toolchain(
name = "ruby",
portable_ruby = True,
version_file = "//:.ruby-version",
)
use_repo(ruby, "ruby")
Expand All @@ -29,6 +30,8 @@ ruby.bundle_fetch(
gem_checksums = {
"ast-2.4.2": "1e280232e6a33754cde542bc5ef85520b74db2aac73ec14acef453784447cc12",
"concurrent-ruby-1.3.5": "813b3e37aca6df2a21a3b9f1d497f8cbab24a2b94cab325bffe65ee0f6cbebc6",
"date-3.5.1": "750d06384d7b9c15d562c76291407d89e368dda4d4fff957eb94962d325a0dc0",
"date-3.5.1-java": "12e09477dc932afe45bf768cd362bf73026804e0db1e6c314186d6cd0bee3344",
"debug-1.10.0": "11e28ca74875979e612444104f3972bd5ffb9e79179907d7ad46dba44bd2e7a4",
"diff-lcs-1.5.1": "273223dfb40685548436d32b4733aa67351769c7dea621da7d9dd4813e63ddfe",
"i18n-1.14.7": "ceba573f8138ff2c0915427f1fc5bdf4aa3ab8ae88c8ce255eb3ecf0a11a5d0f",
Expand All @@ -43,8 +46,8 @@ ruby.bundle_fetch(
"parser-3.3.7.0": "7449011771e3e7881297859b849de26a6f4fccd515bece9520a87e7d2116119b",
"pp-0.6.2": "947ec3120c6f92195f8ee8aa25a7b2c5297bb106d83b41baa02983686577b6ff",
"prettyprint-0.2.0": "2bc9e15581a94742064a3cc8b0fb9d45aae3d03a1baa6ef80922627a0766f193",
"psych-5.0.1": "43264252cc33b8e626fb940cddc7379d00f7583500680da32c74a54bd9b000d0",
"psych-5.0.1-java": "c4a94895e325d326063f9d53f5e4f231a5df3a6e593ce9563d19b35fe11f800d",
"psych-5.3.1": "eb7a57cef10c9d70173ff74e739d843ac3b2c019a003de48447b2963d81b1974",
"psych-5.3.1-java": "20a4a81ad01479ef060f604ed75ba42fe673169e67d923b1bae5aa4e13cc5820",
"racc-1.8.1": "4a7f6929691dbec8b5209a0b373bc2614882b55fc5d2e447a21aaa691303d62f",
"racc-1.8.1-java": "54f2e6d1e1b91c154013277d986f52a90e5ececbe91465d29172e49342732b98",
"rainbow-3.1.1": "039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a",
Expand All @@ -67,7 +70,7 @@ ruby.bundle_fetch(
gemfile = "//:Gemfile",
gemfile_lock = "//:Gemfile.lock",
jar_checksums = {
"org.yaml:snakeyaml:1.33": "11ff459788f0a2d781f56a4a86d7e69202cebacd0273d5269c4ae9f02f3fd8f0",
"org.snakeyaml:snakeyaml-engine:2.10": "c99d9fd66c7c251d881a9cd95089b7c8044c29a1b02983d7036981bd4354ec37",
},
)
use_repo(ruby, "bundle", "ruby_toolchains")
Expand Down
1 change: 1 addition & 0 deletions examples/gem/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ http_archive(
load("@rules_ruby//ruby:deps.bzl", "rb_bundle_fetch", "rb_register_toolchains")

rb_register_toolchains(
portable_ruby = True,
version_file = "//:.ruby-version",
)

Expand Down
1 change: 1 addition & 0 deletions examples/jekyll/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local_path_override(
ruby = use_extension("@rules_ruby//ruby:extensions.bzl", "ruby")
ruby.toolchain(
name = "ruby",
portable_ruby = True,
version = "3.2.8",
)
use_repo(ruby, "ruby")
Expand Down
1 change: 1 addition & 0 deletions examples/native_ext/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local_path_override(
ruby = use_extension("@rules_ruby//ruby:extensions.bzl", "ruby")
ruby.toolchain(
name = "ruby",
portable_ruby = True,
version_file = "//:.ruby-version",
)
use_repo(ruby, "ruby", "ruby_toolchains")
Expand Down
1 change: 1 addition & 0 deletions examples/proto/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local_path_override(
ruby = use_extension("@rules_ruby//ruby:extensions.bzl", "ruby")
ruby.toolchain(
name = "ruby",
portable_ruby = True,
version_file = "//:.ruby-version",
)
use_repo(ruby, "ruby", "ruby_toolchains")
Expand Down
24 changes: 0 additions & 24 deletions examples/rails/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,3 @@ source "https://rubygems.org"

# Rails for generating and running the People Tracker Rails application
gem "rails", "~> 8.0"

# Pin default gems with C extensions to versions bundled with rv-ruby Ruby
# binaries. The list can be found at https://stdgems.org/<ruby-version> (e.g,
# https://stdgems.org/3.4.8 for Ruby 3.4.8). These gems should NOT be
# downloaded - they're pre-compiled in rv-ruby with portable dependencies.
#
# IMPORTANT: These gems must be listed in the excluded_gems list in the
# bundle_fetch configured in your MODULE.bazel file.
gem "date", "3.4.1"
gem "digest", "3.2.0"
gem "etc", "1.4.6"
gem "fcntl", "1.2.0"
gem "fiddle", "1.1.6"
gem "io-console", "0.8.1"
gem "io-nonblock", "0.3.2"
gem "io-wait", "0.3.2"
gem "json", "2.9.1"
gem "openssl", "3.3.1"
gem "pathname", "0.4.0"
gem "prism", "1.5.2"
gem "psych", "5.2.2"
gem "stringio", "3.1.2"
gem "strscan", "3.1.2"
gem "zlib", "3.2.1"
Loading
Loading