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
7 changes: 7 additions & 0 deletions examples/gem/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ sh_test(
data = ["@bundle//:BUILD"],
)

sh_test(
name = "launcher_env_check_test",
srcs = ["launcher_env_check.sh"],
args = ["$(location //lib/gem:add-numbers)"],
data = ["//lib/gem:add-numbers"],
)

rb_binary(
name = "rake",
main = "@bundle//bin:rake",
Expand Down
10 changes: 10 additions & 0 deletions examples/gem/launcher_env_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -euo pipefail

# TruffleRuby legitimately records Homebrew prefixes, so only look for paths
# into the downloaded Ruby's `dist/` directory.
if grep -q -E -e '--with-(libyaml|openssl)-dir=[^ ]*/dist' "$1"; then
echo "launcher embeds build-time Bundler options or absolute dist paths:" >&2
grep -E -e '--with-(libyaml|openssl)-dir=[^ ]*/dist' "$1" >&2
exit 1
fi
3 changes: 3 additions & 0 deletions ruby/private/bundle_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ load(
_convert_env_to_script = "convert_env_to_script",
_is_windows = "is_windows",
_normalize_path = "normalize_path",
_resolve_bundle_build_env = "resolve_bundle_build_env",
_to_rlocation_path = "to_rlocation_path",
)

Expand Down Expand Up @@ -64,6 +65,8 @@ def _rb_bundle_install_impl(ctx):

env = {}
env.update(toolchain.env)
if not _is_windows(ctx):
env.update(_resolve_bundle_build_env(toolchain.ruby.path, getattr(toolchain, "bundle_build_env", {})))
env.update(attr_env)

bundler_env = {}
Expand Down
100 changes: 89 additions & 11 deletions ruby/private/download.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,16 @@ def _rb_download_impl(repository_ctx):

engine = "ruby"
env = {}
bundle_build_env = {}
sha256 = None
ruby_binary_name = "ruby"
gem_binary_name = "gem"

platform = _resolve_platform(repository_ctx)
is_windows_target = platform.endswith("_windows")

if version.startswith("jruby"):
_install_jruby(repository_ctx, version)
sha256 = _install_jruby(repository_ctx, version)

engine = "jruby"
ruby_binary_name = "jruby"
Expand Down Expand Up @@ -192,7 +194,7 @@ def _rb_download_impl(repository_ctx):
# (multi-platform mode picking the Windows per-platform repo).
_install_via_rubyinstaller(repository_ctx, version)
elif repository_ctx.attr.portable_ruby:
_install_portable_ruby(
sha256 = _install_portable_ruby(
repository_ctx,
version,
platform,
Expand All @@ -201,12 +203,12 @@ def _rb_download_impl(repository_ctx):

# Portable Ruby may ship with libyaml and openssl which are needed to compile the
# psych and openssl gems respectively when built with --incompatible_strict_action_env).
# TODO: This won't work on RBE.
dist_dir = str(repository_ctx.path("dist"))
if repository_ctx.path("dist/include/yaml.h").exists:
env.update({"BUNDLE_BUILD__PSYCH": "--with-libyaml-dir=" + dist_dir})
if repository_ctx.path("dist/include/openssl").exists:
env.update({"BUNDLE_BUILD__OPENSSL": "--with-openssl-dir=" + dist_dir})
# The paths are relative to this repository and are made absolute when the action
# runs (see `resolve_bundle_build_env`), so the repository has no absolute paths.
bundle_build_env = portable_ruby_bundle_build_env(
has_libyaml = repository_ctx.path("dist/include/yaml.h").exists,
has_openssl = repository_ctx.path("dist/include/openssl").exists,
)
else:
_install_via_ruby_build(repository_ctx, version)

Expand All @@ -228,6 +230,7 @@ def _rb_download_impl(repository_ctx):
"{ruby_binary_name}": ruby_binary_name,
"{gem_binary_name}": gem_binary_name,
"{env}": repr(env),
"{bundle_build_env}": repr(bundle_build_env),
"{includes}": repr(includes),
},
)
Expand All @@ -241,6 +244,69 @@ def _rb_download_impl(repository_ctx):
},
)

# Bazel 8.3+ can reuse the repository from the repository cache when it is
# marked reproducible. Only do that when nothing host-specific was written.
if hasattr(repository_ctx, "repo_metadata") and is_reproducible(
version = version,
portable_ruby = repository_ctx.attr.portable_ruby,
is_windows_target = is_windows_target,
platform = repository_ctx.attr.platform,
sha256 = sha256,
):
return repository_ctx.repo_metadata(reproducible = True)
return None

def portable_ruby_bundle_build_env(has_libyaml, has_openssl):
"""Returns Bundler build options for libraries shipped with portable Ruby.

Values are `<flag>=<path relative to the repository root>`. Rules that
compile native extensions turn the path into an absolute one at action time
with `resolve_bundle_build_env`.

Args:
has_libyaml: whether `dist/` ships libyaml headers
has_openssl: whether `dist/` ships openssl headers

Returns:
dict of environment variable name to relative value
"""
env = {}
if has_libyaml:
env["BUNDLE_BUILD__PSYCH"] = "--with-libyaml-dir=dist"
if has_openssl:
env["BUNDLE_BUILD__OPENSSL"] = "--with-openssl-dir=dist"
return env

def is_reproducible(version, portable_ruby, is_windows_target, platform, sha256):
"""Returns whether the downloaded Ruby repository is reproducible.

Portable Ruby is, when the archive is pinned by sha256 and the platform is
set explicitly rather than detected from the host. JRuby is, when its
archive is pinned by sha256; the archive is the same on every platform, so
neither `portable_ruby` nor `platform` matter. System Ruby depends on the
host, TruffleRuby records Homebrew prefixes, and Windows and ruby-build
installs are not covered.

Args:
version: Ruby version string
portable_ruby: value of the `portable_ruby` attribute
is_windows_target: whether the repository targets Windows
platform: value of the `platform` attribute (empty when detected from the host)
sha256: sha256 the downloaded archive was verified against, or None

Returns:
bool
"""
if not sha256:
return False
if version.startswith("jruby"):
return True
if not portable_ruby or is_windows_target or not platform:
return False
if version == "system" or version.startswith("truffleruby"):
return False
return True

def _read_version_from_file(repository_ctx):
version = repository_ctx.read(repository_ctx.attr.version_file).strip("\r\n")
if repository_ctx.attr.version_file.name == ".tool-versions":
Expand Down Expand Up @@ -277,9 +343,17 @@ def _install_jruby(repository_ctx, version):

# Always create the Windows `.cmd` wrappers — JRuby's archive is the same
# across platforms, and the BUILD.tpl select picks `bundle.cmd` / `jgem.cmd`
# when consumed at a Windows target config. Symlinks are harmless on Unix.
repository_ctx.symlink("dist/bin/bundle.bat", "dist/bin/bundle.cmd")
repository_ctx.symlink("dist/bin/jgem.bat", "dist/bin/jgem.cmd")
# when consumed at a Windows target config. They are copies rather than
# symlinks, which would point to an absolute path and keep the repository
# from being reproducible. They are harmless on Unix.
for name in ["bundle", "jgem"]:
repository_ctx.file(
"dist/bin/%s.cmd" % name,
repository_ctx.read("dist/bin/%s.bat" % name),
executable = True,
)

return sha256

# https://github.com/oneclick/rubyinstaller2/wiki/FAQ#q-how-do-i-perform-a-silentunattended-install-with-the-rubyinstaller
def _install_via_rubyinstaller(repository_ctx, version):
Expand Down Expand Up @@ -349,6 +423,9 @@ def _install_portable_ruby(repository_ctx, ruby_version, platform, checksums):
ruby_version: Ruby version (e.g., "3.4.8")
platform: Canonical platform key (e.g., "x86_64_linux")
checksums: Dict mapping artifact names to SHA256 checksums

Returns:
The sha256 the archive was verified against, or None when unknown.
"""
if platform not in PORTABLE_RUBY_PLATFORMS:
fail("portable Ruby is not available for platform {}; supported: {}".format(
Expand Down Expand Up @@ -385,6 +462,7 @@ def _install_portable_ruby(repository_ctx, ruby_version, platform, checksums):
stripPrefix = "ruby-{}".format(ruby_version),
**kwargs
)
return kwargs.get("sha256")

def _symlink_system_ruby(repository_ctx):
_symlink_system_ruby_dir("bindir", repository_ctx)
Expand Down
1 change: 1 addition & 0 deletions ruby/private/download/BUILD.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ rb_toolchain(
"@platforms//os:windows": "dist/bin/bundle.cmd",
"//conditions:default": "dist/bin/bundle",
}),
bundle_build_env = {bundle_build_env},
env = {env},
files = [":files"],
gem = select({
Expand Down
3 changes: 3 additions & 0 deletions ruby/private/gem_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ load(
_convert_env_to_script = "convert_env_to_script",
_is_windows = "is_windows",
_normalize_path = "normalize_path",
_resolve_bundle_build_env = "resolve_bundle_build_env",
)

def _rb_gem_install_impl(ctx):
Expand All @@ -17,6 +18,8 @@ def _rb_gem_install_impl(ctx):

env = {}
env.update(toolchain.env)
if not _is_windows(ctx):
env.update(_resolve_bundle_build_env(toolchain.ruby.path, getattr(toolchain, "bundle_build_env", {})))

tools = []
tools.extend(toolchain.files)
Expand Down
29 changes: 29 additions & 0 deletions ruby/private/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,35 @@ def convert_env_to_script(ctx, env):

return newline.join(environment)

def resolve_bundle_build_env(ruby_path, bundle_build_env):
"""Makes the relative paths in `bundle_build_env` absolute at action time.

Each value has the form `<flag>=<path>`, with `<path>` relative to the
toolchain root: the directory two levels above the `ruby` binary
(`<root>/dist/bin/ruby`). The result is a shell value that prefixes the
path with `$PWD`, the execroot when the action starts. Paths are resolved on
the machine that runs the action, so this also works with remote execution.

Only for shell scripts. Windows toolchains never set `bundle_build_env`.

Args:
ruby_path: exec path of the toolchain `ruby` binary
bundle_build_env: dict of environment variable name to relative value

Returns:
dict of environment variable name to a quoted shell value
"""
root = ruby_path.rpartition("/")[0].rpartition("/")[0].rpartition("/")[0]
resolved = {}
for name, value in bundle_build_env.items():
flag, sep, path = value.partition("=")
if not sep:
fail("invalid bundle_build_env value for %s: %r (expected <flag>=<path>)" % (name, value))
if root:
path = root + "/" + path
resolved[name] = '"%s=$PWD/%s"' % (flag, path)
return resolved

def normalize_path(ctx, path):
"""Converts path to an OS-specific equivalent.

Expand Down
3 changes: 3 additions & 0 deletions ruby/tests/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load(":download_tests.bzl", "download_test_suite")
load(":jars_downloader_tests.bzl", "jars_downloader_test_suite")
load(":toolchain_selection_tests.bzl", "toolchain_selection_test_suite")
load(":utils_tests.bzl", "utils_test_suite")
Expand All @@ -7,3 +8,5 @@ jars_downloader_test_suite()
utils_test_suite()

toolchain_selection_test_suite()

download_test_suite()
73 changes: 73 additions & 0 deletions ruby/tests/download_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Unit tests for the portable Ruby download helpers."""

load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load(
"//ruby/private:download.bzl",
_is_reproducible = "is_reproducible",
_portable_ruby_bundle_build_env = "portable_ruby_bundle_build_env",
)

def _portable_ruby_bundle_build_env_test_impl(ctx):
env = unittest.begin(ctx)

# The psych/openssl options are kept, with repository-relative paths.
asserts.equals(
env,
{
"BUNDLE_BUILD__PSYCH": "--with-libyaml-dir=dist",
"BUNDLE_BUILD__OPENSSL": "--with-openssl-dir=dist",
},
_portable_ruby_bundle_build_env(has_libyaml = True, has_openssl = True),
)
asserts.equals(
env,
{"BUNDLE_BUILD__PSYCH": "--with-libyaml-dir=dist"},
_portable_ruby_bundle_build_env(has_libyaml = True, has_openssl = False),
)
asserts.equals(
env,
{"BUNDLE_BUILD__OPENSSL": "--with-openssl-dir=dist"},
_portable_ruby_bundle_build_env(has_libyaml = False, has_openssl = True),
)
asserts.equals(
env,
{},
_portable_ruby_bundle_build_env(has_libyaml = False, has_openssl = False),
)

# No value may be an absolute path.
for value in _portable_ruby_bundle_build_env(has_libyaml = True, has_openssl = True).values():
asserts.false(env, value.partition("=")[2].startswith("/"), "absolute path in %s" % value)

return unittest.end(env)

def _is_reproducible_test_impl(ctx):
env = unittest.begin(ctx)

pinned = {"platform": "x86_64_linux", "sha256": "0" * 64}
asserts.true(env, _is_reproducible("3.4.8", portable_ruby = True, is_windows_target = False, **pinned))
asserts.false(env, _is_reproducible("3.4.8", portable_ruby = False, is_windows_target = False, **pinned))
asserts.false(env, _is_reproducible("3.4.8", portable_ruby = True, is_windows_target = True, **pinned))
asserts.false(env, _is_reproducible("system", portable_ruby = True, is_windows_target = False, **pinned))
asserts.false(env, _is_reproducible("truffleruby-25.0.0", portable_ruby = True, is_windows_target = False, **pinned))

# JRuby is reproducible once pinned; its archive is platform independent.
asserts.true(env, _is_reproducible("jruby-10.0.2.0", portable_ruby = True, is_windows_target = False, **pinned))
asserts.true(env, _is_reproducible("jruby-10.0.2.0", portable_ruby = False, is_windows_target = True, platform = "", sha256 = "0" * 64))
asserts.false(env, _is_reproducible("jruby-10.0.2.0", portable_ruby = True, is_windows_target = False, platform = "x86_64_linux", sha256 = None))

# Host-detected platform or an unpinned download is not reproducible.
asserts.false(env, _is_reproducible("3.4.8", portable_ruby = True, is_windows_target = False, platform = "", sha256 = "0" * 64))
asserts.false(env, _is_reproducible("3.4.8", portable_ruby = True, is_windows_target = False, platform = "x86_64_linux", sha256 = None))

return unittest.end(env)

portable_ruby_bundle_build_env_test = unittest.make(_portable_ruby_bundle_build_env_test_impl)
is_reproducible_test = unittest.make(_is_reproducible_test_impl)

def download_test_suite():
unittest.suite(
"download_tests",
portable_ruby_bundle_build_env_test,
is_reproducible_test,
)
Loading
Loading