Skip to content

Reverting a vendored gem while another gem is hosted puts its spec in the hosted gem's GEM section: fresh bundle install fails and rollback can no longer undo either gem #2090

Closed

Description

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

Summary

When a project has one vendored gem and one hosted gem, reverting the vendored gem (rollback, remove <purl>, or vendor --revert) puts its spec back into the hosted gem's GEM section of Gemfile.lock (the one whose remote: is the patch registry) instead of the https://rubygems.org/ section it came from.

The result:

  • The lock now says rack (3.2.8) comes from another gem's patch registry. A fresh bundle install (plain, not frozen) exits 7: "Could not find rack-3.2.8 in … patch-registry/gem//<capybara's uuid>/".
  • rollback can no longer undo the hosted gem: "its Socket GEM section does not lock exactly capybara (3.40.0)". A second rollback reports the same error for both gems. The tool cannot get the project out of this state.
  • socket-patch list reports the reverted gem as "Mode: hosted".
  • remove pkg:gem/rack@3.2.8 prints "Reverted vendoring for pkg:gem/rack@3.2.8" and exits 0 while leaving the lock like this.

A project gets a vendored and a hosted gem side by side by following the tool's own messages on an ordinary Rails Gemfile: scan --mode vendored vendors the top-level gems and refuses every gem declared in a group … do block, and scan --mode hosted then patches those. I found this on a rails new app (Rails 7.1) with rack and rails-html-sanitizer vendored and capybara, web-console, puma and bootsnap hosted.

remove <purl> is also the remedy the hosted scan prints for a vendored gem ("un-vendor this gem first (socket-patch remove pkg:gem/rack@3.2.8 …), then re-run the hosted scan"), so a user who follows that warning lands here as well.

Impact

Do no harm: rollback is meant to give back the original pair byte for byte. Here it leaves a Gemfile.lock no fresh checkout can install, exits 1, and tells the user to git checkout -- Gemfile Gemfile.lock, which (once the patched pair is committed) restores the patched pair, not the original. The machine that ran the rollback keeps working because Bundler reuses the installed gems, so the breakage first shows up in CI or on a colleague's machine.

Repro

Needs patches for rack 3.2.8 and capybara 3.40.0 (I used a local mock of the patch API and patch registry). Bundler 4.0.18, Ruby 3.3.6.

mkdir repro && cd repro && git init -q .
cat > Gemfile <<'G'
source "https://rubygems.org"

gem "rack", "3.2.8"

group :test do
  gem "capybara", "3.40.0"
end
G
bundle config set --local path vendor/bundle
bundle install
git add -A && git commit -qm base

socket-patch scan --mode vendored   # Vendored 1 package (rack); capybara refused: "declaration is indented (inside a group/conditional block)"
socket-patch scan --mode hosted     # Switched 1 package to hosted patches (capybara)
BUNDLE_FROZEN=true bundle install   # Bundle complete!
git add -A && git commit -qm patched

socket-patch rollback               # exit 1
git diff HEAD~1 -- Gemfile.lock
mkdir ../fresh && cp Gemfile Gemfile.lock ../fresh && cd ../fresh && bundle install   # exit 7

rollback:

Reverted vendoring for pkg:gem/rack@3.2.8
Error: Cannot restore pkg:gem/capybara@3.40.0 to its upstream registry entry: its Socket GEM section does not lock exactly capybara (3.40.0); restore it from version control instead (`git checkout -- Gemfile Gemfile.lock`)

Note: 1 unwired package keeps its patched bytes in installed trees until the next package-manager install.

The lock afterwards, against the pre-scan lock:

 GEM
-  remote: https://rubygems.org/
+  remote: http://127.0.0.1:8791/patch-registry/gem/tok123/2fc039c8-b97d-545c-b447-879e2b468dfe/
   specs:
-    addressable (2.9.2)
-      public_suffix (>= 2.0.2, < 8.0)
     capybara (3.40.0)
       addressable
 …
       xpath (~> 3.2)
+    rack (3.2.8)
+
+GEM
+  remote: https://rubygems.org/
+  specs:
+    addressable (2.9.2)
+      public_suffix (>= 2.0.2, < 8.0)
     matrix (0.4.3)
 …
     racc (1.8.1)
-    rack (3.2.8)
     rack-test (2.3.0)

Fresh checkout, bundle install:

… either the author of rack (3.2.8) has removed it, or you no longer
have access to that source. You'll need to update your bundle to a version other
than rack (3.2.8) that hasn't been removed, or check your credentials and access
rights for rubygems repository
http://127.0.0.1:8791/patch-registry/gem/tok123/2fc039c8-b97d-545c-b447-879e2b468dfe/
or installed locally, in order to install.

A second socket-patch rollback:

Error: Cannot restore pkg:gem/capybara@3.40.0 to its upstream registry entry: its Socket GEM section does not lock exactly capybara (3.40.0); …
Error: Cannot restore pkg:gem/rack@3.2.8 to its upstream registry entry: its Socket GEM section does not lock exactly capybara (3.40.0); …

Expected

CLI_CONTRACT.md / docs/ecosystems.md ("Undoing it"): rollback and remove restore the lock, "the pair is the pre-scan pair byte for byte". The vendored gem's spec belongs back in the GEM section whose remote is https://rubygems.org/, after which the hosted gem's section still locks exactly its own gem and can be unwound too.

Actual

The spec is spliced into the first GEM section of the lock. Bundler sorts a patch-registry section (https://patch.socket.dev/…, here http://127.0.0.1…) before https://rubygems.org/, so once any gem is hosted, the first section is a hosted one.

Versions

Bundler Ruby Command Result
4.0.18 3.3.6 rollback reproduces (twice on the minimal project, once on the Rails 7.1 app); exit 1
4.0.18 3.3.6 remove pkg:gem/rack@3.2.8 reproduces; exit 0
4.0.18 3.3.6 vendor --revert reproduces; also prints "Cannot restore pkg:gem/rack@3.2.8 to its upstream registry entry: Gemfile wires it in a shape other than the source: option …"
2.6.9 3.3.6 rollback reproduces
2.5.22 3.3.6 rollback reproduces
4.0.18 3.3.6 rollback with vendored gems only, or hosted gems only does not reproduce (pair restored byte for byte)

Tested on main 63f791d (5.1.0), Linux. This is lock text handling, so macOS and Windows should behave the same. First bad release not searched.

Suspect code

  • crates/socket-patch-core/src/vendor/gem.rs:2130 (record_gem_section): with no recorded remote: line it returns spans.first(), on the assumption in its doc comment that "the block came from" the first GEM section. That held when the gem was vendored (the lock had one GEM section), but the hosted scan that ran afterwards added a section ahead of it.
  • crates/socket-patch-core/src/vendor/gem.rs:2140 (record_source_remote): the source section's remote is recorded "only when it was not the first GEM section", so the record has nothing to find the right section by later.
  • Callers: crates/socket-patch-core/src/vendor/gem.rs:2750, :2854, :2864.

Generated by Claude Code

Activity

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

    @mikolalysenko
    CollaboratorAuthor

    [agent] Claiming this issue. Branch: agent/fix-gem-revert-lock-section.


    Generated by Claude Code

  3. added 3 commits that reference this issue on Nov 16, 2026
    3d89fde
    0271a02
    e258a63
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