Skip to content

Commit 6a5a9da

Browse files
Fix cleanup of a vendored gem the project dropped (#2325) (#2418)
* Start gem revert fix for a removed dependency Refs #2325. Assisted-by: Claude Code:claude-fable-5-2 * Add failing tests for a dropped vendored gem After bundle remove, or a move to another release, neither the Gemfile nor Gemfile.lock names the vendored copy. The gem revert still reports the entry as drift and keeps the copy and the ledger record on every run. Three of the unit tests and the end-to-end test fail until the revert learns that case. Refs #2325. Assisted-by: Claude Code:claude-fable-5-2 * Clean up a vendored gem the project dropped The gem revert called every record it could not restore drift and kept the copy and the ledger record. After bundle remove, or a move of the gem to another release, nothing can undo that drift, so rollback, remove, vendor --revert and scan --prune kept the entry for good and vendor --check stayed red. When neither the Gemfile nor Gemfile.lock names the entry's copy directory in any spelling, such a record now warns vendor_lock_entry_removed, as the other backends do. Both files stay as the user left them and the copy is removed. A file that still names the copy, or that cannot be read, keeps it as drift. Fixes #2325. Assisted-by: Claude Code:claude-fable-5-2 * Hold the dropped-gem audit to what each undo leaves rollback and vendor --revert keep the manifest's patch, so the audit that follows reports it as not vendored. Only remove leaves nothing for the audit to report. Refs #2325. Assisted-by: Claude Code:claude-fable-5-2 * Keep a gem entry wired under another patch uuid A Gemfile and lock that route the gem through a vendored copy of another patch uuid (an interrupted re-vendor, a merge that kept an older ledger) no longer read as a dropped gem: the project is still vendored and the entry may hold the only pre-vendor originals, so the revert keeps it as drift. The removed warning no longer states one cause for every case, the contract says an unreadable file fails the revert, and the end-to-end test re-locks after bundle remove, which Bundler 2.1 and older leave to the next install. Refs #2325. Assisted-by: Claude Code:claude-fable-5-2 --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 0120126 commit 6a5a9da

3 files changed

Lines changed: 453 additions & 6 deletions

File tree

‎crates/socket-patch-cli/CLI_CONTRACT.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,13 @@ worse, lets a warm cache silently serve unpatched bytes):
18791879
* `vendor --revert` restores the originals (fragments that no longer match — a user re-resolved —
18801880
are left alone with a `vendor_lock_entry_drifted` warning; the drift-kept artifact and entry stay,
18811881
every backend alike — gem included as of v5.0, where a MISSING `Gemfile`/`Gemfile.lock` instead
1882-
warns `vendor_lockfile_missing` and still removes the artifact; composer / maven / nuget, whose
1882+
warns `vendor_lockfile_missing` and still removes the artifact, and where a fragment that cannot
1883+
be restored once neither `Gemfile` nor `Gemfile.lock` names the `.socket/vendor/gem/<uuid>` dir,
1884+
or a vendored copy of the gem under another patch uuid, in any spelling — the gem was dropped
1885+
with `bundle remove`, or moved to another release — is not drift: it warns
1886+
`vendor_lock_entry_removed`, both files stay as the user left them, and the artifact and entry
1887+
are removed, so `vendor --revert` / `rollback` / `remove` / `scan --prune` clean up after it (a
1888+
file of the two that cannot be read fails the revert, which keeps both); composer / maven / nuget, whose
18831889
whole-file wiring cannot tell a converged fragment from a drifted one, keep the artifact exactly
18841890
while the live `composer.lock` / `pom.xml` / `nuget.config` still names its
18851891
`.socket/vendor/<eco>/<uuid>` dir — a file that no longer references it is warned about and the

‎crates/socket-patch-cli/tests/e2e_vendor_gem_build.rs‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,6 +2228,134 @@ fn gem_vendor_drops_positional_constraints() {
22282228
}
22292229
}
22302230

