Skip to content

Fix the stale-gem remedy for a copy in another Ruby's tree (#2242) - #2251

Merged
Mikola Lysenko (mikolalysenko) merged 4 commits into
mainfrom
agent/fix-gem-refresh-foreign-ruby-remedy
Nov 25, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 4 commits into
mainfrom
agent/fix-gem-refresh-foreign-ruby-remedy

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 can hold the tree of the earlier Ruby next to the current one. A hosted scan could not rebuild the native gems in the old tree, and told the user to run rm -f <gemspec> <archive> && bundle install. That command does nothing useful there: bundle install installs into the gem home of the Ruby that runs it, so the gem directory stayed in the old tree with no gemspec, vex kept omitting the gem, and every re-scan printed the same rm -f for files that were already gone.

The remedy now removes the gem directory too in the two cases where bundle install would not replace the copy:

  • A Ruby the scan asked builds the gem's extension into another gem home (the issue's case). The command is rm -rf <gem dir> <gemspec> <archive> && bundle install, and the retry named is "Re-run socket-patch scan with the Ruby that installed the gem first on PATH".
  • The gem directory has no gemspec left (what the old remedy left behind) and a refresh of it failed. The command is rm -rf <gem dir> && bundle install, it names only paths that still exist, and no re-run is offered, since a re-run fails the same way.

Both the human warning and the --json detail (redirect_gem_stale_install, upstream_gem_stale_install) follow the same rule, in both directions (scan and unwind). Every other remedy is unchanged. Nothing is deleted by socket-patch itself: this only changes the text of the command it prints.

Root cause

crates/socket-patch-cli/src/commands/gem_refresh.rs built one remedy for every failed refresh, on the assumption that bundle install writes into the gem home that holds the stale copy.

What changed

  • gem_refresh.rs (CLI): outlives_reinstall decides whether a failed copy needs its gem directory removed, remedy_paths builds the list, and retry_hint picks a retry that can work.
  • A copy left alone because it resolves outside the project directory now has its own reason (Why::OutOfTree) instead of sharing Why::Failed, so "a refresh was tried and failed" is a fact the code can check. Its messages are the same as before.
  • gem_ext.rs (core): the "(another Ruby's)" ending of the owning-Ruby error is a public constant, so the CLI and core cannot drift apart on it.
  • docs/ecosystems.md and CLI_CONTRACT.md describe the new remedy.

Evidence

Test Covers Before the fix After
a_copy_no_ruby_owns_is_removed_with_its_gem_dir (unit) a native gem in another Ruby's tree, scan and unwind, human and JSON fails passes
a_gem_dir_without_its_gemspec_is_the_thing_to_remove (unit) the leftover directory; a default gem's gemspec still counts as one fails passes
a_gem_dir_left_without_its_gemspec_is_named_as_the_thing_to_remove (e2e, real binary) the leftover directory in ruby/3.2.0 beside a patched ruby/3.3.0; after the printed rm -rf a re-scan has nothing to warn about fails with the issue's message passes

Commands run locally:

  • cargo clippy --locked --workspace --all-features -- -D warnings: clean.
  • cargo test -p socket-patch-cli --lib: 1069 passed.
  • cargo test -p socket-patch-core --lib gem_ext: 75 passed.
  • cargo test -p socket-patch-cli --test e2e_redirect_gem_stale_install --test e2e_gem_native_ext_build --test e2e_vex_redirect: 67, 43 and 35 passed (11 ignored host capstones in the native build suite).

cargo clippy --all-targets reports 68 errors in test files this PR does not touch; they are the same on main and CI's clippy job does not build those targets.

There is no end-to-end test with two real Rubies: no CI leg installs two. The issue's exact case is covered by the unit test, which feeds the report the reason string the issue quotes.

Code review

/code-review high found five things. Three are fixed in the last commit:

  • A copy outside the project directory with no gemspec took the new remedy although no refresh was tried, which broke an existing e2e test. Fixed with Why::OutOfTree.
  • A Ruby probe that timed out was treated as "another Ruby owns this tree". Only a candidate that answered and builds elsewhere counts now.
  • The default-gem half of the gemspec check treated any stat error as "gone". Only not-found does now.

One was a docs sentence that stated a condition the code does not have; reworded.

One is not fixed, and is why this is Refs and not Fixes: a copy in another Ruby's tree still gets the old rm -f remedy when the refresh stops before the Ruby probe (--no-gem-install-refresh, a download or offline failure, --no-gem-native-rebuild). Fixing that needs the scan to know which Ruby will run bundle install without probing one, which is a larger change. Once such a run leaves a directory without a gemspec, the next scan that tries a refresh names the directory.

Left for a follow-up

  • The case above.
  • The issue also asks vex to name the path it objects to in its "omitting … (not_applied)" warning. That message is shared by every ecosystem and is not changed here.

