Skip to content

Fix the stale-gem remedy beside another Ruby tree (#2242) - #2258

Open
Mikola Lysenko (mikolalysenko) wants to merge 4 commits into
mainfrom
agent/fix-gem-remedy-sibling-ruby-tree
Open

Mikola Lysenko (mikolalysenko) wants to merge 4 commits into
mainfrom
agent/fix-gem-remedy-sibling-ruby-tree

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

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

Refs #2242

Summary

After a Ruby upgrade, vendor/bundle often holds two trees (ruby/3.2.0 beside ruby/3.3.0). For a stale gem in the old tree, the hosted scan's remedy rm -f <gemspec> <archive> && bundle install does not work: Bundler installs into the tree of the Ruby that runs it, so the unpatched gem directory stays behind with no gemspec. #2251 fixed this where the native rebuild's Ruby probe showed the copy belongs to another Ruby. This PR covers the first part #2251 left: a refresh that is turned off or fails without that evidence (--no-gem-install-refresh, --no-gem-native-rebuild, a patched gem that could not be downloaded, --offline).

Where the stale copy's gem home is named by a Ruby ABI version and has another such gem home beside it, the remedy now removes the gem directory too:

- Not refreshed (`--no-gem-install-refresh` is set): stale-probe-gem 1.0.0. Re-run without `--no-gem-install-refresh`, or run `rm -rf vendor/bundle/ruby/3.2.0/gems/stale-probe-gem-1.0.0 vendor/bundle/ruby/3.2.0/specifications/stale-probe-gem-1.0.0.gemspec vendor/bundle/ruby/3.2.0/cache/stale-probe-gem-1.0.0.gem && bundle install`. The gem directory is removed too: `bundle install` replaces a copy only in the gem home of the Ruby that runs it, and more than one Ruby has a gem home here.

That command is right whichever Ruby runs bundle install: the owner reinstalls the gem, any other Ruby leaves a tree with no copy. Only the text of the warning changes. The warning code, exit codes and JSON shape are as they were, and the scan itself deletes nothing new.

Root cause

outlives_reinstall in commands/gem_refresh.rs decided that the remedy must name the gem directory only from a failed refresh's reason (the probe's "another Ruby's" line, or a missing gemspec). A refresh that stopped earlier carried no such reason, so the remedy fell back to rm -f.

What changed

  • beside_another_gem_home: true when the copy's gem home is <dir>/<abi> (3.2.0, 3.4.0+1) and <dir> holds another ABI-named directory with gems/ and specifications/. Two gem homes side by side under other names are not counted.
  • outlives_reinstall gains Outlives::BesideAnotherRuby for Why::Failed and Why::Disabled, after the two existing reasons. The re-run hint stays the one the reason already had.
  • A default gem (its gemspec is under specifications/default/) keeps the rm -f remedy: nothing would reinstall its gem directory. A copy patched in place, a shared home left alone and a path outside the project are unchanged.
  • docs/ecosystems.md and CLI_CONTRACT.md describe the rule.

Evidence

  • Unit test a_copy_beside_another_rubys_tree_is_removed_with_its_gem_dir (commands/gem_refresh.rs): the three reasons, both directions, the lone tree, a non-gem-home sibling, a non-ABI sibling gem home, a default gem and an in-place copy. Failed before the fix with the rm -f bullet.
  • End-to-end test an_unrefreshed_copy_beside_another_rubys_tree_is_removed_with_its_gem_dir (e2e_redirect_gem_stale_install): runs the CLI with --no-gem-install-refresh over two trees, checks the human and --json remedy, follows it, and re-scans with no warning. Failed before the fix.
  • cargo test -p socket-patch-cli --lib: 1076 passed.
  • cargo test -p socket-patch-cli --test e2e_redirect_gem_stale_install: 68 passed. --test e2e_redirect_gem_build (Ruby 3.x, Bundler 4.0.18): 43 passed.
  • cargo clippy --locked -p socket-patch-cli --all-features -- -D warnings: clean. With --all-targets, clippy reports errors in test helper modules this PR does not touch (tests/prebuilt_common, duplicate mod loads), the same as on main; none is in a changed file.

Review findings not fixed here

/code-review high found one defect this PR introduced (the default gem case), now fixed with a test. It also listed gaps that were there before and that I left out to keep the change small:

  • A lone old tree before the first bundle install under the new Ruby still gets rm -f. The follow-up scan then names the gem directory (Fix the stale-gem remedy for a copy in another Ruby's tree (#2242) #2251's missing-gemspec remedy).
  • A sibling tree of another engine (vendor/bundle/jruby/... beside ruby/...) is not counted.
  • Copies left for being outside the project or unverified, and the shared-home command of an unwind, keep their remedy.
  • With --no-gem-install-refresh and a gemspec that is already gone, the warning still offers a re-run first.

