Skip to content

ruby: put resolved Ruby's bin dir on $PATH for gem invocations#2021

Merged
j178 merged 6 commits intoj178:masterfrom
xiaoyanli-lyft:ruby-bin-on-path-for-gem
Apr 28, 2026
Merged

ruby: put resolved Ruby's bin dir on $PATH for gem invocations#2021
j178 merged 6 commits intoj178:masterfrom
xiaoyanli-lyft:ruby-bin-on-path-for-gem

Conversation

@xiaoyanli-lyft
Copy link
Copy Markdown
Contributor

@xiaoyanli-lyft xiaoyanli-lyft commented Apr 27, 2026

Summary

build_gemspec, resolve_gems, install_single_gem, and install_gems_sequential in crates/prek/src/languages/ruby/gem.rs all spawn <ruby> -S gem .... Ruby's -S flag searches $PATH for the named script.

The rv-ruby tarballs that prek auto-downloads ship gem next to ruby in the same bin/ directory, but prek wasn't putting that directory on $PATH for these child commands. In environments without a system Ruby on PATH (e.g. a vanilla Docker image / CI container), the -S lookup fails and Ruby aborts with:

/<PREK_HOME>/tools/ruby/<version>/bin/ruby: No such file or directory -- gem (LoadError)

The first place this trips is build_gemspec (called from Ruby::install before install_gems), so the user-visible failure is Failed to install hook ...: Failed to build gemspecs. Once that's worked around, gem install --explain would fail identically — same root cause for all four call sites.

This is distinct from #2006 / #2017, which fixed missing gems/bin/ruby shim used at hook run time. The bug here is at hook install time and is not mitigated by 0.3.11 (gem.rs is unchanged across v0.3.10..v0.3.11).

Reproduction repo / context: filed as a separate issue with the full repro alongside this PR.

Fix

Prepend the resolved Ruby's bin dir to $PATH for every ruby -S gem invocation:

  • New ruby_path_env(&RubyResult) -> Result<OsString> helper that uses prek_consts::prepend_paths (same helper used by Ruby::run).
  • gem_env now takes &RubyResult in addition to gem_home and sets PATH alongside the existing GEM_HOME / BUNDLE_* / MAKEFLAGS env. Returns Result so the prepend_paths join error can propagate (vanishingly rare in practice).
  • build_gemspec doesn't have a GEM_HOME to set so it doesn't use gem_env, but it sets PATH directly via the same helper.

Test plan

  • cargo fmt --check -p prek — clean
  • cargo clippy -p prek --all-targets -- -D warnings — clean
  • cargo test -p prek --bin prek — 400 passed; 0 failed (existing unit tests, including all five languages::ruby::gem::tests::*)
  • New integration test: languages::ruby::auto_download_gem_install_without_gem_on_path (CI-only, mirrors the existing auto_download gating). It:
    • pins language_version: '3.2.9' to force prek to auto-download Ruby via rv-ruby (and asserts a tools/ruby/3.2.9* directory exists after the run);
    • drops a .gemspec into the local repo so build_gemspec runs;
    • sets additional_dependencies: ["webrick"] so resolve_gems (gem install --explain) and install_single_gem (gem install) run;
    • scrubs ruby and gem from $PATH via the existing remove_bin_from_path helper, so ruby -S gem ... only resolves if the resolved Ruby's bin dir is prepended (i.e. the test would have failed before this fix with the LoadError quoted above).

The pre-existing auto_download test only ran ruby --version and never hit gem; gemspec_workflow used language_version: system and so picked gem up from the system PATH. The new test combines auto-download + gem operations + scrubbed PATH to lock the regression in.

Manual verification of the failure mode (without this fix)

In a container with no system Ruby:

default_language_version:
    ruby: 3.2.1
repos:
  - repo: https://github.com/jumanjihouse/pre-commit-hooks
    rev: 3.0.0
    hooks:
      - id: rubocop
prek install-hooks
# error: Failed to install hook `rubocop`
#   caused by: Failed to build gemspecs
#   caused by: Command `gem build` exited with an error:
#   /.../tools/ruby/3.2.1/bin/ruby: No such file or directory -- gem (LoadError)

With this patch, prek prepends <ruby_prefix>/bin to $PATH, ruby -S gem resolves, and the install proceeds.

`build_gemspec`, `resolve_gems`, `install_single_gem` and
`install_gems_sequential` all spawn `<ruby> -S gem ...`. Ruby's `-S`
flag searches `$PATH` for the named script.