https://claude.ai/code/session_011f9aeQSmD8W1tSmEKfea3n


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_011f9aeQSmD8W1tSmEKfea3n
Two failing tests for #2242: the remedy of a native gem in another
Ruby's tree, and of a gem directory whose gemspec is already gone.

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_011f9aeQSmD8W1tSmEKfea3n
The hosted scan told the user to rm -f the gemspec and archive of a
stale gem and run bundle install. Bundler installs into the gem home
of the Ruby that runs it, so for a native gem in the tree of an
earlier Ruby the gem directory stayed behind with no gemspec, vex
kept omitting the gem, and a re-scan offered the same rm -f for
files that were gone.

Where a native rebuild found no Ruby owning the install, or the gem
directory has no gemspec left, the remedy now removes the gem
directory too and names only what is on disk. The retry it names is
one that can work.

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_011f9aeQSmD8W1tSmEKfea3n
Review findings on the remedy for #2242:

- A copy left alone because it resolves outside the project is its
  own reason, so it keeps the remedy it had.
- A Ruby that could not be asked is not a Ruby that owns another
  tree: only a candidate that builds the extension elsewhere counts.
- Only not-found says a default gem's gemspec is gone.
- The docs no longer state a condition the code does not have.

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

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
User-facing warning/remedy text and classification logic in gem refresh reporting; no automatic deletion or changes to refresh execution paths beyond messaging.

Overview
After a Ruby upgrade, stale gems in an older vendor/bundle/ruby/<version> tree could not be refreshed, and the printed fix (rm -f on gemspec + archive, then bundle install) left an orphan gem directory because Bundler installs into the current Ruby's home only.

Stale-gem warnings and remedies now detect when bundle install will not replace the copy (outlives_reinstall: foreign Ruby's tree via native rebuild errors mentioning OWNED_BY_ANOTHER_RUBY, or missing gemspec including default gems). They use rm -rf on the gem directory when needed, list only paths that still exist, and adjust retry hints (use the installing Ruby on PATH, or no re-run when a gemspec-less dir cannot be refreshed).

Why::OutOfTree separates "left alone outside the project" from a failed refresh so out-of-tree copies do not get the gem-dir remedy incorrectly.

Docs (CLI_CONTRACT.md, ecosystems.md) match the new behavior; unit and e2e tests cover the foreign-Ruby and no-gemspec cases.

Reviewed by Cursor Bugbot for commit 5da3cb0. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does
After a Ruby upgrade, a stale native gem in the old Ruby's vendor/bundle/ruby/<old> tree got a remedy (rm -f <gemspec> <archive> && bundle install) that left the gem directory behind for good. Now the printed remedy also removes the gem directory (rm -rf) in two cases: when the Ruby the scan asked builds into another gem home, and when the gemspec is already gone. In those cases it names only paths that still exist and gives a retry hint that can work. The change is only in the printed text and JSON details. socket-patch itself deletes nothing. It is Refs #2242, not Fixes: when the refresh stops before the Ruby probe, the old remedy is still printed. The description explains why.

Risk: low. It changes only messages and remedy text. Why::OutOfTree is split from Why::Failed so an out-of-tree copy keeps its old wording and hint, and every matches! that named Failed now names OutOfTree too.

Look here

  • gem_refresh.rs:392: gemspec_is_gone. Only NotFound counts, and a default gem's specifications/default/ entry counts as present
  • gem_refresh.rs:424: outlives_reinstall. Only Why::Failed, never in-place or left-alone copies
  • gem_refresh.rs:443: remedy_paths (the gem dir first, then the files that still exist)
  • gem_refresh.rs:934: a group's special hint is used only when every gem has the same reason
  • gem_ext.rs:2408: the shared OWNED_BY_ANOTHER_RUBY marker, so core and CLI can't drift apart

Verified

  • Read the whole diff against main.
  • Paths in the printed rm -rf go through the existing shell_word quoting.
  • A shared gem home still gets rm -f in the summary (reinstall_command(.., false)).
  • The new unit tests and the e2e test check both directions (scan and unwind) in human and JSON output.
  • CHANGELOG.md is untouched.
  • CI on 5da3cb0: ci-ok and clippy are green, mergeable, no open review threads, and the Bugbot pass has no findings.

Changes I made: none.

Open questions: none. The leftover case (the refresh stops before the Ruby probe) is already called out for 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 Nov 25, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[agent] Reviewed the Why::OutOfTree split, outlives_reinstall and gemspec_is_gone. Only the printed remedy changes; shared, disabled and out-of-tree copies keep the old one. No findings.

Merged via the queue into main with commit c40cc39 Nov 25, 2026
127 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the agent/fix-gem-refresh-foreign-ruby-remedy branch November 25, 2026 20:36
Mikola Lysenko (mikolalysenko) pushed a commit that referenced this pull request Nov 25, 2026
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
Mikola Lysenko (mikolalysenko) pushed a commit that referenced this pull request Nov 25, 2026
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
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.

4 participants