Still open on #2242

vex does not name the path behind a not_applied omission. That message is shared by every ecosystem.

https://claude.ai/code/session_011f9aeSZGRcxA2epZBuB4RJ


Generated by Claude Code

Refs #2242.

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_011f9aeSZGRcxA2epZBuB4RJ
Two failing tests for the part of #2242 left by #2251: a stale copy
in a gem home beside another Ruby's, left before any Ruby was asked
which tree it installs into, still gets the rm -f remedy.

Refs #2242.

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_011f9aeSZGRcxA2epZBuB4RJ
The hosted scan's remedy for a stale installed gem is rm -f of its
gemspec and archive, then bundle install. Bundler installs into the
gem home of the Ruby that runs it, so a copy in the tree of another
Ruby keeps its gem dir, with no gemspec. #2251 removed the gem dir
where the native rebuild's Ruby probe showed whose the copy is. A
refresh that stops before the probe (--no-gem-install-refresh,
--no-gem-native-rebuild, a gem that could not be had) still printed
the old remedy.

Where such a copy's gem home is named by a Ruby ABI and has another
one beside it, the remedy now removes the gem dir too. That is right
whichever Ruby runs bundle install. A lone tree keeps rm -f.

Refs #2242.

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_011f9aeSZGRcxA2epZBuB4RJ
A default gem's gemspec stays under specifications/default/, so a
bundle install would not put its gem dir back: the remedy for a copy
beside another Ruby's tree leaves it. The docs and the contract now
say the rule covers any refresh that was turned off or failed.

Refs #2242.

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_011f9aeSZGRcxA2epZBuB4RJ
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 25, 2026 21:47
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
User-facing warning text and manual cleanup commands only; no change to refresh behavior, exit codes, or JSON warning codes.

Overview
Extends the stale gem install remedy when refresh is skipped or fails without evidence from the native Ruby probe (e.g. --no-gem-install-refresh, offline, --no-gem-native-rebuild). If the stale copy lives under an ABI-named gem home that has another Ruby gem home beside it (vendor/bundle/ruby/3.2.0 next to ruby/3.3.0), warnings now recommend rm -rf including the gem directory—not only gemspec and archive—because bundle install only repopulates the running Ruby’s tree.

Adds beside_another_gem_home detection and Outlives::BesideAnotherRuby so human and JSON warnings pick the right explanation text. Default gems and in-place patches stay on the lighter rm -f remedy. CLI_CONTRACT.md and docs/ecosystems.md document the rule; new unit and e2e tests cover #2242.

Reviewed by Cursor Bugbot for commit 69e43ed. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief

What it does
Sometimes a gem refresh doesn't run (--no-gem-install-refresh, --offline, a failed download, or native rebuild turned off). When that leaves a stale gem copy whose gem home sits beside another Ruby ABI's gem home (vendor/bundle/ruby/3.2.0 next to ruby/3.3.0), the remedy the scan prints now also removes the gem directory (rm -rf). The old remedy was rm -f on the gemspec and archive, which left the unpatched directory behind. Only the warning text changes: codes, exit status and the JSON shape stay the same, and the scan itself deletes nothing new.

Risk
Low. The change only affects advice text for a narrow layout. The rm -rf targets are the gem's own directory, gemspec and archive inside the project, and that command is correct whichever Ruby later runs bundle install. Default gems, copies patched in place, shared homes and paths outside the project keep their old remedy.

Look here

  • gem_refresh.rs:433: beside_another_gem_home, the ABI-name test and the check for a sibling gem home.
  • gem_refresh.rs:479: outlives_reinstall. The new arm runs after the two existing reasons and excludes default gems.
  • gem_refresh.rs:600 and :1022: which explanation goes after a remedy that removes a gem directory.
  • gem_refresh.rs:2601 and [e2e_redirect_gem_stale_install.rs:2398](https://github.com/SocketDev/socket-patch/blob/69e43edfe705aa5b933e9a413a785a344233e0cd/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs#L2398): the tests. They cover three reasons, both directions, a lone tree, sibling directories that aren't gem homes, a default gem and a copy patched in place.

Verified

Changes I made
None.

Open questions
None blocking. The description lists the gaps that predate this PR and were left out on purpose: a lone old tree, a JRuby sibling tree, and the shared-home unwind.


Generated by Claude Code

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

Development

Successfully merging this pull request may close these issues.

2 participants