The rv-ruby tarballs that prek auto-downloads ship `gem` next to
`ruby` in the same `bin/` directory, but prek wasn't putting that
directory on `$PATH` for these child commands. In environments
without a system Ruby on `PATH` (e.g. a vanilla Docker image), the
`-S` lookup fails and Ruby aborts with:

    /<PREK_HOME>/tools/ruby/<version>/bin/ruby: No such file or directory -- gem (LoadError)

Prepend the resolved Ruby's bin dir to `$PATH` for every `ruby -S
gem` invocation. `gem_env` now also takes the resolved `RubyResult`
and sets `PATH`; `build_gemspec` (which doesn't use `gem_env` since
it has no `GEM_HOME` to set) sets `PATH` directly via the same
helper.

Made-with: Cursor
Copilot AI review requested due to automatic review settings April 27, 2026 17:57
@xiaoyanli-lyft xiaoyanli-lyft requested a review from j178 as a code owner April 27, 2026 17:57
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 27, 2026

Codecov Report

❌ Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 92.22%. Comparing base (6b9db86) to head (e2f02fe).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
crates/prek/src/languages/ruby/gem.rs 93.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2021      +/-   ##
==========================================
+ Coverage   92.19%   92.22%   +0.03%     
==========================================
  Files         117      117              
  Lines       23781    23790       +9     
==========================================
+ Hits        21924    21940      +16     
+ Misses       1857     1850       -7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Ruby hook install-time failures when prek uses an auto-downloaded rv-ruby toolchain by ensuring ruby -S gem ... can actually find the bundled gem script (which Ruby locates via $PATH).

Changes:

  • Add a ruby_path_env helper to prepend the resolved Ruby toolchain’s bin/ directory to PATH.
  • Apply the computed PATH to all ruby -S gem ... invocation sites (including build_gemspec and the shared gem_env helper).

Comment thread crates/prek/src/languages/ruby/gem.rs
@prek-ci-bot
Copy link
Copy Markdown

prek-ci-bot Bot commented Apr 27, 2026

📦 Cargo Bloat Comparison

Binary size change: -0.77% (26.0 MiB → 25.8 MiB)

Expand for cargo-bloat output

Head Branch Results

 File  .text     Size             Crate Name
 1.3%   2.6% 332.0KiB        aws_lc_sys aws_lc_0_39_1_aes_gcm_encrypt_avx512
 1.3%   2.6% 332.0KiB        aws_lc_sys aws_lc_0_39_1_aes_gcm_decrypt_avx512
 0.3%   0.7%  84.0KiB              prek prek::languages::<impl prek::config::Language>::run::{{closure}}::{{closure}}
 0.3%   0.7%  82.2KiB              prek prek::languages::<impl prek::config::Language>::run::{{closure}}::{{closure}}
 0.2%   0.5%  61.1KiB             prek? <prek::cli::Command as clap_builder::derive::Subcommand>::augment_subcommands
 0.2%   0.4%  56.8KiB              prek prek::languages::<impl prek::config::Language>::install::{{closure}}
 0.2%   0.4%  53.3KiB annotate_snippets annotate_snippets::renderer::render::render
 0.2%   0.4%  46.8KiB              prek prek::run::{{closure}}
 0.2%   0.3%  41.3KiB              prek prek::cli::run::run::run::{{closure}}
 0.1%   0.3%  33.3KiB             prek? <prek::cli::RunArgs as clap_builder::derive::Args>::augment_args
 0.1%   0.2%  28.0KiB        aws_lc_sys aws_lc_0_39_1_edwards25519_scalarmuldouble_alt
 0.1%   0.2%  28.0KiB             prek? <prek::config::_::<impl serde_core::de::Deserialize for prek::config::Config>::deserialize::__Visitor as serde_core::de::Visitor>::visit_map
 0.1%   0.2%  27.6KiB      serde_saphyr saphyr_parser_bw::scanner::Scanner<T>::fetch_more_tokens
 0.1%   0.2%  27.5KiB        aws_lc_sys aws_lc_0_39_1_edwards25519_scalarmuldouble
 0.1%   0.2%  27.4KiB               std core::ptr::drop_in_place<prek::languages::<impl prek::config::Language>::install::{{closure}}>
 0.1%   0.2%  26.4KiB              prek prek::cli::try_repo::try_repo::{{closure}}
 0.1%   0.2%  22.7KiB              prek prek::hooks::meta_hooks::MetaHooks::run::{{closure}}
 0.1%   0.2%  22.5KiB      serde_saphyr saphyr_parser_bw::scanner::Scanner<T>::fetch_more_tokens
 0.1%   0.2%  22.3KiB         [Unknown] Lp384_montjscalarmul_alt_p384_montjadd
 0.1%   0.2%  21.5KiB      clap_builder clap_builder::parser::parser::Parser::get_matches_with
41.2%  86.3%  10.6MiB                   And 23741 smaller methods. Use -n N to show more.
47.7% 100.0%  12.3MiB                   .text section size, the file size is 25.8MiB

Base Branch Results

 File  .text     Size             Crate Name
 1.2%   2.6% 332.0KiB        aws_lc_sys aws_lc_0_39_1_aes_gcm_encrypt_avx512
 1.2%   2.6% 332.0KiB        aws_lc_sys aws_lc_0_39_1_aes_gcm_decrypt_avx512
 0.3%   0.7%  84.0KiB              prek prek::languages::<impl prek::config::Language>::run::{{closure}}::{{closure}}
 0.3%   0.6%  82.2KiB              prek prek::languages::<impl prek::config::Language>::run::{{closure}}::{{closure}}
 0.2%   0.5%  60.9KiB             prek? <prek::cli::Command as clap_builder::derive::Subcommand>::augment_subcommands
 0.2%   0.4%  56.8KiB              prek prek::languages::<impl prek::config::Language>::install::{{closure}}
 0.2%   0.4%  53.3KiB annotate_snippets annotate_snippets::renderer::render::render
 0.2%   0.4%  46.8KiB              prek prek::run::{{closure}}
 0.2%   0.3%  41.3KiB              prek prek::cli::run::run::run::{{closure}}
 0.1%   0.3%  33.1KiB             prek? <prek::cli::RunArgs as clap_builder::derive::Args>::augment_args
 0.1%   0.2%  28.0KiB        aws_lc_sys aws_lc_0_39_1_edwards25519_scalarmuldouble_alt
 0.1%   0.2%  28.0KiB             prek? <prek::config::_::<impl serde_core::de::Deserialize for prek::config::Config>::deserialize::__Visitor as serde_core::de::Visitor>::visit_map
 0.1%   0.2%  27.6KiB      serde_saphyr saphyr_parser_bw::scanner::Scanner<T>::fetch_more_tokens
 0.1%   0.2%  27.5KiB        aws_lc_sys aws_lc_0_39_1_edwards25519_scalarmuldouble
 0.1%   0.2%  27.4KiB               std core::ptr::drop_in_place<prek::languages::<impl prek::config::Language>::install::{{closure}}>
 0.1%   0.2%  26.4KiB              prek prek::cli::try_repo::try_repo::{{closure}}
 0.1%   0.2%  23.0KiB              prek prek::hooks::meta_hooks::MetaHooks::run::{{closure}}
 0.1%   0.2%  22.5KiB      serde_saphyr saphyr_parser_bw::scanner::Scanner<T>::fetch_more_tokens
 0.1%   0.2%  22.3KiB         [Unknown] Lp384_montjscalarmul_alt_p384_montjadd
 0.1%   0.2%  21.5KiB      clap_builder clap_builder::parser::parser::Parser::get_matches_with
41.5%  86.4%  10.8MiB                   And 23794 smaller methods. Use -n N to show more.
48.0% 100.0%  12.5MiB                   .text section size, the file size is 26.0MiB

@prek-ci-bot
Copy link
Copy Markdown

prek-ci-bot Bot commented Apr 27, 2026

⚡️ Hyperfine Benchmarks

Summary: 0 regressions, 0 improvements above the 10% threshold.

Environment
  • OS: Linux 6.17.0-1010-azure
  • CPU: 4 cores
  • prek version: prek 0.3.11+9 (7efdd91 2026-04-28)
  • Rust version: rustc 1.95.0 (59807616e 2026-04-14)
  • Hyperfine version: hyperfine 1.20.0
CLI Commands

Benchmarking basic commands in the main repo:

prek --version

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base --version 2.2 ± 0.1 2.1 2.4 1.02 ± 0.04
prek-head --version 2.2 ± 0.1 2.1 2.5 1.00

prek list

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base list 8.7 ± 0.1 8.5 9.0 1.00
prek-head list 8.7 ± 0.1 8.5 9.6 1.00 ± 0.02

prek validate-config .pre-commit-config.yaml

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base validate-config .pre-commit-config.yaml 3.1 ± 0.0 3.0 3.1 1.01 ± 0.02
prek-head validate-config .pre-commit-config.yaml 3.0 ± 0.0 2.9 3.2 1.00

prek sample-config

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base sample-config 2.5 ± 0.0 2.4 2.6 1.00
prek-head sample-config 2.5 ± 0.0 2.4 2.6 1.01 ± 0.02
Cold vs Warm Runs

Comparing first run (cold) vs subsequent runs (warm cache):

prek run --all-files (cold - no cache)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run --all-files 142.5 ± 2.3 138.7 147.0 1.01 ± 0.03
prek-head run --all-files 141.4 ± 2.7 137.8 144.8 1.00

prek run --all-files (warm - with cache)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run --all-files 141.4 ± 3.4 135.4 146.8 1.01 ± 0.03
prek-head run --all-files 140.2 ± 2.8 135.6 144.5 1.00
Full Hook Suite

Running the builtin hook suite on the benchmark workspace:

prek run --all-files (full builtin hook suite)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run --all-files 140.0 ± 3.4 133.9 145.9 1.00
prek-head run --all-files 143.1 ± 15.5 135.0 247.5 1.02 ± 0.11
Individual Hook Performance

Benchmarking each hook individually on the test repo:

prek run trailing-whitespace --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run trailing-whitespace --all-files 20.3 ± 0.4 19.7 21.4 1.00
prek-head run trailing-whitespace --all-files 20.6 ± 1.6 19.4 25.7 1.02 ± 0.08

prek run end-of-file-fixer --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run end-of-file-fixer --all-files 26.8 ± 2.1 23.6 31.2 1.00 ± 0.12
prek-head run end-of-file-fixer --all-files 26.7 ± 2.3 23.2 30.7 1.00

prek run check-json --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run check-json --all-files 11.8 ± 0.5 11.3 14.0 1.04 ± 0.06
prek-head run check-json --all-files 11.4 ± 0.4 10.7 12.1 1.00

prek run check-yaml --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run check-yaml --all-files 11.0 ± 0.4 10.7 12.9 1.02 ± 0.04
prek-head run check-yaml --all-files 10.8 ± 0.1 10.7 11.1 1.00

prek run check-toml --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run check-toml --all-files 11.0 ± 0.3 10.5 11.6 1.01 ± 0.03
prek-head run check-toml --all-files 11.0 ± 0.2 10.6 11.5 1.00

prek run check-xml --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run check-xml --all-files 10.9 ± 0.2 10.5 11.2 1.00
prek-head run check-xml --all-files 10.9 ± 0.2 10.4 11.5 1.00 ± 0.03

prek run detect-private-key --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run detect-private-key --all-files 17.2 ± 1.4 15.2 19.8 1.00 ± 0.12
prek-head run detect-private-key --all-files 17.2 ± 1.5 15.0 20.3 1.00

prek run fix-byte-order-marker --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run fix-byte-order-marker --all-files 21.8 ± 1.8 19.4 25.3 1.01 ± 0.12
prek-head run fix-byte-order-marker --all-files 21.5 ± 1.8 19.1 25.5 1.00
Installation Performance

Benchmarking hook installation (fast path hooks skip Python setup):

prek install-hooks (cold - no cache)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base install-hooks 4.5 ± 0.1 4.4 4.6 1.00
prek-head install-hooks 4.5 ± 0.1 4.4 4.6 1.00 ± 0.02

prek install-hooks (warm - with cache)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base install-hooks 4.4 ± 0.1 4.4 4.5 1.00
prek-head install-hooks 4.5 ± 0.0 4.4 4.5 1.01 ± 0.02
File Filtering/Scoping Performance

Testing different file selection modes:

prek run (staged files only)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run 47.9 ± 0.9 46.4 49.5 1.00 ± 0.02
prek-head run 47.9 ± 0.7 46.4 48.8 1.00

prek run --files '*.json' (specific file type)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run --files '*.json' 8.1 ± 0.1 7.9 8.2 1.01 ± 0.01
prek-head run --files '*.json' 8.0 ± 0.1 7.9 8.1 1.00
Workspace Discovery & Initialization

Benchmarking hook discovery and initialization overhead:

prek run --dry-run --all-files (measures init overhead)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run --dry-run --all-files 13.2 ± 0.8 12.4 14.6 1.04 ± 0.06
prek-head run --dry-run --all-files 12.7 ± 0.2 12.4 13.1 1.00
Meta Hooks Performance

Benchmarking meta hooks separately:

prek run check-hooks-apply --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run check-hooks-apply --all-files 13.0 ± 0.1 12.8 13.2 1.03 ± 0.02
prek-head run check-hooks-apply --all-files 12.7 ± 0.1 12.5 13.1 1.00

prek run check-useless-excludes --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run check-useless-excludes --all-files 11.4 ± 0.1 11.2 11.6 1.00
prek-head run check-useless-excludes --all-files 11.4 ± 0.1 11.2 11.5 1.00 ± 0.01

prek run identity --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run identity --all-files 9.9 ± 0.1 9.7 10.0 1.00
prek-head run identity --all-files 9.9 ± 0.1 9.7 10.0 1.00 ± 0.01

Covers the auto-downloaded Ruby + gemspec/additional_dependencies path
with `gem` scrubbed from PATH. Without the fix in 880edab the test
would fail at hook install with `LoadError: ... -- gem`.

Exercises all four call sites touched by the fix: build_gemspec,
resolve_gems, install_single_gem, and (on resolve fallback)
install_gems_sequential.

Made-with: Cursor
Top-level import triggered `unused_imports` on Windows where the only
caller is gated behind `#[cfg(not(target_os = "windows"))]`. Move it
inside the function so the import follows the same gate.

