ruby: put resolved Ruby's bin dir on $PATH for gem invocations#2021
ruby: put resolved Ruby's bin dir on $PATH for gem invocations#2021j178 merged 6 commits intoj178:masterfrom
$PATH for gem invocations#2021Conversation
`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
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
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_envhelper to prepend the resolved Ruby toolchain’sbin/directory toPATH. - Apply the computed
PATHto allruby -S gem ...invocation sites (includingbuild_gemspecand the sharedgem_envhelper).
📦 Cargo Bloat ComparisonBinary size change: -0.77% (26.0 MiB → 25.8 MiB) Expand for cargo-bloat outputHead Branch ResultsBase Branch Results |
⚡️ Hyperfine BenchmarksSummary: 0 regressions, 0 improvements above the 10% threshold. Environment
CLI CommandsBenchmarking basic commands in the main repo:
|
| 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
There was a problem hiding this comment.
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
PATHvalue with the resolved Ruby’sbin/prepended, and apply it to allruby -S gem ...call sites during gemspec build / dependency resolution / gem install. - Adjust gem environment setup (
gem_env) to setPATH(in addition toGEM_HOMEand bundler-related vars) and propagate join errors. - Add an integration regression test that scrubs
ruby/gemfromPATHand 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. |
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
There was a problem hiding this comment.
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 toPATHfor allruby -S gem ...invocations during gemspec build / gem resolution / gem installs. - Refactor gem install environment setup to propagate
PATHjoin errors viaResult. - Add a CI-only integration regression test that scrubs
rubyandgemfromPATHwhile 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. |
|
Thanks! |
Summary
build_gemspec,resolve_gems,install_single_gem, andinstall_gems_sequentialincrates/prek/src/languages/ruby/gem.rsall spawn<ruby> -S gem .... Ruby's-Sflag searches$PATHfor the named script.The rv-ruby tarballs that prek auto-downloads ship
gemnext torubyin the samebin/directory, but prek wasn't putting that directory on$PATHfor these child commands. In environments without a system Ruby onPATH(e.g. a vanilla Docker image / CI container), the-Slookup fails and Ruby aborts with:The first place this trips is
build_gemspec(called fromRuby::installbeforeinstall_gems), so the user-visible failure isFailed to install hook ...: Failed to build gemspecs. Once that's worked around,gem install --explainwould fail identically — same root cause for all four call sites.This is distinct from #2006 / #2017, which fixed missing
gems/bin/rubyshim used at hook run time. The bug here is at hook install time and is not mitigated by 0.3.11 (gem.rsis 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
$PATHfor everyruby -S geminvocation:ruby_path_env(&RubyResult) -> Result<OsString>helper that usesprek_consts::prepend_paths(same helper used byRuby::run).gem_envnow takes&RubyResultin addition togem_homeand setsPATHalongside the existingGEM_HOME/BUNDLE_*/MAKEFLAGSenv. ReturnsResultso theprepend_pathsjoin error can propagate (vanishingly rare in practice).build_gemspecdoesn't have aGEM_HOMEto set so it doesn't usegem_env, but it setsPATHdirectly via the same helper.Test plan
cargo fmt --check -p prek— cleancargo clippy -p prek --all-targets -- -D warnings— cleancargo test -p prek --bin prek— 400 passed; 0 failed (existing unit tests, including all fivelanguages::ruby::gem::tests::*)languages::ruby::auto_download_gem_install_without_gem_on_path(CI-only, mirrors the existingauto_downloadgating). It:language_version: '3.2.9'to force prek to auto-download Ruby via rv-ruby (and asserts atools/ruby/3.2.9*directory exists after the run);.gemspecinto the local repo sobuild_gemspecruns;additional_dependencies: ["webrick"]soresolve_gems(gem install --explain) andinstall_single_gem(gem install) run;rubyandgemfrom$PATHvia the existingremove_bin_from_pathhelper, soruby -S gem ...only resolves if the resolved Ruby's bin dir is prepended (i.e. the test would have failed before this fix with theLoadErrorquoted above).The pre-existing
auto_downloadtest only ranruby --versionand never hitgem;gemspec_workflowusedlanguage_version: systemand so pickedgemup from the systemPATH. The new test combines auto-download + gem operations + scrubbedPATHto lock the regression in.Manual verification of the failure mode (without this fix)
In a container with no system Ruby:
With this patch, prek prepends
<ruby_prefix>/binto$PATH,ruby -S gemresolves, and the install proceeds.