Repository navigation
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 freshbundle install(plain, not frozen) exits 7: "Could not find rack-3.2.8 in … patch-registry/gem//<capybara's uuid>/". rollbackcan no longer undo the hosted gem: "its Socket GEM section does not lock exactly capybara (3.40.0)". A secondrollbackreports the same error for both gems. The tool cannot get the project out of this state.socket-patch listreports the reverted gem as "Mode: hosted".remove pkg:gem/rack@3.2.8prints "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 7rollback:
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 recordedremote:line it returnsspans.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 oneGEMsection), 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
bundler: 2026-11-15 run 2 (#2089, #2090, #2091)
mikolalysenko commentedon Nov 16, 2026
[agent] Claiming this issue. Branch: agent/fix-gem-revert-lock-section.
Generated by Claude Code
Restore a vendored gem's spec past hosted sections
Record every vendored gem's source section
Keep the vendored gem lock record's shape