Made-with: Cursor
Copilot AI review requested due to automatic review settings April 27, 2026 18:41
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes Ruby hook install-time failures when prek auto-downloads Ruby (rv-ruby) but the parent process PATH does not include a gem executable, by ensuring the resolved Ruby’s bin/ directory is on PATH for all ruby -S gem ... invocations.

Changes:

  • Add a helper to compute a PATH value with the resolved Ruby’s bin/ prepended, and apply it to all ruby -S gem ... call sites during gemspec build / dependency resolution / gem install.
  • Adjust gem environment setup (gem_env) to set PATH (in addition to GEM_HOME and bundler-related vars) and propagate join errors.
  • Add an integration regression test that scrubs ruby/gem from PATH and verifies hook installation still succeeds when Ruby is auto-downloaded.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
crates/prek/src/languages/ruby/gem.rs Ensures ruby -S gem can locate the bundled gem script by prepending the resolved Ruby bin/ dir to PATH for install-time gem operations.
crates/prek/tests/languages/ruby.rs Adds a CI-only regression test covering auto-downloaded Ruby + gem operations with ruby/gem removed from PATH.

Comment thread crates/prek/tests/languages/ruby.rs Outdated
remove_bin_from_path strips the entire directory that contains the
named binary. On Ubuntu CI that directory is /usr/bin, which also
holds git -- and prek shells out to git, failing with "Failed to find
git: cannot find binary path".

