Skip to content

Fix cleanup of a vendored gem the project dropped (#2325) - #2418

Merged
Mikola Lysenko (mikolalysenko) merged 5 commits into
mainfrom
agent/fix-gem-revert-removed-dependency
Dec 1, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 5 commits into
mainfrom
agent/fix-gem-revert-removed-dependency

Conversation

@mikolalysenko

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

Copy link
Copy Markdown
Collaborator

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

Fixes #2325

Summary

After bundle remove <gem>, or after the gem is moved to another release,
a gem that vendored mode patched could never be cleaned up. rollback,
remove, vendor --revert and scan --mode vendored --prune all kept
its .socket/vendor/gem/<uuid>/ directory and ledger record as "drifted",
rollback and remove exited 1, and vendor --check stayed red, so a
CI job gating on it failed on every run.

Those commands now clean the entry up. The user's Gemfile and
Gemfile.lock are not written.

Root cause

The gem revert reported every wiring record it could not restore as drift
(vendor_lock_entry_drifted) and kept the copy and the ledger record. It
had no case for a record whose fragment is gone because the project dropped
the gem. Nothing can undo that "drift", so the keep was permanent. The npm
family, uv and Pipenv already report this case as
vendor_lock_entry_removed and clean up.

The change

In revert_gem_opts (core, vendor/gem.rs), before any record is
reverted, the revert reads whether Gemfile or Gemfile.lock still
names a vendored copy of the gem, in any path spelling and any case: the
entry's own .socket/vendor/gem/<uuid> directory, or a copy of the same
gem and version under another patch uuid.

  • Neither file names one: a record that cannot be restored warns
    vendor_lock_entry_removed. Neither file is written, and the copy and
    the ledger record are removed.
  • Either file names one: unchanged. The record is drift, both files and the
    copy are kept. This still covers an edited path: line, a PATH section
    left in the lock, and a commented-out reference.
  • A dry run and --preserve-state report the same and delete nothing.

A false "still referenced" only keeps a copy, as before. CLI_CONTRACT.md
documents the gem rule beside the other backends' rules.

Evidence

Unit tests in crates/socket-patch-core/src/vendor/gem.rs:

Test Covers
revert_of_a_gem_the_project_dropped_removes_the_copy bundle remove and the upgrade variant
revert_of_a_dropped_gem_with_checksums_removes_the_copy a lock with a CHECKSUMS section
revert_of_a_dropped_gem_keeps_the_copy_on_a_dry_run_and_under_keep_artifact dry run, --preserve-state
revert_of_a_dropped_gem_is_drift_while_a_file_names_the_copy guard: a comment, a backslashed upper-case path, another gem's PATH section
revert_keeps_an_entry_whose_gem_is_wired_to_another_uuid guard: the same gem wired under another patch uuid

End to end with real Bundler,
gem_vendor_dropped_gem_is_cleaned_up_by_every_undo_command in
crates/socket-patch-cli/tests/e2e_vendor_gem_build.rs: vendor rack, drop
it with bundle remove (or pin another release), then run
vendor --revert, rollback or remove. It asserts exit 0, no drift
report, the copy and ledger record gone, both files byte-identical, a frozen
bundle install passing, and vendor --check no longer reporting the
entry.

Red before the fix (commit 0d27b2b holds the tests alone):

cargo test -p socket-patch-core --lib vendor::gem::tests::revert_of_a
test result: FAILED. 1 passed; 3 failed      # the pass is the still-drift guard
cargo test -p socket-patch-cli --test e2e_vendor_gem_build -- --ignored gem_vendor_dropped_gem
test result: FAILED. 0 passed; 1 failed      # "a dropped gem is not reported as drift"

Green on the head, locally with Ruby 3.3.6 and Bundler 4.0.18:

cargo test -p socket-patch-core --lib vendor::                 3250 passed
cargo test -p socket-patch-cli --lib vendor                     162 passed
cargo test -p socket-patch-cli --test e2e_vendor_gem_build -- --ignored    14 passed
cargo test -p socket-patch-cli --test e2e_redirect_gem_build -- --ignored  55 passed
cargo clippy --locked -p socket-patch-core -p socket-patch-cli --all-features -- -D warnings   clean

cargo clippy --all-targets fails locally on test code this PR does not
touch (the same errors on main); CI lints without --all-targets.

Code review

/code-review high ran on the diff. Fixed: the other-uuid guard above, the
end-to-end test re-locking after bundle remove (Bundler 2.1 and older
edit only the Gemfile), the contract's wording for an unreadable file, and
the warning text naming one cause for every case.

Not fixed here, both existed before this PR and are outside the revert:

  • vendor --check still tells the user of a dropped gem to "re-run
    socket-patch vendor to rewire it". Its dependency-removed wording is
    reserved for npm and PyPI, whose lock readers can prove the package is
    gone. The commands that work now are scan --mode vendored --prune,
    rollback, remove and vendor --revert.
  • rollback of a dropped gem adds the generic reinstall_required
    advisory, though nothing is left to reinstall.

Follow-ups

  • The two items above.
  • The issue's comment about a Gemfile.lock with merge-conflict markers
    is a different cause (vendored mode does not name the conflict) and is not
    changed here.

https://claude.ai/code/session_011f9aeRfGJhX51LT65r9KP2


Generated by Claude Code

Refs #2325.

Assisted-by: Claude Code:claude-fable-5-2
After bundle remove, or a move to another release, neither the
Gemfile nor Gemfile.lock names the vendored copy. The gem revert
still reports the entry as drift and keeps the copy and the ledger
record on every run. Three of the unit tests and the end-to-end
test fail until the revert learns that case.

Refs #2325.

Assisted-by: Claude Code:claude-fable-5-2
The gem revert called every record it could not restore drift and
kept the copy and the ledger record. After bundle remove, or a move
of the gem to another release, nothing can undo that drift, so
rollback, remove, vendor --revert and scan --prune kept the entry
for good and vendor --check stayed red.

When neither the Gemfile nor Gemfile.lock names the entry's copy
directory in any spelling, such a record now warns
vendor_lock_entry_removed, as the other backends do. Both files
stay as the user left them and the copy is removed. A file that
still names the copy, or that cannot be read, keeps it as drift.

Fixes #2325.

Assisted-by: Claude Code:claude-fable-5-2
rollback and vendor --revert keep the manifest's patch, so the
audit that follows reports it as not vendored. Only remove leaves
nothing for the audit to report.

Refs #2325.

Assisted-by: Claude Code:claude-fable-5-2
A Gemfile and lock that route the gem through a vendored copy of
another patch uuid (an interrupted re-vendor, a merge that kept an
older ledger) no longer read as a dropped gem: the project is still
vendored and the entry may hold the only pre-vendor originals, so
the revert keeps it as drift.

The removed warning no longer states one cause for every case, the
contract says an unreadable file fails the revert, and the
end-to-end test re-locks after bundle remove, which Bundler 2.1
and older leave to the next install.

Refs #2325.

Assisted-by: Claude Code:claude-fable-5-2
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review December 1, 2026 18:19
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes gem revert classification and artifact deletion, which affects rollback/remove/prune flows, but scope is limited to an already-removed dependency with extensive regression tests.

Overview
Gem vendor revert now treats a vendored gem the user removed from the project (bundle remove) or repointed to another release like other ecosystems: irrecoverable wiring plus no remaining Gemfile / Gemfile.lock reference to a .socket/vendor/gem/ copy emits vendor_lock_entry_removed, leaves both files untouched, and removes the artifact and ledger entry instead of permanent vendor_lock_entry_drifted.

Implementation in revert_gem_opts: before applying wiring records, it scans both files for the entry’s uuid dir or the same gem/version under another patch uuid (names_a_vendored_copy_of, path spelling/case tolerant). Drift-keep behavior is unchanged when anything still names a vendored copy. CLI_CONTRACT.md documents the rule; unit and Bundler e2e tests cover revert, dry-run/--preserve-state, guards, and vendor --revert, rollback, and remove cleanup paths.

Reviewed by Cursor Bugbot for commit 1f3be36. Configure here.

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Dec 1, 2026
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does
After bundle remove, or after a move to another release, a gem that vendored mode patched could never be cleaned up: every undo command called it drift, kept the copy and the ledger record, and vendor --check stayed red. Now, when neither Gemfile nor Gemfile.lock names a vendored copy of the gem any more, a record that can't be restored warns vendor_lock_entry_removed and the copy and record are removed. The user's two files are never written. This is the same rule npm, uv and Pipenv already follow.

Risk: low. The new path runs only when a record has already drifted and neither file mentions the copy dir or a copy of the same gem@version under any uuid, in any spelling or case. A file that can't be read counts as "still referenced". Every doubtful case keeps the copy, as before. Neither file is written on this path, so it can't break a bundle install.

Look here

  • gem.rs:2232: the reference check, read before any record is reverted
  • gem.rs:2269: the new "removed, not drift" arm
  • gem.rs:2320: nothing is written when a record was removed
  • gem.rs:2347: names_a_vendored_copy_of, with version-boundary matching (rack-3.2.6 doesn't match rack-3.2.60 or rack-test-3.2.6)
  • e2e_vendor_gem_build.rs:2240: the real-Bundler end-to-end test

Verified

Changes I made: none.

Open questions: none.


Generated by Claude Code

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; LGTM.

@mikolalysenko
Mikola Lysenko (mikolalysenko) added this pull request to the merge queue Dec 1, 2026
Merged via the queue into main with commit 6a5a9da Dec 1, 2026
127 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the agent/fix-gem-revert-removed-dependency branch December 1, 2026 21:02
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

4 participants