Skip to content

Fix revert of a vendored gem whose Gemfile line was edited (#2338) - #2470

Open
Mikola Lysenko (mikolalysenko) wants to merge 3 commits into
mainfrom
agent/fix-vendored-gem-edited-line-revert
Open

Mikola Lysenko (mikolalysenko) wants to merge 3 commits into
mainfrom
agent/fix-vendored-gem-edited-line-revert

Conversation

@mikolalysenko

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

Copy link
Copy Markdown
Collaborator

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

Fixes #2338

Summary

A vendored gem could not be rolled back or removed once the user edited
its Gemfile line: an option added (require: false, group: :web), the
line moved into a group block, or the quotes switched. Bundler,
scan --mode vendored and vendor --check all took the edited line as the
gem's wiring, but rollback, remove and vendor --revert called it
drift and kept the gem on every run, and none of the remedies they named
did anything.

The revert now undoes such a line around the user's edit:

  • the path: option comes off and the pre-vendor version requirement goes
    back;
  • everything else stays as the user has it: options added or taken off,
    indentation (a move into a group, platforms or conditional block),
    quotes, and a comment they changed;
  • the managed block of a transitive gem goes whole with an edit of its
    line (the block is vendor's own text; the gem had no declaration before).

The lock is restored as before, so the pair moves together.

Root cause

revert_gemfile_record (crates/socket-patch-core/src/vendor/gem.rs)
found the wired line by the exact text vendor wrote, allowing only for a
changed trailing comment. Vendor's own reader and vendor --check judge
the wiring by what the line means: the gem's declaration, with a path:
to the entry's copy. The two disagreed on every other edit.

What still reads as drift (neither file is written)

The edited line is restored only when it is the gem's one declaration,
pins the vendored version, and its path: names this patch's copy, with
keyword options around them. It stays drift when:

  • it pins another version, or carries a second constraint or a positional
    argument;
  • it carries a statement modifier (… if ENV["X"]) or a second statement;
  • it sits in a source / git / github / path block: restored there,
    the gem would resolve from a source the restored lock does not name;
  • another line declares the gem too;
  • the ledger's lines fail the existing integrity rule
    (OwnGem::rewrite_refusal), which is unchanged.

Evidence

Red before the fix, green after (cargo test -p socket-patch-core --lib -- vendor::gem::tests::revert_):

Test Covers
revert_edited_wired_gemfile_line_restores_the_pair_and_keeps_the_edit every edit in the issue's table: option added, group: added, moved into a group block, quotes switched; also a conditional block, a hash-rocket and a string-keyed option, an option before path:, changed spacing
revert_edited_wired_gemfile_line_follows_the_users_options an original with two constraints, an option and a comment: options added and taken off, comment kept or changed
revert_edited_wired_gemfile_line_reads_the_original_as_vendor_did originals with a string-keyed option, a ;, a tab after gem, and another gem's line that names this one
revert_edited_wired_gemfile_line_keeps_crlf CRLF Gemfile
revert_edited_managed_block_line_removes_the_block the transitive gem's managed line, LF and CRLF
revert_still_wired_but_unreadable_gemfile_writes_neither_file the forms above that stay drift

Each test also re-runs vendor over the edited Gemfile and asserts it is
read as in sync, so the revert and vendor agree.

revert_still_wired_but_edited_gemfile_writes_neither_file asserted the
reported behavior for two of these edits (require: false added, a comment
added to the managed line). It is replaced by the last test in the table,
which holds the same "neither file is written" guarantee for the forms that
are still drift.

End to end, with real Bundler (e2e_vendor_gem_build, new test
gem_vendor_edited_gemfile_line_is_undone_by_every_undo_command): vendor,
edit the line, bundle install plain and frozen (lock unchanged),
vendor --check exit 0, then vendor --revert / rollback / remove:
exit 0, no drift warning, the copy and ledger record gone, the Gemfile as
expected, the lock byte for byte the pre-vendor one, and a frozen install
passes.

Local runs (Ruby 3.3.6, Bundler 4.0.18):

cargo test -p socket-patch-core --lib                          9014 passed
cargo test -p socket-patch-cli --lib --bins                    1105 passed
cargo test -p socket-patch-cli --test e2e_vendor_gem_build \
  --test e2e_redirect_gem_build -- --include-ignored           57 + 24 passed
cargo clippy --locked --workspace --all-features -- -D warnings   clean

cargo clippy -p socket-patch-core --all-targets reports lints in test
targets on main as well (two of them in vendor/gem.rs, both in code
this PR does not touch); this PR adds none.

CLI_CONTRACT.md (the vendor --revert drift rule) is updated. The npm,
PyPI and gem wrappers need no change.

Review

/code-review high findings, all in the new code:

  • fixed: a string-keyed option ("require" => false) made the edited line
    drift; another gem's line that only names this gem counted as a second
    declaration; an original with a ; was refused; the original's spacing
    after gem was not restored; the contract named only group blocks.
  • not fixed, pre-existing: after a patch update repoints a line the user
    had edited (RewireOurs keeps its bytes), the new ledger entry records a
    line whose options differ from the carried-forward original, and the
    revert refuses the record ("puts back Gemfile text that is not one plain
    declaration…"). Accepting it means loosening the ledger integrity rule
    that revert_refuses_records_that_are_not_this_gems_pair_edit holds
    ("an option the vendored line does not carry"), which is a maintainer's
    call. This PR leaves that rule as it is.

Follow-ups

  • The patch-update case above.
  • A patch update over a wired line moved into a group block is refused
    by vendor as an indented declaration (also pre-existing).
  • The drift warnings still do not print the line they expect.

https://claude.ai/code/session_011f9aePN8sXX1PS6fywq5ub


Generated by Claude Code

Refs #2338.

Assisted-by: Claude Code:claude-fable-5-2
The vendored gem revert found the Gemfile line it wired by the exact
text vendor wrote, allowing only for a changed trailing comment. An
option added to that line, a move into a `group` block or switched
quotes read as drift, so `rollback`, `remove` and `vendor --revert`
kept the gem on every run, while `scan`, `vendor --check` and Bundler
all took the line as the gem's wiring.

The revert now reads such a line argument by argument. When it is the
gem's one declaration, pins the vendored version and its `path:` names
this patch's copy, the `path:` comes off, the pre-vendor version
requirement goes back and the rest of the line stays as the user has
it. The managed block of a transitive gem goes whole with an edit of
its line. A line that pins another version, carries a statement
modifier or a second statement, or sits in a block that picks its
gems' source is still drift, and neither file is written.

Fixes #2338.

Assisted-by: Claude Code:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9aePN8sXX1PS6fywq5ub
Review fixes for the edited-line revert:

- a string-keyed option (`"require" => false`) is an option, not a
  second version constraint;
- another gem's line that only names this gem is not a second
  declaration of it;
- an original line with a `;` in a string, or one ending its
  statement, is restorable;
- the spacing the original had after `gem` comes back when the user
  left the head as vendor wrote it;
- the contract names the blocks a moved line may sit in.

Refs #2338.

Assisted-by: Claude Code:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9aePN8sXX1PS6fywq5ub
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review December 3, 2026 02:22
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches Gemfile parsing and revert/lock pairing for gems; mistakes could mis-restore wiring or leave inconsistent Gemfile.lock, but behavior is heavily tested and drift cases are still conservative.

Overview
Fixes #2338: vendor --revert, rollback, and remove no longer mark a vendored gem as drift when the user edits the same wired gem line Bundler and vendor --check already accept (extra options, group / conditional wrapping, quote changes, etc.).

Gem revert in gem.rs still restores an exact wired line when possible; if that text is missing, it now locates the gem's sole declaration that still pins the vendored version and points path: at this patch's copy, strips path:, puts back the pre-vendor version constraints from the ledger's original, and keeps the user's options, indentation, quotes, and comment changes. Edited managed blocks for transitive gems are removed wholesale when the inner line still wires the copy.

Lines that change the pin, add statement modifiers or extra declarations, sit under source/git/github/path blocks, or duplicate the gem declaration remain drift—documented in CLI_CONTRACT.md. Coverage adds unit tests in gem.rs, an e2e test across undo commands in e2e_vendor_gem_build.rs, and narrows the old "edited = always drift" test to cases that should still refuse revert.

Reviewed by Cursor Bugbot for commit 237b439. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 237b439. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does
Fixes #2338. If a user edits the Gemfile line that vendor wrote for a gem (adds an option, moves it into a group block, switches quotes), rollback / remove / vendor --revert now undo it. Before, they treated the line as drift and kept the gem forever. The revert removes path:, puts back the version requirement from before vendoring, and leaves the user's other edits alone. For a transitive gem, the whole managed block is removed.

Risk: medium. This rewrites a user's Gemfile on revert. It only does so in a narrow, conservative case: the gem has one declaration, pinned to the vendored version, with a path: to this patch's copy, and keyword options only. Any other shape still falls back to the old drift path, which writes neither file. The lock restore has not changed.

Look here

  • gem.rs:4425: the revert's new fallback when the exact written line is not found
  • gem.rs:4533: edited_wired_line, which decides which edited lines count as still wired
  • gem.rs:4633: restore_edited_gemfile_line, which rebuilds the line (constraints, kept keywords, comment, CRLF) and refuses source/git/path blocks
  • gem.rs:4734: edited_managed_block, which removes the managed block of a transitive gem
  • gem.rs:7040: the cases that still count as drift

Verified

  • Read the full diff (gem.rs, the new e2e test in e2e_vendor_gem_build.rs, and the CLI_CONTRACT.md drift rule).
  • cargo test -p socket-patch-core --lib vendor::gem: 240 passed.
  • CI is green on 237b4397: ci-ok and clippy both pass. Bugbot found no issues. No open review threads, no conflict, and CHANGELOG.md is untouched.

Changes I made: none.

Open questions

  • The description names one known case that this PR does not fix. After a patch update repoints a line the user had edited, the revert still refuses the record. Fixing that means loosening the ledger-integrity rule, which is a maintainer's call, and it's tracked as a follow-up.

Generated by Claude Code

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Dec 3, 2026
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

2 participants