2231+
/// #2325: after the project drops a vendored gem (`bundle remove`) or moves
2232+
/// it to another release, nothing bundler reads names the vendored copy any
2233+
/// more. `vendor --check` reports the dead entry, and each undo command
2234+
/// then cleans it up: the copy and its ledger record go, the user's
2235+
/// Gemfile and Gemfile.lock stay byte for byte, a frozen install still
2236+
/// passes, and `vendor --check` is green again.
2237+
#[test]
2238+
#[ignore = "host capstone: shells out to a real bundler >= 1.17; the unpinned `test` job \
2239+
skips it, the e2e job runs it with a pinned toolchain via --ignored"]
2240+
fn gem_vendor_dropped_gem_is_cleaned_up_by_every_undo_command() {
2241+
// How the project drops the gem, and the command that cleans up.
2242+
let cases: [(&str, &[&str]); 4] = [
2243+
("remove", &["vendor", "--revert"]),
2244+
("remove", &["rollback"]),
2245+
("remove", &["remove", "--yes"]),
2246+
("upgrade", &["vendor", "--revert"]),
2247+
];
2248+
for (how, undo) in cases {
2249+
let what = format!("{how}, then `{}`", undo.join(" "));
2250+
let Some((_tmp, proj, _bundler, purl)) = staged_rack_project("dropped gem") else {
2251+
return;
2252+
};
2253+
let cwd = proj.to_str().unwrap();
2254+
let (code, stdout, stderr) =
2255+
run_socket(&proj, &["vendor", "--json", "--offline", "--cwd", cwd]);
2256+
assert_eq!(code, 0, "{what}: vendor failed.\n{stdout}\n{stderr}");
2257+
let uuid_dir = proj.join(".socket/vendor/gem").join(UUID);
2258+
assert!(uuid_dir.is_dir(), "{what}: vendored copy written");
2259+
2260+
// The user's edit, with real bundler.
2261+
let edit = match how {
2262+
// Bundler <= 2.1 edits only the Gemfile on `remove`; the
2263+
// install that follows re-locks, as it does for such a user.
2264+
"remove" => {
2265+
let remove = bundle(&proj, &["remove", DEP], false);
2266+
assert!(
2267+
remove.status.success(),
2268+
"{what}: `bundle remove` (test premise):\n{}",
2269+
String::from_utf8_lossy(&remove.stderr)
2270+
);
2271+
bundle(&proj, &["install"], false)
2272+
}
2273+
_ => {
2274+
let version = purl.rsplit('@').next().unwrap();
2275+
let other = if version == "3.1.8" { "3.1.9" } else { "3.1.8" };
2276+
std::fs::write(
2277+
proj.join("Gemfile"),
2278+
format!("source \"https://rubygems.org\"\n\ngem \"rack\", \"{other}\"\n"),
2279+
)
2280+
.unwrap();
2281+
bundle(&proj, &["install"], false)
2282+
}
2283+
};
2284+
assert!(
2285+
edit.status.success(),
2286+
"{what}: the user's bundler command (test premise):\n{}",
2287+
String::from_utf8_lossy(&edit.stderr)
2288+
);
2289+
let gemfile = std::fs::read(proj.join("Gemfile")).unwrap();
2290+
let lock = std::fs::read(proj.join("Gemfile.lock")).unwrap();
2291+
for file in [&gemfile, &lock] {
2292+
assert!(
2293+
!String::from_utf8_lossy(file).contains(".socket/vendor"),
2294+
"{what}: bundler left no reference to the vendored copy (test premise)"
2295+
);
2296+
}
2297+
let (code, stdout, stderr) = run_socket(&proj, &["vendor", "--check", "--cwd", cwd]);
2298+
assert!(
2299+
code == 1 && format!("{stdout}{stderr}").contains("wiring missing"),
2300+
"{what}: vendor --check reports the dead entry.\n{stdout}\n{stderr}"
2301+
);
2302+
2303+
// The clean-up.
2304+
let mut args: Vec<&str> = undo.to_vec();
2305+
if undo[0] != "vendor" {
2306+
args.push(&purl);
2307+
}
2308+
args.extend(["--json", "--cwd", cwd]);
2309+
let (code, stdout, stderr) = run_socket(&proj, &args);
2310+
assert_eq!(
2311+
code, 0,
2312+
"{what}: failed.\nstdout:\n{stdout}\nstderr:\n{stderr}"
2313+
);
2314+
let env = parse_json_envelope(&stdout);
2315+
assert_eq!(env["status"], "success", "{what}: {env}");
2316+
let all = format!("{stdout}{stderr}");
2317+
assert!(
2318+
!all.contains("drift"),
2319+
"{what}: a dropped gem is not reported as drift:\n{all}"
2320+
);
2321+
2322+
assert!(!uuid_dir.exists(), "{what}: the dead copy is removed");
2323+
let state = proj.join(".socket/vendor/state.json");
2324+
assert!(
2325+
!state.exists() || !std::fs::read_to_string(&state).unwrap().contains(UUID),
2326+
"{what}: the ledger record is dropped"
2327+
);
2328+
assert_eq!(
2329+
std::fs::read(proj.join("Gemfile")).unwrap(),
2330+
gemfile,
2331+
"{what}: the user's Gemfile is left alone"
2332+
);
2333+
assert_eq!(
2334+
std::fs::read(proj.join("Gemfile.lock")).unwrap(),
2335+
lock,
2336+
"{what}: the user's Gemfile.lock is left alone"
2337+
);
2338+
let install = bundle(&proj, &["install"], true);
2339+
assert!(
2340+
install.status.success(),
2341+
"{what}: a frozen install passes after the clean-up:\n{}",
2342+
String::from_utf8_lossy(&install.stderr)
2343+
);
2344+
// `remove` drops the manifest's patch too, so the audit has nothing
2345+
// left to hold the project to. The other two keep that patch, which
2346+
// the audit then reports as not vendored: no longer a dead entry.
2347+
let (code, stdout, stderr) = run_socket(&proj, &["vendor", "--check", "--cwd", cwd]);
2348+
let audit = format!("{stdout}{stderr}");
2349+
assert!(
2350+
!audit.contains("wiring missing") && !audit.contains(UUID),
2351+
"{what}: vendor --check no longer reports the dead entry:\n{audit}"
2352+
);
2353+
if undo[0] == "remove" {
2354+
assert_eq!(code, 0, "{what}: vendor --check is green again:\n{audit}");
2355+
}
2356+
}
2357+
}
2358+
22312359
/// #1821: vendoring over a CRLF Gemfile keeps every line CRLF (the rewired
22322360
/// `gem` line used to end in a bare LF), a real frozen install accepts the
22332361
/// result, and revert restores the Gemfile byte for byte.

0 commit comments

Comments
 (0)