Fix by symlinking git into a fresh stub directory that intentionally
contains no ruby or gem, then prepending that stub to PATH. Verified
locally with CI=1 (rv-ruby auto-downloaded, gemspec built, webrick
installed, hook ran).

Made-with: Cursor
Copilot AI review requested due to automatic review settings April 27, 2026 18:57
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes Ruby hook install-time failures when prek auto-downloads Ruby (rv-ruby) in environments where no system ruby/gem exists on PATH, by ensuring ruby -S gem ... can locate the gem script shipped alongside the resolved Ruby interpreter.

Changes:

  • Prepend the resolved Ruby toolchain’s bin/ directory to PATH for all ruby -S gem ... invocations during gemspec build / gem resolution / gem installs.
  • Refactor gem install environment setup to propagate PATH join errors via Result.
  • Add a CI-only integration regression test that scrubs ruby and gem from PATH while exercising gemspec build + dependency install.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
crates/prek/src/languages/ruby/gem.rs Adds a helper to build a PATH with the resolved Ruby bin/ prepended and applies it to all gem-related ruby -S subprocesses.
crates/prek/tests/languages/ruby.rs Adds an integration regression test covering auto-downloaded Ruby gem operations when ruby/gem are absent from PATH.

@j178 j178 added the bug Something isn't working label Apr 28, 2026
@j178 j178 merged commit 3268a83 into j178:master Apr 28, 2026
34 checks passed
@j178
Copy link
Copy Markdown
Owner

j178 commented Apr 28, 2026

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants