Skip to content

Vendored gem dropped with bundle remove (or moved to a newer version) can never be cleaned up: scan --prune, remove, rollback and vendor --revert keep it as drifted, and vendor --check stays red #2325

Done

Description

[agent] Found by the scheduled Bundler (RubyGems) bug-hunt routine (ledger #316).

Summary

After bundle remove <gem> drops a gem that vendored mode patched, no socket-patch command can clean up what is left of it. scan --mode vendored --prune, remove, rollback and vendor --revert all call the entry "drifted" and keep its .socket/vendor/gem/<uuid>/ directory and its ledger record, and vendor --check exits 1 from then on. Each message names a remedy that cannot work, because the gem is no longer in the project. The same happens when the user moves the gem to a newer release by editing its Gemfile line (gem "rake", "13.3.0") and running bundle install.

This is the Bundler form of #1132 (bun remove) and #1287 (uv remove), which #1147 fixed for those backends.

Impact

  • vendor --check is red for good, so a CI job that gates on it fails on every run after an ordinary bundle remove.
  • The dead vendored copy and its ledger record stay in the repository. The only way out is to delete the directory and edit .socket/vendor/state.json by hand.
  • remove and rollback of that gem exit 1, also on every later run.
  • Bundler itself is not affected: the Gemfile and Gemfile.lock are left alone and bundle install passes. vex correctly leaves the gem out (vendor_unwired).

Repro

A patch service that serves a patch for rake 13.2.1 (the repo's stand-in, tests/nokogiri/patch_service.rb, was used here; $M is the --api-url … --patch-server-url … --api-token … --org … flags that point at it).

mkdir repro && cd repro
printf 'source "https://rubygems.org"\n\ngem "rack", "2.2.8"\ngem "rake", "13.2.1"\n' > Gemfile
bundle config set --local path vendor/bundle
bundle install
socket-patch scan --mode vendored $M          # exit 0, rack and rake vendored
BUNDLE_FROZEN=true bundle install             # passes

bundle remove rake                            # the project no longer uses rake

socket-patch vendor --check $M                # exit 1
socket-patch scan --mode vendored --prune $M  # exit 0, entry kept
socket-patch remove pkg:gem/rake@13.2.1 $M    # exit 1
socket-patch rollback pkg:gem/rake@13.2.1 $M  # exit 1
socket-patch vendor --revert $M               # exit 0, rack reverted, rake kept
socket-patch vendor --check $M                # exit 1
ls .socket/vendor/gem                         # rake's uuid directory is still there

Actual

$ socket-patch vendor --check
pkg:gem/rake@13.2.1?platform=ruby: wiring missing: no lockfile or config references .socket/vendor/gem/00000002-… any more, so a fresh install gets the unpatched package; re-run `socket-patch vendor` to rewire it

$ socket-patch scan --mode vendored --prune
  [skip] pkg:gem/rake@13.2.1?platform=ruby (vendor_gem_version_not_locked): Gemfile.lock does not resolve rake 13.2.1: this project doesn't lock that version, so there is nothing to vendor
GC: kept 1 drifted vendored entry: lock entries were re-resolved since vendoring, so their artifacts and manifest/ledger entries were retained; undo the drift and re-run `vendor --revert` to finish.

$ socket-patch remove pkg:gem/rake@13.2.1
Warning: Gemfile.lock no longer carries what vendor wrote for rake; left alone, and neither Gemfile nor Gemfile.lock was changed
Warning: kept .socket/vendor/gem/00000002-…: some recorded lock entries were left alone (…) undo the drift (restore the vendored lock entries or re-vendor) and re-run `vendor --revert` to finish cleaning up
Warning: Kept vendored state for pkg:gem/rake@13.2.1?platform=ruby: lockfile wiring drifted
Error: 1 matching entry was drift-kept (vendored state and ledger record retained); re-run `scan --mode vendored` to normalize, then remove again

$ socket-patch vendor --revert
Reverted 1 vendored package.
Kept 1 drifted package: lock entries were re-resolved since vendoring, so their artifacts and ledger entries were retained — undo the drift and re-run `vendor --revert` to finish.

None of the named remedies works:

  • "re-run socket-patch vendor to rewire it": prints No manifest to vendor from; … entries are tracked in the ledger and changes nothing.
  • "re-run scan --mode vendored to normalize, then remove again": the scan skips the gem (vendor_gem_version_not_locked), and remove fails the same way again.
  • "undo the drift and re-run vendor --revert": the only way to undo it is to add the gem back to the project.

Expected

crates/socket-patch-cli/CLI_CONTRACT.md (vendored revert, drift rules) says that for the npm family "a recorded lock entry that no longer exists at all — the user removed the dependency — is not drift: it warns vendor_lock_entry_removed and the artifact and entry are kept unless every wired file that exists was read and none mentions the uuid in any spelling …, so rollback / remove / scan --prune clean up after npm uninstall / yarn remove / pnpm remove / bun remove", and that the same holds for an entry re-resolved away from the artifact by an upgrade. #1147 gave bun.lockb, uv and Pipenv the same rule. The gem backend should follow it: when neither Gemfile nor Gemfile.lock names the entry's .socket/vendor/gem/<uuid> directory any more, there is nothing to restore, so the artifact and the ledger record should go and vendor --check should pass again. Neither docs/ecosystems.md nor the contract documents a gem exception.

Versions

socket-patch Bundler Ruby Result
main cfc870f 4.1.1 3.3.6 reproduces (both the bundle remove and the upgrade variant)
main cfc870f 4.0.18 3.3.6 reproduces
main cfc870f 2.6.9 3.3.6 reproduces
main cfc870f 2.5.22 3.3.6 reproduces
5.1.0 (npm) 4.1.1 3.3.6 reproduces (scan --prune exits 1 there)

Not a regression since v5.1.0. Linux only was tested; nothing here depends on the platform.

Suspect code

crates/socket-patch-core/src/vendor/gem.rs:2174: every RecordRevert::Drifted record sets drifted = true and warns vendor_lock_entry_drifted. There is no arm for a record whose entry is gone while no wired file still names the uuid directory (the vendor_lock_entry_removed case of the other backends).


Generated by Claude Code

Activity

  1. added
    bug
    bughunt
    pm:bundler
    on Nov 28, 2026
  2. mikolalysenko commented on Nov 28, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Two more observations from the same run (main cfc870f, Bundler 4.1.1), in case they help the fix:

    • rollback --dry-run in the stuck state previews the revert as if it would work: it prints Would revert vendoring for pkg:gem/rake@13.2.1?platform=ruby beside the vendor_lock_entry_drifted warning, exits 0, and under --json reports status: success with a verified event for the gem. The real rollback then exits 1 and keeps the entry.
    • A Gemfile.lock with merge-conflict markers (a branch that added a gem, merged into the branch that vendored) goes down the same "drifted" path for every vendored gem: rollback, remove and vendor --check print the texts above, and a scan --mode vendored says Gemfile.lock does not resolve nokogiri 1.18.10: this project doesn't lock that version. Nothing is written, and after the conflict is resolved with bundle install everything works again (re-scan changes nothing, frozen install, vex, rollback). Hosted mode names the conflict marker's line in the same situation; vendored mode does not.

    Generated by Claude Code

  3. mikolalysenko commented on Dec 1, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Claiming this issue. Branch: agent/fix-gem-revert-removed-dependency.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent:triagedbugbughuntpm:bundlerpriority:p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions