Skip to content

Fix agent mode reading gemrc gem directories Ruby does not load (#2317) - #2459

Open
Mikola Lysenko (mikolalysenko) wants to merge 4 commits into
mainfrom
agent/fix-gem-env-gemrc-dirs
Open

Mikola Lysenko (mikolalysenko) wants to merge 4 commits into
mainfrom
agent/fix-gem-env-gemrc-dirs

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

LLM Description written by Claude Code:claude-fable-5-2

Fixes #2317

Summary

With gemhome or gempath in a gemrc (~/.gemrc, the system gemrc, a GEMRC file), agent mode read gem directories the application does not load from and skipped the ones it does. An installed gem was reported as not installed (apply and apply --check exited 0 without patching), and a copy of the same version in the gemrc's directory was patched, checked and attested by vex while the application kept loading unpatched code. scan -g found no gems.

The fix is one line of behavior: the two questions are now asked as gem env gemdir --norc and gem env gempath --norc, which answer with Ruby's own Gem.dir and Gem.path. GEM_HOME and GEM_PATH count as before. A project with no such gemrc gets the same answer as on main.

Root cause

run_gem_env (core crawlers/ruby_crawler.rs) ran gem env <key>. The gem command applies the gemrc keys gemhome and gempath to its own process; require, bundle install and bundle exec never read them. Every caller shares that one probe (the project fallback, the global paths, the hosted stale-install probe, the rollback lookup), so the fix is at the probe.

Checked with the real tools (Ruby 3.3.6, RubyGems 3.5.22), with gemhome: <dir> in a GEMRC file:

Question Answer
gem env gemdir / gempath the gemrc's directory
gem env gemdir --norc / gempath --norc the default and user directories (and GEM_HOME when set)
ruby -e 'puts Gem.dir, Gem.path' the same as --norc

Do-no-harm notes for the reviewer

  • A gem that gives no answer with --norc (one too old to know the flag, or a wrapper that refuses it) is asked the plain question again, so such a machine behaves as on main and does not lose its gem directories.
  • The fake gem scripts of the existing suites match on the first two arguments, so they answer the new question unchanged.
  • No write path changes. The run reads other directories only on a machine whose gemrc sets one of the two keys.

Evidence

Red to green (commit fc201bc adds the first three tests before the fix; each fails on main):

  • Core, crawler_ruby_e2e::get_gem_paths_reads_rubys_gem_dirs_not_the_gemrcs: a gem that answers as the real one does under a gemrc; the project paths and the --global paths are Ruby's, and only the copies Ruby loads are found (states 1, 2 and 4 of the issue).
  • CLI, apply suite, a_gemrc_gemhome_does_not_hide_the_copy_ruby_loads and a_copy_in_the_gemrc_gemhome_is_not_patched_in_place_of_rubys: apply --check exits 1 over the unpatched copy Ruby loads, apply patches it and leaves the gemrc's copy alone, and a patched copy in the gemrc's directory does not make the check pass (states 1 to 3).
  • CLI with the real gem and ruby, e2e_gem_native_ext_build::a_gemrc_gemhome_is_not_read_in_place_of_rubys_gem_dirs (--ignored, the pinned-Ruby legs): a GEMRC file and GEM_HOME; it first asserts the premise from the tools themselves, then the same outcome.
  • Core, get_gem_paths_asks_a_gem_without_norc_the_plain_question: fails on the commit before the fallback.

Run locally on this branch (Ruby 3.3.6, Bundler 4.0.18):

Command Result
cargo test -p socket-patch-core --test crawler_ruby_e2e 32 passed
cargo test -p socket-patch-core --lib ruby 155 passed
cargo test -p socket-patch-cli --test apply --test scan --test get --test e2e_redirect_gem_stale_install --test in_process_gem_apply --test in_process_gem_multi_platform all passed
cargo test -p socket-patch-cli --test e2e_redirect_gem_build --test e2e_vendor_gem_build --test e2e_gem_native_ext_build -- --include-ignored 57, 22 and 55 passed
cargo clippy --locked --workspace --all-features -- -D warnings (the CI command) clean

cargo clippy --all-targets reports errors in test code on main as well (none in the lines this PR adds); CI does not run it that way.

Review

/code-review high ran on the diff. Fixed: a gem that rejects --norc left a system-gems project with no gem directory (the fallback above, with its test).

Not fixed, with reasons:

  • A copy an earlier release patched in the gemrc's directory is not restored by rollback or remove. Those commands look where apply looks, which no longer includes that directory. The copy is one Ruby does not load, it keeps the patched bytes, and gem pristine <gem> puts the upstream ones back. Reading the gemrc's directories as a class of unloaded copies (found by rollback, never written or attested) is the change the issue leaves to a maintainer: "they can be added after Ruby's own, never in place of them". It touches the copy classes of apply, vex and rollback, so it is a follow-up and not part of this fix.
  • --global no longer lists gems that only the gemrc's directory holds. A Ruby program cannot load them unless GEM_HOME or GEM_PATH names that directory too, in which case it is read. Same maintainer question as above.

CI

ci-ok and clippy are green on 4b2e385. Cursor Bugbot reviewed that commit and reported no findings.

The nokogiri compatibility workflow (not required for merge) was started twice for this head, and the second start cancelled seven jobs of the first. I re-ran them once: six passed. jruby-9.4 / nokogiri 1.18.10 java / ubuntu-latest is red because its runner "lost communication with the server" (no log kept). Its first attempt had finished with PASS: 49 checks one second before it was cancelled; see the comment on this PR.

Docs

CLI_CONTRACT.md, gem discovery: how the question is asked and why a directory only a gemrc names is not read.

Not checked

The system gemrc, Windows, JRuby and TruffleRuby (--norc skips every gemrc through one code path in RubyGems; read, not run).

https://claude.ai/code/session_011f9aeNtgNFdDtdR3SwZfjU


Generated by Claude Code

Refs #2317.

Assisted-by: Claude Code:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9aeNtgNFdDtdR3SwZfjU
The gem command applies the gemrc keys gemhome and gempath to its
own directories. Ruby and Bundler never read them, so under such a
gemrc `gem env` names a directory the project loads nothing from.

Three tests that fail on main:

- core, crawler_ruby_e2e: with a gem that answers as the real one
  does under a gemrc, the project and the global paths are Ruby's
  Gem.dir and Gem.path.
- CLI, apply suite: apply and apply --check act on the copy Ruby
  loads, and leave a copy in the gemrc's directory alone.
- CLI, e2e_gem_native_ext_build: the same with the real gem and
  ruby, a GEMRC file and GEM_HOME.

Refs #2317.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9aeNtgNFdDtdR3SwZfjU
Assisted-by: Claude Code:claude-fable-5-2
A Bundler project with no bundle path of its own, and a --global
run, find the gem directories with `gem env gemdir` and `gem env
gempath`. The gem command applies the gemrc keys gemhome and gempath
to its own directories. Ruby and Bundler never read them: require,
bundle install and bundle exec load from Gem.dir and Gem.path as the
environment gives them.

With either key in a gemrc, the run read a directory the project
loads nothing from and skipped the ones it does. An installed gem
was reported as not installed, and a copy of the same version in the
gemrc's directory was patched, checked and attested in place of the
unpatched one the application loads.

Both questions now pass --norc, which answers with Gem.dir and
Gem.path. GEM_HOME and GEM_PATH are the environment's and count as
before. Every caller shares the one probe: the project fallback,
the global paths, the hosted stale-install probe and the rollback
lookup.

Fixes #2317.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9aeNtgNFdDtdR3SwZfjU
Assisted-by: Claude Code:claude-fable-5-2
A gem command that gives no answer to `gem env <key> --norc` (one
too old to know the flag, or a wrapper that refuses it) left a
system-gems project with no gem directory at all: every gem read as
not installed, and apply --check passed over unpatched code.

Such a gem is now asked again without the flag, which is the
question every earlier release asked. Its answer is right wherever
no gemrc sets gemhome or gempath.

Found by the review of this branch. The new test fails on the
previous commit.

Refs #2317.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9aeNtgNFdDtdR3SwZfjU
Assisted-by: Claude Code:claude-fable-5-2
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review December 2, 2026 17:54
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes how all gem path discovery runs, which directly drives apply, check, scan, and rollback lookups; behavior is backward-compatible via --norc fallback but affects any host with gemrc gemhome/gempath.

Overview
Gem discovery and apply/check were treating gem env gemdir / gempath as where a project loads gems, but those answers follow gemrc gemhome/gempath, which Ruby and Bundler do not use. The crawler now probes gem env <key> --norc so paths match Ruby’s Gem.dir / Gem.path, with a fallback to the plain gem env call when --norc is unsupported.

CLI contract documents the new probing behavior (#2317). Tests cover gemrc fixtures (in-process apply, core crawler path resolution, ignored host e2e with real gem/ruby) so patches and drift checks hit the copy Ruby actually loads and leave the gemrc-only directory alone.

Reviewed by Cursor Bugbot for commit 4b2e385. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] The jruby-9.4 / nokogiri 1.18.10 java / ubuntu-latest check is red on 4b2e385, and it is not a result of this change:

  • Its annotation is "The self-hosted runner lost communication with the server", and the job kept no log.
  • That was the one re-run of the job. The first attempt was cancelled by a second start of the workflow for this head ("a higher priority waiting request"), one second after its script had finished: its log ends with PASS: 49 checks on nokogiri 1.18.10 (java), jruby 3.1.7 on java.
  • The other JRuby legs passed on this head (9.4 hosted and vendored, 10.1), and the workflow is not required for merge.

The other legs that were cancelled the same way were re-run with it. I have not re-run this job a second time.


Generated by Claude Code

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does
Agent mode finds a Bundler project's fallback gem directories (and the -g ones) by asking gem env gemdir / gem env gempath. Those answers follow the gemrc keys gemhome/gempath, which Ruby and Bundler never read, so a machine with either key set had installed gems reported as "not installed" or an unused copy patched and attested (#2317). The probe now passes --norc, which returns Ruby's own Gem.dir/Gem.path, and falls back to the plain question if a gem gives no answer with the flag.

Risk: medium. The one-line probe change feeds every gem caller (project fallback, --global, stale-install probe, rollback lookup). On a machine without those gemrc keys the answer is unchanged, and an old or wrapped gem that rejects --norc gets the pre-change question, so the downside is bounded. Known gap, stated in the description: a copy an earlier release patched in a gemrc-only directory is no longer found by rollback/remove (Ruby does not load it; gem pristine restores it).

Look here

  • ruby_crawler.rs:1081: the probe and its --norc → plain fallback
  • crawler_ruby_e2e.rs:1423 and :1501: gemrc case and the no---norc fallback
  • [in_process_gem_fallback_home.rs:1188](https://github.com/SocketDev/socket-patch/blob/4b2e385b17e71afa6675a529671a2cbd2f59b6a7/crates/socket-patch-cli/tests/apply/in_process_gem_fallback_home.rs#L1188): apply --check / apply behavior with a gemrc gemhome
  • [e2e_gem_native_ext_build.rs:5495](https://github.com/SocketDev/socket-patch/blob/4b2e385b17e71afa6675a529671a2cbd2f59b6a7/crates/socket-patch-cli/tests/e2e_gem_native_ext_build.rs#L5495): real-Ruby test, asserts the premise from the tools first
  • CLI_CONTRACT.md:1712: contract wording

Verified

  • Read the full diff and With gemhome or gempath in a gemrc, agent mode reads gem directories Ruby does not load: an installed gem is reported as not installed, or an unused copy as patched #2317. --norc is a RubyGems common option; locally (RubyGems 3.5.22) gem env gemdir --norc answers as expected. runner.run returns None on a non-zero exit, so a rejecting gem reaches the fallback.
  • cargo test -p socket-patch-core --test crawler_ruby_e2e: 32 passed. With ruby_crawler.rs reverted to main, get_gem_paths_reads_rubys_gem_dirs_not_the_gemrcs fails, so the test pins the fix.
  • No CHANGELOG.md change, no debug leftovers, no write-path change.
  • CI on 4b2e385: ci-ok and clippy green. One non-required leg, jruby-9.4 / nokogiri 1.18.10 java / ubuntu-latest, is red because a self-hosted runner lost contact (no log). Its earlier attempt had printed PASS before being cancelled, and it has already been re-run once (see the comment above).

Changes I made: none.

Open questions: whether gemrc-only directories should later be read as a "found but never written" copy class for rollback (the description leaves that to a maintainer as a follow-up).


Generated by Claude Code

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review

Projects

None yet

2 participants