From b5fb64172bac380cdb9f0bb336ccbb9b64f2ad1a Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 21:22:48 +0000 Subject: [PATCH 1/4] Start fix for gem revert lock section 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 From 3d89fde9884505f2f409649831c63247718dbe84 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 21:32:35 +0000 Subject: [PATCH 2/4] Restore a vendored gem's spec past hosted sections 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 --- .../tests/e2e_redirect_gem_build.rs | 186 ++++++++++++++++++ crates/socket-patch-core/src/vendor/gem.rs | 175 ++++++++++++++-- 2 files changed, 348 insertions(+), 13 deletions(-) diff --git a/crates/socket-patch-cli/tests/e2e_redirect_gem_build.rs b/crates/socket-patch-cli/tests/e2e_redirect_gem_build.rs index 688005c64..a9d34a59a 100644 --- a/crates/socket-patch-cli/tests/e2e_redirect_gem_build.rs +++ b/crates/socket-patch-cli/tests/e2e_redirect_gem_build.rs @@ -3058,6 +3058,192 @@ async fn unwind_after_superseding_rescan(command: &str) { ); } +/// #2090: one gem vendored, then another hosted. The vendored gem's lock +/// record names no `GEM` section (its spec came out of the only one), and +/// the hosted gem's patch-registry section sorts ahead of the upstream one. +/// Unwinding the vendored gem used to put its spec into the hosted gem's +/// section: a fresh `bundle install` then looked for it on the patch +/// registry and failed, and the hosted gem could no longer be unwound. +#[tokio::test(flavor = "multi_thread")] +#[ignore = "host capstone: shells out to a real ruby/gem/bundler; \ + the unpinned `test` job skips it, an e2e job with a pinned toolchain runs it via --ignored"] +async fn gem_vendored_unwind_beside_a_hosted_gem_restores_the_upstream_section() { + for command in ["rollback", "remove"] { + vendored_unwind_beside_a_hosted_gem(command).await; + } +} + +async fn vendored_unwind_beside_a_hosted_gem(command: &str) { + let Some(fx) = redirect_scanned_project( + &format!("vendored-beside-hosted-{command}"), + Spelling::Gemfile, + false, + true, + None, + Driver::ScanVex, + ) + .await + else { + return; + }; + const VULN_GEN2: &str = "10000000-1a2b-4a1b-8c2d-3e4f5a6b7c8d"; + const TINY_GEN2: &str = "80000000-1a2b-4a1b-8c2d-3e4f5a6b7c8d"; + let tiny_purl = format!("pkg:gem/{TRANSITIVE}@1.0.0"); + let api = fx._server.uri(); + let upstream = format!("{api}/upstream"); + let cwd = fx.proj.to_str().expect("utf8 tmp path"); + let gemfile_path = fx.proj.join(fx.gemfile_name); + let lock_path = fx.proj.join(fx.lock_name); + let socket = |what: &str, head: &[&str]| { + let mut argv = head.to_vec(); + argv.extend([ + "--json", + "--cwd", + cwd, + "--api-url", + &api, + "--org", + ORG, + "--api-token", + "fake", + "--patch-server-url", + &api, + ]); + let (code, stdout, stderr) = + run_socket_env(&fx.proj, &argv, &[("SOCKET_RUBYGEMS_URL", &upstream)]); + assert_eq!( + code, 0, + "{command}: {what}:\nstdout:\n{stdout}\nstderr:\n{stderr}" + ); + stdout + }; + let install = |what: &str, dir: &Path, frozen: bool| { + let envs: &[(&str, &str)] = if frozen { + &[("BUNDLE_FROZEN", "true")] + } else { + &[] + }; + let out = bundle_env(dir, &["install"], envs); + assert!( + out.status.success(), + "{command}: {what}: bundle install:\nstdout:\n{}\nstderr:\n{}", + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr) + ); + }; + let assert_pristine = |what: &str| { + assert_eq!( + std::fs::read_to_string(&gemfile_path).unwrap(), + String::from_utf8_lossy(&fx.pristine_gemfile), + "{command}: {what}: the Gemfile must come back byte for byte" + ); + assert_eq!( + std::fs::read_to_string(&lock_path).unwrap(), + String::from_utf8_lossy(&fx.pristine_lock), + "{command}: {what}: the lock must come back byte for byte" + ); + }; + + // Back to the upstream pair: the fixture's own hosted scan is not part + // of this scenario. + socket("unwind the fixture scan", &["rollback", PURL]); + assert_pristine("fixture unwind"); + install("upstream pair", &fx.proj, false); + + // The first patch generation covers the direct gem only; vendor it. + let stage = fx.tmp.path().join("generation-stage"); + let vuln_gem = build_gem( + &stage.join(VULN_GEN2), + DEP, + DEP_VERSION, + "vuln_gem.rb", + &patched_lib(), + &[TRANSITIVE], + ); + let vuln = || GenerationGem { + name: DEP, + uuid: VULN_GEN2, + deps: vec![format!("{TRANSITIVE}:>= 0")], + lib_file: "vuln_gem.rb", + orig: orig_lib(), + patched: patched_lib(), + gem: vuln_gem.clone(), + }; + let tiny_patched = TINY_LIB.replace("tiny-ok", "tiny-patched"); + let tiny = GenerationGem { + name: TRANSITIVE, + uuid: TINY_GEN2, + deps: vec![], + lib_file: "tiny_dep.rb", + orig: TINY_LIB.to_string(), + gem: build_gem( + &stage.join(TINY_GEN2), + TRANSITIVE, + "1.0.0", + "tiny_dep.rb", + &tiny_patched, + &[], + ), + patched: tiny_patched, + }; + mount_patch_generation(&fx._server, 2, &[vuln()]).await; + socket("vendored scan", &["scan", "--mode", "vendored", "--yes"]); + let lock = std::fs::read_to_string(&lock_path).unwrap(); + assert!( + lock.starts_with(&format!( + "PATH\n remote: .socket/vendor/gem/{VULN_GEN2}/{DEP}-{DEP_VERSION}\n" + )), + "{command}: {DEP} must be vendored:\n{lock}" + ); + install("vendored pair", &fx.proj, false); + + // The next generation also patches the transitive gem. A hosted scan + // leaves the vendored gem alone and hosts the other one, whose section + // bundler sorts ahead of the upstream one. + mount_patch_generation(&fx._server, 1, &[vuln(), tiny]).await; + socket("hosted scan", &["scan", "--mode", "hosted", "--yes"]); + install("vendored and hosted pair", &fx.proj, false); + let lock = std::fs::read_to_string(&lock_path).unwrap(); + let hosted_remote = format!("{api}/patch-registry/gem/{TOKEN}/{TINY_GEN2}/"); + let remotes = gem_remotes(&lock); + assert_eq!( + remotes.first().copied(), + Some(hosted_remote.as_str()), + "{command}: the hosted section must be the lock's first GEM section:\n{lock}" + ); + assert!( + lock.contains(&format!("{VULN_GEN2}/{DEP}-{DEP_VERSION}\n")), + "{command}: {DEP} must still be vendored:\n{lock}" + ); + + if command == "remove" { + // Un-vendoring one gem keeps the other hosted, and a cold checkout + // of that pair still installs. + socket("remove the vendored gem", &["remove", PURL, "--yes"]); + let lock = std::fs::read_to_string(&lock_path).unwrap(); + assert!( + lock.contains(&format!( + "GEM\n remote: {hosted_remote}\n specs:\n {TRANSITIVE} (1.0.0)\n\n\ + GEM\n remote: {upstream}/\n specs:\n {DEP} ({DEP_VERSION})\n" + )), + "{command}: {DEP} must return to the upstream section:\n{lock}" + ); + let fresh = stage_fresh_checkout(&fx, "one-gem-left-hosted"); + install("cold checkout with one gem left hosted", &fresh, true); + assert_eq!( + std::fs::read_to_string(fresh.join(fx.lock_name)).unwrap(), + lock, + "{command}: the frozen install leaves the lock as is" + ); + socket("remove the hosted gem", &["remove", &tiny_purl, "--yes"]); + } else { + socket("rollback both gems", &["rollback"]); + } + assert_pristine("unwind of both gems"); + let fresh = stage_fresh_checkout(&fx, "unwound"); + install("cold checkout of the unwound pair", &fresh, true); +} + /// Bundler's modern `gems.rb`/`gems.locked` spelling, end to end: the /// candidate list must read the pair, the rewriter must key its edits to it, /// and the real bundler must install the patched gem from the redirected diff --git a/crates/socket-patch-core/src/vendor/gem.rs b/crates/socket-patch-core/src/vendor/gem.rs index abce7f118..a23b46312 100644 --- a/crates/socket-patch-core/src/vendor/gem.rs +++ b/crates/socket-patch-core/src/vendor/gem.rs @@ -97,8 +97,10 @@ const GEMFILE_LOCK: &str = "Gemfile.lock"; /// block and the single 2-space line (if any) other than a ` remote: ` line /// is the pre-vendor DEPENDENCIES entry — its absence means the gem was /// transitive and revert deletes the added entry. A ` remote: ` line names -/// the GEM section the block came from; it is recorded only when that is not -/// the lock's first GEM section (#779), and revert restores into it. In +/// the GEM section the block came from, and revert restores into it. It is +/// left out when that section is the lock's first GEM section (#779) and +/// not a hosted gem's own ([`hosted_gem_section`]); revert then restores +/// into the first GEM section that is not a hosted gem's (#2090). In /// `new`, the last element is the DEPENDENCIES entry we wrote and the rest /// is the emitted PATH section. /// @@ -1815,8 +1817,9 @@ struct LockEdit { /// drops out of the ledger with the entry being replaced. checksum_bare: Option, /// The ` remote: ` line of the GEM section the spec block was lifted - /// from, when that is not the lock's first GEM section (#779). Revert - /// puts the block back into that section. + /// from, unless that is the lock's first GEM section (#779) and not a + /// hosted gem's own (#2090). Revert puts the block back into that + /// section. source_remote: Option, } @@ -1967,7 +1970,7 @@ fn edit_lock(text: &str, name: &str, version: &str, rel: &str) -> Result = match hit { Some((k, gem_start, gem_end, block_start)) => { - if k > 0 { + if k > 0 || hosted_gem_section(&lines[gem_start..gem_end]) { // Revert must find this section again; without a remote // line there is nothing to find it by. source_remote = Some( @@ -2214,21 +2217,38 @@ fn gem_section_spans(lines: &[String]) -> Vec<(usize, usize)> { spans } +/// True when `section` (one GEM section's lines) is a hosted gem's own: +/// it has a `remote:` line and each one is a Socket patch registry, on any +/// origin ([`crate::origin::hosted_shape_uuid`]; revert is not told which +/// patch servers the scan used). Hosted mode writes one such section per +/// patched gem, and bundler sorts it by its remote, which for +/// `https://patch.socket.dev/` is ahead of `https://rubygems.org/`. A +/// section that merges a patch registry remote with an upstream one (the +/// bundler < 2.2 lock layout) holds upstream specs too and is not one. +fn hosted_gem_section(section: &[String]) -> bool { + let mut remotes = section + .iter() + .filter_map(|l| l.strip_prefix(" remote: ")) + .peekable(); + remotes.peek().is_some() && remotes.all(|r| crate::origin::hosted_shape_uuid(r).is_some()) +} + /// The GEM section a `gemfile_lock_spec` record's spec block belongs in: -/// the one carrying the recorded `remote:` line, or the first GEM section -/// when none was recorded (the block came from it). +/// the one carrying the recorded `remote:` line. With none recorded the +/// block came from what was the lock's first GEM section when the gem was +/// vendored, and that is the first GEM section that is not a hosted gem's +/// own: a hosted scan run since then may have put its sections ahead +/// (#2090), and a block lifted from a hosted section always records it. fn record_gem_section(lines: &[String], source_remote: Option<&str>) -> Option<(usize, usize)> { - let spans = gem_section_spans(lines); + let mut spans = gem_section_spans(lines).into_iter(); match source_remote { - None => spans.first().copied(), - Some(remote) => spans - .into_iter() - .find(|&(gs, ge)| lines[gs..ge].iter().any(|l| l == remote)), + None => spans.find(|&(gs, ge)| !hosted_gem_section(&lines[gs..ge])), + Some(remote) => spans.find(|&(gs, ge)| lines[gs..ge].iter().any(|l| l == remote)), } } /// The recorded source-section `remote:` line in a `gemfile_lock_spec` -/// record's `original` (present only when it was not the first GEM section). +/// record's `original`, if it has one (see [`LOCK_WIRING_KIND`]). fn record_source_remote(original_lines: &[String]) -> Option<&str> { original_lines .iter() @@ -10019,4 +10039,133 @@ mod tests { LOCK_TWO_SOURCES ); } + + // ── #2090: a hosted gem's section sorts ahead of the upstream one ───── + + /// What a hosted scan of puma does to a lock: puma's spec moves into a + /// GEM section of its own whose remote is the patch registry at + /// `origin`, and its DEPENDENCIES entry gains the `!` source pin. + /// Bundler sorts that section ahead of `https://rubygems.org/`. + fn with_puma_hosted(lock: &str, origin: &str) -> String { + let hosted = format!( + "GEM\n remote: {origin}/patch-registry/gem/tok123/2fc039c8-b97d-545c-b447-879e2b468dfe/\n specs:\n puma (6.4.2)\n nio4r (~> 2.0)\n\nGEM\n remote: https://rubygems.org/\n specs:\n" + ); + let out = lock + .replacen( + "GEM\n remote: https://rubygems.org/\n specs:\n puma (6.4.2)\n nio4r (~> 2.0)\n", + &hosted, + 1, + ) + .replacen("\n puma\n", "\n puma!\n", 1); + assert_ne!(out, lock, "the fixture must change the lock"); + out + } + + /// #2090: rack was vendored out of the lock's only GEM section, so its + /// record names no section. A hosted scan of puma then put a patch + /// registry section first. Revert must restore rack into the + /// rubygems.org section it came from, not into puma's. + #[test] + fn revert_lock_text_skips_a_hosted_gem_section() { + let edit = edit_lock(LOCK_DIRECT, "rack", "3.2.6", ©_rel()).unwrap(); + assert_eq!(edit.source_remote, None); + let (original, new) = lock_record_lines(&edit); + // Socket's own host, and a self-hosted or mock patch server. + for origin in ["https://patch.socket.dev", "http://127.0.0.1:8791"] { + let both = with_puma_hosted(&edit.text, origin); + let want = with_puma_hosted(LOCK_DIRECT, origin); + assert_eq!( + revert_lock_text(&both, &original, &new).as_deref(), + Some(want.as_str()), + "{origin}" + ); + // The reverted lock is the record's converged state, and the + // corrupt one (rack in puma's section) is not. + assert!(lock_record_converged(&want, &original, &new), "{origin}"); + let corrupt = want + .replacen(" rack (3.2.6)\n base64 (>= 0.1.0)\n", "", 1) + .replacen( + " nio4r (~> 2.0)\n", + " nio4r (~> 2.0)\n rack (3.2.6)\n base64 (>= 0.1.0)\n", + 1, + ); + assert!( + !lock_record_converged(&corrupt, &original, &new), + "{origin}" + ); + } + } + + /// #2090: with every upstream spec vendored or hosted, bundler still + /// writes the rubygems.org section (empty), and revert restores into + /// it. A lock with nothing but hosted sections has no section the + /// block could have come from: drift, never a guess. + #[test] + fn revert_lock_text_never_restores_into_a_hosted_only_lock() { + let rack_only = "GEM\n remote: https://rubygems.org/\n specs:\n rack (3.2.6)\n\nPLATFORMS\n ruby\n\nDEPENDENCIES\n rack (~> 3.1)\n\nBUNDLED WITH\n 2.5.22\n"; + let edit = edit_lock(rack_only, "rack", "3.2.6", ©_rel()).unwrap(); + let (original, new) = lock_record_lines(&edit); + let hosted_section = "GEM\n remote: https://patch.socket.dev/patch-registry/gem/tok123/2fc039c8-b97d-545c-b447-879e2b468dfe/\n specs:\n puma (6.4.2)\n\n"; + let with_hosted = |lock: &str| lock.replacen("GEM\n", &format!("{hosted_section}GEM\n"), 1); + assert_eq!( + revert_lock_text(&with_hosted(&edit.text), &original, &new), + Some(with_hosted(rack_only)) + ); + let hosted_only = edit.text.replacen( + " remote: https://rubygems.org/\n", + " remote: https://patch.socket.dev/patch-registry/gem/tok123/2fc039c8-b97d-545c-b447-879e2b468dfe/\n", + 1, + ); + assert_eq!(revert_lock_text(&hosted_only, &original, &new), None); + } + + /// #2090: a GEM section that merges a patch registry remote with an + /// upstream one (the bundler < 2.2 lock layout) still holds upstream + /// specs, so it is where an unrecorded block goes back. + #[test] + fn revert_lock_text_keeps_a_merged_gem_section() { + let edit = edit_lock(LOCK_DIRECT, "rack", "3.2.6", ©_rel()).unwrap(); + let (original, new) = lock_record_lines(&edit); + let merge = |lock: &str| { + lock.replacen( + " remote: https://rubygems.org/\n", + " remote: https://patch.socket.dev/patch-registry/gem/tok123/2fc039c8-b97d-545c-b447-879e2b468dfe/\n remote: https://rubygems.org/\n", + 1, + ) + }; + assert_eq!( + revert_lock_text(&merge(&edit.text), &original, &new), + Some(merge(LOCK_DIRECT)) + ); + } + + /// #2090: record and revert agree on what "no recorded section" means. + /// A spec lifted from a hosted gem's section records that section even + /// when it is the lock's first, so revert puts it back there and not + /// into the first upstream section. + #[test] + fn edit_lock_records_a_hosted_source_section() { + let lock = with_puma_hosted(LOCK_DIRECT, "https://patch.socket.dev"); + let hosted_remote = " remote: https://patch.socket.dev/patch-registry/gem/tok123/2fc039c8-b97d-545c-b447-879e2b468dfe/"; + + let rack = edit_lock(&lock, "rack", "3.2.6", ©_rel()).unwrap(); + assert_eq!( + rack.source_remote.as_deref(), + Some(" remote: https://rubygems.org/") + ); + let (original, new) = lock_record_lines(&rack); + assert_eq!( + revert_lock_text(&rack.text, &original, &new).as_deref(), + Some(lock.as_str()) + ); + + let puma_rel = copy_rel().replace("rack-3.2.6", "puma-6.4.2"); + let puma = edit_lock(&lock, "puma", "6.4.2", &puma_rel).unwrap(); + assert_eq!(puma.source_remote.as_deref(), Some(hosted_remote)); + let (original, new) = lock_record_lines(&puma); + assert_eq!( + revert_lock_text(&puma.text, &original, &new).as_deref(), + Some(lock.as_str()) + ); + } } From 0271a026c4b807b9c625e2f8b69910854cb9a62c Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 22:06:12 +0000 Subject: [PATCH 3/4] Record every vendored gem's source section 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 --- .../tests/e2e_redirect_gem_build.rs | 10 + crates/socket-patch-core/src/vendor/gem.rs | 241 +++++++++++++----- 2 files changed, 188 insertions(+), 63 deletions(-) diff --git a/crates/socket-patch-cli/tests/e2e_redirect_gem_build.rs b/crates/socket-patch-cli/tests/e2e_redirect_gem_build.rs index a9d34a59a..dd31ec3fc 100644 --- a/crates/socket-patch-cli/tests/e2e_redirect_gem_build.rs +++ b/crates/socket-patch-cli/tests/e2e_redirect_gem_build.rs @@ -3086,6 +3086,16 @@ async fn vendored_unwind_beside_a_hosted_gem(command: &str) { else { return; }; + // Bundler < 2.2 merges every remote into one GEM section, so no hosted + // section can sort ahead of the upstream one there. + if !fx.bundler.at_least(2, 2) { + println!( + "SKIP e2e_redirect_gem_build (vendored-beside-hosted-{command}): bundler {}: \ + per-source GEM sections need bundler >= 2.2", + fx.bundler.version + ); + return; + } const VULN_GEN2: &str = "10000000-1a2b-4a1b-8c2d-3e4f5a6b7c8d"; const TINY_GEN2: &str = "80000000-1a2b-4a1b-8c2d-3e4f5a6b7c8d"; let tiny_purl = format!("pkg:gem/{TRANSITIVE}@1.0.0"); diff --git a/crates/socket-patch-core/src/vendor/gem.rs b/crates/socket-patch-core/src/vendor/gem.rs index a23b46312..3e002261b 100644 --- a/crates/socket-patch-core/src/vendor/gem.rs +++ b/crates/socket-patch-core/src/vendor/gem.rs @@ -97,10 +97,9 @@ const GEMFILE_LOCK: &str = "Gemfile.lock"; /// block and the single 2-space line (if any) other than a ` remote: ` line /// is the pre-vendor DEPENDENCIES entry — its absence means the gem was /// transitive and revert deletes the added entry. A ` remote: ` line names -/// the GEM section the block came from, and revert restores into it. It is -/// left out when that section is the lock's first GEM section (#779) and -/// not a hosted gem's own ([`hosted_gem_section`]); revert then restores -/// into the first GEM section that is not a hosted gem's (#2090). In +/// the GEM section the block came from, and revert restores into it +/// ([`record_gem_section`]). Records written before #2090 leave it out when +/// that section was the lock's first GEM section (#779). In /// `new`, the last element is the DEPENDENCIES entry we wrote and the rest /// is the emitted PATH section. /// @@ -1817,9 +1816,10 @@ struct LockEdit { /// drops out of the ledger with the entry being replaced. checksum_bare: Option, /// The ` remote: ` line of the GEM section the spec block was lifted - /// from, unless that is the lock's first GEM section (#779) and not a - /// hosted gem's own (#2090). Revert puts the block back into that - /// section. + /// from; revert puts the block back into that section. `None` only for + /// a first GEM section with no `remote:` line (a Gemfile with no + /// `source`). Which section is first changes once a hosted scan or a new + /// source adds one ahead, so position alone is not recorded (#2090). source_remote: Option, } @@ -1970,18 +1970,16 @@ fn edit_lock(text: &str, name: &str, version: &str, rel: &str) -> Result = match hit { Some((k, gem_start, gem_end, block_start)) => { - if k > 0 || hosted_gem_section(&lines[gem_start..gem_end]) { - // Revert must find this section again; without a remote - // line there is nothing to find it by. - source_remote = Some( - lines[gem_start..gem_end] - .iter() - .find(|l| l.starts_with(" remote: ")) - .cloned() - .ok_or_else(|| { - format!("Gemfile.lock GEM section holding `{name} ({version})` has no remote: line") - })?, - ); + // Revert must find this section again, and its `remote:` line + // is what it finds it by. + source_remote = lines[gem_start..gem_end] + .iter() + .find(|l| l.starts_with(" remote: ")) + .cloned(); + if k > 0 && source_remote.is_none() { + return Err(format!( + "Gemfile.lock GEM section holding `{name} ({version})` has no remote: line" + )); } let mut block_end = block_start + 1; while block_end < gem_end && lines[block_end].starts_with(" ") { @@ -2218,10 +2216,10 @@ fn gem_section_spans(lines: &[String]) -> Vec<(usize, usize)> { } /// True when `section` (one GEM section's lines) is a hosted gem's own: -/// it has a `remote:` line and each one is a Socket patch registry, on any -/// origin ([`crate::origin::hosted_shape_uuid`]; revert is not told which -/// patch servers the scan used). Hosted mode writes one such section per -/// patched gem, and bundler sorts it by its remote, which for +/// it has a `remote:` line and each one is a Socket gem patch registry +/// (`…/patch-registry/gem///`) on any origin; revert is not +/// told which patch servers the scan used. Hosted mode writes one such +/// section per patched gem, and bundler sorts it by its remote, which for /// `https://patch.socket.dev/` is ahead of `https://rubygems.org/`. A /// section that merges a patch registry remote with an upstream one (the /// bundler < 2.2 lock layout) holds upstream specs too and is not one. @@ -2230,21 +2228,39 @@ fn hosted_gem_section(section: &[String]) -> bool { .iter() .filter_map(|l| l.strip_prefix(" remote: ")) .peekable(); - remotes.peek().is_some() && remotes.all(|r| crate::origin::hosted_shape_uuid(r).is_some()) + remotes.peek().is_some() + && remotes.all(|r| { + r.contains("/patch-registry/gem/") && crate::origin::hosted_shape_uuid(r).is_some() + }) } -/// The GEM section a `gemfile_lock_spec` record's spec block belongs in: -/// the one carrying the recorded `remote:` line. With none recorded the -/// block came from what was the lock's first GEM section when the gem was -/// vendored, and that is the first GEM section that is not a hosted gem's -/// own: a hosted scan run since then may have put its sections ahead -/// (#2090), and a block lifted from a hosted section always records it. +/// The GEM section a `gemfile_lock_spec` record's spec block belongs in. +/// A hosted gem's own section ([`hosted_gem_section`]) is one only when the +/// record names it. +/// +/// * The section carrying the recorded `remote:` line. +/// * That line is gone (the user renamed the source): the lock's one other +/// GEM section, if it has exactly one. With several there is no telling +/// which, and the caller reports drift. +/// * No line recorded (a record written before #2090, whose block came out +/// of what was then the lock's first GEM section): the first GEM section +/// that is not a hosted gem's. A hosted scan run since the vendoring puts +/// its sections ahead of the upstream one. fn record_gem_section(lines: &[String], source_remote: Option<&str>) -> Option<(usize, usize)> { - let mut spans = gem_section_spans(lines).into_iter(); - match source_remote { - None => spans.find(|&(gs, ge)| !hosted_gem_section(&lines[gs..ge])), - Some(remote) => spans.find(|&(gs, ge)| lines[gs..ge].iter().any(|l| l == remote)), + let spans = gem_section_spans(lines); + if let Some(remote) = source_remote { + let named = spans + .iter() + .find(|&&(gs, ge)| lines[gs..ge].iter().any(|l| l == remote)); + if let Some(&span) = named { + return Some(span); + } } + let mut upstream = spans + .into_iter() + .filter(|&(gs, ge)| !hosted_gem_section(&lines[gs..ge])); + let first = upstream.next()?; + (source_remote.is_none() || upstream.next().is_none()).then_some(first) } /// The recorded source-section `remote:` line in a `gemfile_lock_spec` @@ -3737,9 +3753,10 @@ mod tests { &vec![ Value::String(" rack (3.2.6)".to_string()), Value::String(" base64 (>= 0.1.0)".to_string()), + Value::String(" remote: https://rubygems.org/".to_string()), Value::String(" rack (~> 3.1)".to_string()), ], - "spec block + old DEPENDENCIES line recorded verbatim" + "spec block, source remote + old DEPENDENCIES line recorded verbatim" ); let new = lk.new.as_ref().unwrap().as_array().unwrap(); assert_eq!( @@ -3803,9 +3820,14 @@ mod tests { .unwrap() .as_array() .unwrap(); - assert!( - orig.iter().all(|l| l.as_str().unwrap().starts_with(" ")), - "transitive: only the spec block is recorded: {orig:?}" + assert_eq!( + orig, + &vec![ + Value::String(" rack (3.2.6)".to_string()), + Value::String(" base64 (>= 0.1.0)".to_string()), + Value::String(" remote: https://rubygems.org/".to_string()), + ], + "transitive: only the spec block and its source remote are recorded" ); } @@ -9879,14 +9901,36 @@ mod tests { .is_empty()); } - /// The first-section case keeps its ledger shape: no source remote is - /// recorded, so ledgers written before #779 and after it agree. + /// #2090: the first-section case records its source remote too. Which + /// section is first changes when a later scan or a new source adds one + /// ahead of it, so the position says nothing at revert time. #[test] - fn edit_lock_first_gem_section_records_no_source_remote() { + fn edit_lock_first_gem_section_records_its_source_remote() { let edit = edit_lock(LOCK_DIRECT, "rack", "3.2.6", ©_rel()).unwrap(); - assert_eq!(edit.source_remote, None); + assert_eq!( + edit.source_remote.as_deref(), + Some(" remote: https://rubygems.org/") + ); let (original, _new) = lock_record_lines(&edit); - assert!(original.iter().all(|l| !l.starts_with(" remote: "))); + assert_eq!( + original, + [ + " rack (3.2.6)", + " base64 (>= 0.1.0)", + " remote: https://rubygems.org/", + " rack (~> 3.1)" + ] + ); + // A Gemfile with no `source` locks a GEM section with no remote: + // there is nothing to record, and revert still finds the section. + let no_source = LOCK_DIRECT.replacen(" remote: https://rubygems.org/\n", "", 1); + let edit = edit_lock(&no_source, "rack", "3.2.6", ©_rel()).unwrap(); + assert_eq!(edit.source_remote, None); + let (original, new) = lock_record_lines(&edit); + assert_eq!( + revert_lock_text(&edit.text, &original, &new).as_deref(), + Some(no_source.as_str()) + ); } /// SECURITY/fail-closed: the same `name (version)` in two GEM sections @@ -10057,21 +10101,40 @@ mod tests { 1, ) .replacen("\n puma\n", "\n puma!\n", 1); - assert_ne!(out, lock, "the fixture must change the lock"); + assert!( + out.contains(&hosted) && out.contains("\n puma!\n"), + "the fixture must move puma to a hosted section:\n{out}" + ); out } - /// #2090: rack was vendored out of the lock's only GEM section, so its - /// record names no section. A hosted scan of puma then put a patch - /// registry section first. Revert must restore rack into the - /// rubygems.org section it came from, not into puma's. + /// A record as releases before #2090 wrote it for a spec lifted from + /// the lock's first GEM section: no source `remote:` line. + fn before_2090(original: &[String]) -> Vec { + let legacy: Vec = original + .iter() + .filter(|l| !l.starts_with(" remote: ")) + .cloned() + .collect(); + assert_eq!(legacy.len() + 1, original.len(), "{original:?}"); + legacy + } + + /// #2090: rack was vendored out of the lock's only GEM section. A + /// hosted scan of puma then put a patch registry section first. Revert + /// must restore rack into the rubygems.org section it came from, not + /// into puma's, for the record a release before the fix wrote (which + /// names no section) and for the one written now. #[test] fn revert_lock_text_skips_a_hosted_gem_section() { let edit = edit_lock(LOCK_DIRECT, "rack", "3.2.6", ©_rel()).unwrap(); - assert_eq!(edit.source_remote, None); - let (original, new) = lock_record_lines(&edit); + let (recorded, new) = lock_record_lines(&edit); // Socket's own host, and a self-hosted or mock patch server. - for origin in ["https://patch.socket.dev", "http://127.0.0.1:8791"] { + for (origin, original) in [ + ("https://patch.socket.dev", before_2090(&recorded)), + ("http://127.0.0.1:8791", before_2090(&recorded)), + ("https://patch.socket.dev", recorded.clone()), + ] { let both = with_puma_hosted(&edit.text, origin); let want = with_puma_hosted(LOCK_DIRECT, origin); assert_eq!( @@ -10104,28 +10167,81 @@ mod tests { fn revert_lock_text_never_restores_into_a_hosted_only_lock() { let rack_only = "GEM\n remote: https://rubygems.org/\n specs:\n rack (3.2.6)\n\nPLATFORMS\n ruby\n\nDEPENDENCIES\n rack (~> 3.1)\n\nBUNDLED WITH\n 2.5.22\n"; let edit = edit_lock(rack_only, "rack", "3.2.6", ©_rel()).unwrap(); - let (original, new) = lock_record_lines(&edit); + let (recorded, new) = lock_record_lines(&edit); let hosted_section = "GEM\n remote: https://patch.socket.dev/patch-registry/gem/tok123/2fc039c8-b97d-545c-b447-879e2b468dfe/\n specs:\n puma (6.4.2)\n\n"; let with_hosted = |lock: &str| lock.replacen("GEM\n", &format!("{hosted_section}GEM\n"), 1); - assert_eq!( - revert_lock_text(&with_hosted(&edit.text), &original, &new), - Some(with_hosted(rack_only)) - ); let hosted_only = edit.text.replacen( " remote: https://rubygems.org/\n", " remote: https://patch.socket.dev/patch-registry/gem/tok123/2fc039c8-b97d-545c-b447-879e2b468dfe/\n", 1, ); - assert_eq!(revert_lock_text(&hosted_only, &original, &new), None); + for original in [before_2090(&recorded), recorded] { + assert_eq!( + revert_lock_text(&with_hosted(&edit.text), &original, &new), + Some(with_hosted(rack_only)) + ); + assert_eq!(revert_lock_text(&hosted_only, &original, &new), None); + } + } + + /// #2090: a source added to the Gemfile after the vendoring sorts ahead + /// of rubygems.org. The recorded remote still names the section the + /// spec came from. + #[test] + fn revert_lock_text_finds_its_section_behind_a_later_source() { + let edit = edit_lock(LOCK_DIRECT, "rack", "3.2.6", ©_rel()).unwrap(); + let (original, new) = lock_record_lines(&edit); + let private = + "GEM\n remote: https://gems.example.com/\n specs:\n aaa-internal (1.0.0)\n\n"; + let with_private = |lock: &str| lock.replacen("GEM\n", &format!("{private}GEM\n"), 1); + let want = with_private(LOCK_DIRECT); + assert_eq!( + revert_lock_text(&with_private(&edit.text), &original, &new).as_deref(), + Some(want.as_str()) + ); + assert!(lock_record_converged(&want, &original, &new)); + } + + /// The recorded source was renamed (rubygems.org to a mirror) and the + /// lock has one upstream GEM section: the spec can only belong there, + /// with or without a hosted gem's section beside it. (Two upstream + /// sections and a missing remote are drift: + /// `revert_lock_text_targets_recorded_gem_section`.) + #[test] + fn revert_lock_text_follows_a_renamed_only_source() { + let edit = edit_lock(LOCK_DIRECT, "rack", "3.2.6", ©_rel()).unwrap(); + let (original, new) = lock_record_lines(&edit); + let mirror = |lock: &str| lock.replace("https://rubygems.org/", "https://mirror.example/"); + for hosted in [false, true] { + let shape = |lock: &str| { + let lock = mirror(lock); + if hosted { + mirror(&with_puma_hosted( + &lock.replace("https://mirror.example/", "https://rubygems.org/"), + "https://patch.socket.dev", + )) + } else { + lock + } + }; + let want = shape(LOCK_DIRECT); + assert_eq!( + revert_lock_text(&shape(&edit.text), &original, &new).as_deref(), + Some(want.as_str()), + "hosted={hosted}" + ); + assert!(lock_record_converged(&want, &original, &new)); + } } /// #2090: a GEM section that merges a patch registry remote with an /// upstream one (the bundler < 2.2 lock layout) still holds upstream - /// specs, so it is where an unrecorded block goes back. + /// specs, so it is where a block with no recorded section goes back. #[test] fn revert_lock_text_keeps_a_merged_gem_section() { let edit = edit_lock(LOCK_DIRECT, "rack", "3.2.6", ©_rel()).unwrap(); - let (original, new) = lock_record_lines(&edit); + let (recorded, new) = lock_record_lines(&edit); + let original = before_2090(&recorded); let merge = |lock: &str| { lock.replacen( " remote: https://rubygems.org/\n", @@ -10139,10 +10255,9 @@ mod tests { ); } - /// #2090: record and revert agree on what "no recorded section" means. - /// A spec lifted from a hosted gem's section records that section even - /// when it is the lock's first, so revert puts it back there and not - /// into the first upstream section. + /// #2090: a spec lifted from a hosted gem's section records that + /// section like any other, so revert puts it back there and not into + /// the upstream section. #[test] fn edit_lock_records_a_hosted_source_section() { let lock = with_puma_hosted(LOCK_DIRECT, "https://patch.socket.dev"); From e258a636d4b99191a30f3c83ffc20b75e28f85ee Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 22:18:54 +0000 Subject: [PATCH 4/4] Keep the vendored gem lock record's shape 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 --- crates/socket-patch-core/src/vendor/gem.rs | 243 +++++++-------------- 1 file changed, 77 insertions(+), 166 deletions(-) diff --git a/crates/socket-patch-core/src/vendor/gem.rs b/crates/socket-patch-core/src/vendor/gem.rs index 3e002261b..0f83bbcc4 100644 --- a/crates/socket-patch-core/src/vendor/gem.rs +++ b/crates/socket-patch-core/src/vendor/gem.rs @@ -97,9 +97,10 @@ const GEMFILE_LOCK: &str = "Gemfile.lock"; /// block and the single 2-space line (if any) other than a ` remote: ` line /// is the pre-vendor DEPENDENCIES entry — its absence means the gem was /// transitive and revert deletes the added entry. A ` remote: ` line names -/// the GEM section the block came from, and revert restores into it -/// ([`record_gem_section`]). Records written before #2090 leave it out when -/// that section was the lock's first GEM section (#779). In +/// the GEM section the block came from, and revert restores into it. It is +/// left out when that section is the lock's first GEM section (#779) and +/// not a hosted gem's own ([`hosted_gem_section`]); revert then restores +/// into the first GEM section that is not a hosted gem's (#2090). In /// `new`, the last element is the DEPENDENCIES entry we wrote and the rest /// is the emitted PATH section. /// @@ -1816,10 +1817,9 @@ struct LockEdit { /// drops out of the ledger with the entry being replaced. checksum_bare: Option, /// The ` remote: ` line of the GEM section the spec block was lifted - /// from; revert puts the block back into that section. `None` only for - /// a first GEM section with no `remote:` line (a Gemfile with no - /// `source`). Which section is first changes once a hosted scan or a new - /// source adds one ahead, so position alone is not recorded (#2090). + /// from, unless that is the lock's first GEM section (#779) and not a + /// hosted gem's own (#2090). Revert puts the block back into that + /// section. source_remote: Option, } @@ -1970,16 +1970,18 @@ fn edit_lock(text: &str, name: &str, version: &str, rel: &str) -> Result = match hit { Some((k, gem_start, gem_end, block_start)) => { - // Revert must find this section again, and its `remote:` line - // is what it finds it by. - source_remote = lines[gem_start..gem_end] - .iter() - .find(|l| l.starts_with(" remote: ")) - .cloned(); - if k > 0 && source_remote.is_none() { - return Err(format!( - "Gemfile.lock GEM section holding `{name} ({version})` has no remote: line" - )); + if k > 0 || hosted_gem_section(&lines[gem_start..gem_end]) { + // Revert must find this section again; without a remote + // line there is nothing to find it by. + source_remote = Some( + lines[gem_start..gem_end] + .iter() + .find(|l| l.starts_with(" remote: ")) + .cloned() + .ok_or_else(|| { + format!("Gemfile.lock GEM section holding `{name} ({version})` has no remote: line") + })?, + ); } let mut block_end = block_start + 1; while block_end < gem_end && lines[block_end].starts_with(" ") { @@ -2234,33 +2236,18 @@ fn hosted_gem_section(section: &[String]) -> bool { }) } -/// The GEM section a `gemfile_lock_spec` record's spec block belongs in. -/// A hosted gem's own section ([`hosted_gem_section`]) is one only when the -/// record names it. -/// -/// * The section carrying the recorded `remote:` line. -/// * That line is gone (the user renamed the source): the lock's one other -/// GEM section, if it has exactly one. With several there is no telling -/// which, and the caller reports drift. -/// * No line recorded (a record written before #2090, whose block came out -/// of what was then the lock's first GEM section): the first GEM section -/// that is not a hosted gem's. A hosted scan run since the vendoring puts -/// its sections ahead of the upstream one. +/// The GEM section a `gemfile_lock_spec` record's spec block belongs in: +/// the one carrying the recorded `remote:` line. With none recorded the +/// block came from what was the lock's first GEM section when the gem was +/// vendored, and that is the first GEM section that is not a hosted gem's +/// own: a hosted scan run since then may have put its sections ahead +/// (#2090), and a block lifted from a hosted section always records it. fn record_gem_section(lines: &[String], source_remote: Option<&str>) -> Option<(usize, usize)> { - let spans = gem_section_spans(lines); - if let Some(remote) = source_remote { - let named = spans - .iter() - .find(|&&(gs, ge)| lines[gs..ge].iter().any(|l| l == remote)); - if let Some(&span) = named { - return Some(span); - } + let mut spans = gem_section_spans(lines).into_iter(); + match source_remote { + None => spans.find(|&(gs, ge)| !hosted_gem_section(&lines[gs..ge])), + Some(remote) => spans.find(|&(gs, ge)| lines[gs..ge].iter().any(|l| l == remote)), } - let mut upstream = spans - .into_iter() - .filter(|&(gs, ge)| !hosted_gem_section(&lines[gs..ge])); - let first = upstream.next()?; - (source_remote.is_none() || upstream.next().is_none()).then_some(first) } /// The recorded source-section `remote:` line in a `gemfile_lock_spec` @@ -3753,10 +3740,9 @@ mod tests { &vec![ Value::String(" rack (3.2.6)".to_string()), Value::String(" base64 (>= 0.1.0)".to_string()), - Value::String(" remote: https://rubygems.org/".to_string()), Value::String(" rack (~> 3.1)".to_string()), ], - "spec block, source remote + old DEPENDENCIES line recorded verbatim" + "spec block + old DEPENDENCIES line recorded verbatim" ); let new = lk.new.as_ref().unwrap().as_array().unwrap(); assert_eq!( @@ -3820,14 +3806,9 @@ mod tests { .unwrap() .as_array() .unwrap(); - assert_eq!( - orig, - &vec![ - Value::String(" rack (3.2.6)".to_string()), - Value::String(" base64 (>= 0.1.0)".to_string()), - Value::String(" remote: https://rubygems.org/".to_string()), - ], - "transitive: only the spec block and its source remote are recorded" + assert!( + orig.iter().all(|l| l.as_str().unwrap().starts_with(" ")), + "transitive: only the spec block is recorded: {orig:?}" ); } @@ -9901,36 +9882,14 @@ mod tests { .is_empty()); } - /// #2090: the first-section case records its source remote too. Which - /// section is first changes when a later scan or a new source adds one - /// ahead of it, so the position says nothing at revert time. + /// The first-section case keeps its ledger shape: no source remote is + /// recorded, so ledgers written before #779 and after it agree. #[test] - fn edit_lock_first_gem_section_records_its_source_remote() { + fn edit_lock_first_gem_section_records_no_source_remote() { let edit = edit_lock(LOCK_DIRECT, "rack", "3.2.6", ©_rel()).unwrap(); - assert_eq!( - edit.source_remote.as_deref(), - Some(" remote: https://rubygems.org/") - ); - let (original, _new) = lock_record_lines(&edit); - assert_eq!( - original, - [ - " rack (3.2.6)", - " base64 (>= 0.1.0)", - " remote: https://rubygems.org/", - " rack (~> 3.1)" - ] - ); - // A Gemfile with no `source` locks a GEM section with no remote: - // there is nothing to record, and revert still finds the section. - let no_source = LOCK_DIRECT.replacen(" remote: https://rubygems.org/\n", "", 1); - let edit = edit_lock(&no_source, "rack", "3.2.6", ©_rel()).unwrap(); assert_eq!(edit.source_remote, None); - let (original, new) = lock_record_lines(&edit); - assert_eq!( - revert_lock_text(&edit.text, &original, &new).as_deref(), - Some(no_source.as_str()) - ); + let (original, _new) = lock_record_lines(&edit); + assert!(original.iter().all(|l| !l.starts_with(" remote: "))); } /// SECURITY/fail-closed: the same `name (version)` in two GEM sections @@ -10108,33 +10067,17 @@ mod tests { out } - /// A record as releases before #2090 wrote it for a spec lifted from - /// the lock's first GEM section: no source `remote:` line. - fn before_2090(original: &[String]) -> Vec { - let legacy: Vec = original - .iter() - .filter(|l| !l.starts_with(" remote: ")) - .cloned() - .collect(); - assert_eq!(legacy.len() + 1, original.len(), "{original:?}"); - legacy - } - - /// #2090: rack was vendored out of the lock's only GEM section. A - /// hosted scan of puma then put a patch registry section first. Revert - /// must restore rack into the rubygems.org section it came from, not - /// into puma's, for the record a release before the fix wrote (which - /// names no section) and for the one written now. + /// #2090: rack was vendored out of the lock's only GEM section, so its + /// record names no section. A hosted scan of puma then put a patch + /// registry section first. Revert must restore rack into the + /// rubygems.org section it came from, not into puma's. #[test] fn revert_lock_text_skips_a_hosted_gem_section() { let edit = edit_lock(LOCK_DIRECT, "rack", "3.2.6", ©_rel()).unwrap(); - let (recorded, new) = lock_record_lines(&edit); + assert_eq!(edit.source_remote, None); + let (original, new) = lock_record_lines(&edit); // Socket's own host, and a self-hosted or mock patch server. - for (origin, original) in [ - ("https://patch.socket.dev", before_2090(&recorded)), - ("http://127.0.0.1:8791", before_2090(&recorded)), - ("https://patch.socket.dev", recorded.clone()), - ] { + for origin in ["https://patch.socket.dev", "http://127.0.0.1:8791"] { let both = with_puma_hosted(&edit.text, origin); let want = with_puma_hosted(LOCK_DIRECT, origin); assert_eq!( @@ -10167,81 +10110,28 @@ mod tests { fn revert_lock_text_never_restores_into_a_hosted_only_lock() { let rack_only = "GEM\n remote: https://rubygems.org/\n specs:\n rack (3.2.6)\n\nPLATFORMS\n ruby\n\nDEPENDENCIES\n rack (~> 3.1)\n\nBUNDLED WITH\n 2.5.22\n"; let edit = edit_lock(rack_only, "rack", "3.2.6", ©_rel()).unwrap(); - let (recorded, new) = lock_record_lines(&edit); + let (original, new) = lock_record_lines(&edit); let hosted_section = "GEM\n remote: https://patch.socket.dev/patch-registry/gem/tok123/2fc039c8-b97d-545c-b447-879e2b468dfe/\n specs:\n puma (6.4.2)\n\n"; let with_hosted = |lock: &str| lock.replacen("GEM\n", &format!("{hosted_section}GEM\n"), 1); + assert_eq!( + revert_lock_text(&with_hosted(&edit.text), &original, &new), + Some(with_hosted(rack_only)) + ); let hosted_only = edit.text.replacen( " remote: https://rubygems.org/\n", " remote: https://patch.socket.dev/patch-registry/gem/tok123/2fc039c8-b97d-545c-b447-879e2b468dfe/\n", 1, ); - for original in [before_2090(&recorded), recorded] { - assert_eq!( - revert_lock_text(&with_hosted(&edit.text), &original, &new), - Some(with_hosted(rack_only)) - ); - assert_eq!(revert_lock_text(&hosted_only, &original, &new), None); - } - } - - /// #2090: a source added to the Gemfile after the vendoring sorts ahead - /// of rubygems.org. The recorded remote still names the section the - /// spec came from. - #[test] - fn revert_lock_text_finds_its_section_behind_a_later_source() { - let edit = edit_lock(LOCK_DIRECT, "rack", "3.2.6", ©_rel()).unwrap(); - let (original, new) = lock_record_lines(&edit); - let private = - "GEM\n remote: https://gems.example.com/\n specs:\n aaa-internal (1.0.0)\n\n"; - let with_private = |lock: &str| lock.replacen("GEM\n", &format!("{private}GEM\n"), 1); - let want = with_private(LOCK_DIRECT); - assert_eq!( - revert_lock_text(&with_private(&edit.text), &original, &new).as_deref(), - Some(want.as_str()) - ); - assert!(lock_record_converged(&want, &original, &new)); - } - - /// The recorded source was renamed (rubygems.org to a mirror) and the - /// lock has one upstream GEM section: the spec can only belong there, - /// with or without a hosted gem's section beside it. (Two upstream - /// sections and a missing remote are drift: - /// `revert_lock_text_targets_recorded_gem_section`.) - #[test] - fn revert_lock_text_follows_a_renamed_only_source() { - let edit = edit_lock(LOCK_DIRECT, "rack", "3.2.6", ©_rel()).unwrap(); - let (original, new) = lock_record_lines(&edit); - let mirror = |lock: &str| lock.replace("https://rubygems.org/", "https://mirror.example/"); - for hosted in [false, true] { - let shape = |lock: &str| { - let lock = mirror(lock); - if hosted { - mirror(&with_puma_hosted( - &lock.replace("https://mirror.example/", "https://rubygems.org/"), - "https://patch.socket.dev", - )) - } else { - lock - } - }; - let want = shape(LOCK_DIRECT); - assert_eq!( - revert_lock_text(&shape(&edit.text), &original, &new).as_deref(), - Some(want.as_str()), - "hosted={hosted}" - ); - assert!(lock_record_converged(&want, &original, &new)); - } + assert_eq!(revert_lock_text(&hosted_only, &original, &new), None); } /// #2090: a GEM section that merges a patch registry remote with an /// upstream one (the bundler < 2.2 lock layout) still holds upstream - /// specs, so it is where a block with no recorded section goes back. + /// specs, so it is where an unrecorded block goes back. #[test] fn revert_lock_text_keeps_a_merged_gem_section() { let edit = edit_lock(LOCK_DIRECT, "rack", "3.2.6", ©_rel()).unwrap(); - let (recorded, new) = lock_record_lines(&edit); - let original = before_2090(&recorded); + let (original, new) = lock_record_lines(&edit); let merge = |lock: &str| { lock.replacen( " remote: https://rubygems.org/\n", @@ -10255,9 +10145,30 @@ mod tests { ); } - /// #2090: a spec lifted from a hosted gem's section records that - /// section like any other, so revert puts it back there and not into - /// the upstream section. + /// #2090: only a gem patch registry marks a hosted gem's section. An + /// upstream source whose URL merely has another Socket path shape is + /// the user's own, and an unrecorded block goes back into it. + #[test] + fn revert_lock_text_keeps_a_section_that_is_not_a_gem_patch_registry() { + let edit = edit_lock(LOCK_DIRECT, "rack", "3.2.6", ©_rel()).unwrap(); + let (original, new) = lock_record_lines(&edit); + let mirror = |lock: &str| { + lock.replacen( + "https://rubygems.org/", + "https://mirror.example/patch/gem/2fc039c8-b97d-545c-b447-879e2b468dfe/", + 1, + ) + }; + assert_eq!( + revert_lock_text(&mirror(&edit.text), &original, &new), + Some(mirror(LOCK_DIRECT)) + ); + } + + /// #2090: record and revert agree on what "no recorded section" means. + /// A spec lifted from a hosted gem's section records that section even + /// when it is the lock's first, so revert puts it back there and not + /// into the first upstream section. #[test] fn edit_lock_records_a_hosted_source_section() { let lock = with_puma_hosted(LOCK_DIRECT, "https://patch.socket.dev");