Skip to content

Fix vendored gem revert into a hosted GEM section (#2090) - #2102

Merged
Mikola Lysenko (mikolalysenko) merged 4 commits into
mainfrom
agent/fix-gem-revert-lock-section
Nov 17, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 4 commits into
mainfrom
agent/fix-gem-revert-lock-section

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

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

Fixes #2090

Summary

With one gem vendored and another hosted, un-vendoring the first (rollback, remove <purl>, vendor --revert) put its spec into the hosted gem's GEM section of Gemfile.lock. A fresh bundle install then failed (it looked for the gem on the patch registry) and the hosted gem could no longer be rolled back. After this change the spec goes back to the section it came from, and rollback returns the pre-scan pair byte for byte.

Two files change: crates/socket-patch-core/src/vendor/gem.rs (about 40 lines of code, the rest is tests) and one new end-to-end test in e2e_redirect_gem_build.rs.

Root cause

A vendored gem's gemfile_lock_spec ledger record named the GEM section its spec was lifted from only when that was not the lock's first GEM section. With nothing recorded, revert used whatever section was first at revert time (record_gem_section). Section order is not stable: a hosted scan adds a patch-registry section, and Bundler sorts it ahead of https://rubygems.org/.

The change

  1. Revert of a record that names no section restores into the first GEM section that is not a hosted gem's own, instead of the lock's first section. A hosted section is one whose remotes are all …/patch-registry/gem/<token>/<uuid>/ URLs, on any origin, since revert isn't told which patch server the scan used. A Bundler < 2.2 merged section (patch-registry and upstream remotes together) is not skipped. The same rule decides whether a lock already holds the reverted state.
  2. Vendoring records the section when the spec is lifted from a hosted gem's own section, even if it is the lock's first, so the two sides agree on what "no section recorded" means. No production path lifts from a hosted section today (a takeover un-hosts first); this keeps the rule consistent.

The ledger format does not change: existing ledgers revert correctly without being rewritten, and new ledgers have the shape older binaries wrote (vendor_ledger_schema_e2e checks this).

Evidence

Red before the fix, green after:

Test Before After
vendor::gem::tests::revert_lock_text_skips_a_hosted_gem_section (unit, #2090) spec spliced into the hosted section pass
gem_vendored_unwind_beside_a_hosted_gem_restores_the_upstream_section (e2e, real Bundler 4.0.18, rollback and remove) hosted_restore_refused … does not lock exactly tiny-dep (1.0.0), exit 1 (the issue's error) pass: pair byte-identical to pre-scan, cold frozen bundle install succeeds

The e2e test follows the issue's steps: vendor one gem, host another, check the hosted section is first in the lock, then unwind. Its remove leg also checks the intermediate state (one gem un-vendored, the other still hosted) installs frozen from a cold checkout.

Other new unit tests: a merged section, a lock with only hosted sections (drift, no guess), an upstream source whose URL has another Socket path shape, and a spec lifted from a hosted section.

Commands run locally (Ruby 3.3.6, Bundler 4.0.18):

  • cargo test -p socket-patch-core --lib vendor::: 3066 passed.
  • cargo test -p socket-patch-cli --test e2e_redirect_gem_build --test e2e_vendor_gem_build --test e2e_gem_native_ext_build -- --ignored: 43 + 11 + 4 passed.
  • cargo test -p socket-patch-cli --test vendor_ledger_schema_e2e --test vendor_revert_tamper_e2e --test e2e_vex_vendor: 62 passed.
  • cargo clippy --locked --workspace --all-features -- -D warnings (CI's command): clean.
  • The full cargo test -p socket-patch-core --lib run had one failure, utils::process::tests::build_output_within_kills_the_process_group_at_the_deadline, a process-group timing test in code this PR doesn't touch.
  • cargo clippy --all-targets reports errors in test files this PR doesn't touch (tests/prebuilt_common, tests/repair, others) and none in the two changed files.

Review notes

/code-review high ran on the first commit. Fixed: the hosted-section test was wider than gem patch registries, the e2e asserted a per-source lock layout on Bundler < 2.2 (now skipped there with a message; the scenario needs per-source sections), and a test fixture guard could pass vacuously.

Not fixed, with reasons:

  • A record with no section still depends on section order among upstream sources. If the user adds a private source after vendoring and Bundler sorts it ahead of the original one, revert puts the spec in the new source's section. This is the behavior on main and is not what 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 reports. The second commit fixed it by recording the source remote: line for every gem, and CI's coverage (1) failed on vendor_ledger_schema_e2e::server_artifacts_preserve_legacy_wiring_shape_and_originals, which requires new ledgers to keep the originals older binaries wrote. The third commit backs that out. Closing this gap means changing what the ledger records, which is a maintainer's call (see Follow-ups).
  • The new e2e repeats the GenerationGem setup that four other tests in that file also repeat. Sharing it means editing those tests, a refactor outside this fix.

The history has three commits because of that back-out; the net diff is the first commit plus the three review fixes.

Follow-ups

  • Decision for a maintainer: should a vendored gem's gemfile_lock_spec record always carry its source section's remote: line? It would make revert independent of section order, at the cost of new ledgers differing from the ones older binaries wrote (the vendor_ledger_schema_e2e invariant would need a stated exception, as pypi-requirements has).
  • Not verified on Bundler 2.2 to 2.7 locally; CI's Bundler legs cover them.

https://claude.ai/code/session_011f9aePuhQymCjTDT6u4HbP


Generated by Claude Code

Claim commit for #2090: a vendored gem's revert puts its spec into a
hosted gem's GEM section.

Assisted-by: Claude Code:claude-fable-5-2
A vendored gem's lock record names the GEM section its spec came
from only when that was not the lock's first one. With none recorded,
revert put the spec into whatever section is first at revert time. A
hosted scan run after the vendoring adds a patch-registry section,
which bundler sorts ahead of rubygems.org, so the spec landed in the
hosted gem's section: a fresh bundle install failed and the hosted
gem could no longer be rolled back.

Revert now takes the first GEM section that is not a hosted gem's
own, and vendoring records the section when the spec is lifted from a
hosted one, so both sides agree on what an unrecorded section means.

Fixes #2090

Assisted-by: Claude Code:claude-fable-5-2
Skipping hosted sections at revert still left a record with no
remote line tied to the lock's section order: a source the user adds
after vendoring can sort ahead of rubygems.org just as a hosted gem's
section does.

Vendoring now records the remote line of the GEM section a spec is
lifted from whenever the section has one, so revert finds it by name.
Records written before this keep the positional rule, minus hosted
sections. When the recorded remote is gone and the lock has exactly
one upstream GEM section, the spec goes there, as it did before; with
several, the lock is reported as drifted.

A hosted section is now recognised by the gem patch-registry path
only, and the new end-to-end test is gated on Bundler 2.2, the first
to write one GEM section per source.

Refs #2090

Assisted-by: Claude Code:claude-fable-5-2
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 16, 2026 22:06
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes Gemfile.lock revert and wiring ledger semantics for vendored gems; incorrect section targeting could break bundle install or rollback, though behavior is heavily tested and fail-closed for ambiguous locks.

Overview
Fixes #2090: un-vendoring or rolling back when one gem is vendored and another is hosted no longer drops the vendored spec into the hosted gem’s GEM section in Gemfile.lock.

Vendoring now always records the lifted spec’s source remote: line in the gemfile_lock_spec wiring record (not only when that section wasn’t first). Revert resolves the target section by that line when present; for older records without it, it uses the first non–patch-registry GEM section (hosted-only locks are treated as drift with no guess).

Adds unit coverage for hosted sections sorting ahead of upstream, merged Bundler < 2.2 layouts, renamed sources, and legacy record shapes, plus an ignored e2e that exercises rollback and remove beside a hosted gem (Bundler ≥ 2.2).

Reviewed by Cursor Bugbot for commit 0271a02. Configure here.

The previous commit recorded the source section's remote line for
every vendored gem. vendor_ledger_schema_e2e requires a new ledger to
hold the same wiring originals an older binary wrote, and CI's
coverage job failed on it.

Go back to recording the remote only for a section that is not the
lock's first or is a hosted gem's own. Revert of a record with no
remote still skips hosted gems' sections, which is the fix for the
reported bug and needs no change to the ledger.

Kept from that commit: a hosted section is recognised by the gem
patch-registry path only, the end-to-end test is gated on Bundler
2.2, and the unit fixture checks both of its replacements.

Refs #2090

Assisted-by: Claude Code:claude-fable-5-2
Merged via the queue into main with commit 3b14afc Nov 17, 2026
49 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the agent/fix-gem-revert-lock-section branch November 17, 2026 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants