Skip to content

Commit 3b14afc

Browse files
Fix vendored gem revert into a hosted GEM section (#2090) (#2102)
* 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 * 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 * 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 * 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 --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 610ba8d commit 3b14afc

2 files changed

Lines changed: 384 additions & 13 deletions

File tree

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

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,6 +3058,202 @@ async fn unwind_after_superseding_rescan(command: &str) {
30583058
);
30593059
}
30603060

3061+
/// #2090: one gem vendored, then another hosted. The vendored gem's lock
3062+
/// record names no `GEM` section (its spec came out of the only one), and
3063+
/// the hosted gem's patch-registry section sorts ahead of the upstream one.
3064+
/// Unwinding the vendored gem used to put its spec into the hosted gem's
3065+
/// section: a fresh `bundle install` then looked for it on the patch
3066+
/// registry and failed, and the hosted gem could no longer be unwound.
3067+
#[tokio::test(flavor = "multi_thread")]
3068+
#[ignore = "host capstone: shells out to a real ruby/gem/bundler; \
3069+
the unpinned `test` job skips it, an e2e job with a pinned toolchain runs it via --ignored"]
3070+
async fn gem_vendored_unwind_beside_a_hosted_gem_restores_the_upstream_section() {
3071+
for command in ["rollback", "remove"] {
3072+
vendored_unwind_beside_a_hosted_gem(command).await;
3073+
}
3074+
}
3075+
3076+
async fn vendored_unwind_beside_a_hosted_gem(command: &str) {
3077+
let Some(fx) = redirect_scanned_project(
3078+
&format!("vendored-beside-hosted-{command}"),
3079+
Spelling::Gemfile,
3080+
false,
3081+
true,
3082+
None,
3083+
Driver::ScanVex,
3084+
)
3085+
.await
3086+
else {
3087+
return;
3088+
};
3089+
// Bundler < 2.2 merges every remote into one GEM section, so no hosted
3090+
// section can sort ahead of the upstream one there.
3091+
if !fx.bundler.at_least(2, 2) {
3092+
println!(
3093+
"SKIP e2e_redirect_gem_build (vendored-beside-hosted-{command}): bundler {}: \
3094+
per-source GEM sections need bundler >= 2.2",
3095+
fx.bundler.version
3096+
);
3097+
return;
3098+
}
3099+
const VULN_GEN2: &str = "10000000-1a2b-4a1b-8c2d-3e4f5a6b7c8d";
3100+
const TINY_GEN2: &str = "80000000-1a2b-4a1b-8c2d-3e4f5a6b7c8d";
3101+
let tiny_purl = format!("pkg:gem/{TRANSITIVE}@1.0.0");
3102+
let api = fx._server.uri();
3103+
let upstream = format!("{api}/upstream");
3104+
let cwd = fx.proj.to_str().expect("utf8 tmp path");
3105+
let gemfile_path = fx.proj.join(fx.gemfile_name);
3106+
let lock_path = fx.proj.join(fx.lock_name);
3107+
let socket = |what: &str, head: &[&str]| {
3108+
let mut argv = head.to_vec();
3109+
argv.extend([
3110+
"--json",
3111+
"--cwd",
3112+
cwd,
3113+
"--api-url",
3114+
&api,
3115+
"--org",
3116+
ORG,
3117+
"--api-token",
3118+
"fake",
3119+
"--patch-server-url",
3120+
&api,
3121+
]);
3122+
let (code, stdout, stderr) =
3123+
run_socket_env(&fx.proj, &argv, &[("SOCKET_RUBYGEMS_URL", &upstream)]);
3124+
assert_eq!(
3125+
code, 0,
3126+
"{command}: {what}:\nstdout:\n{stdout}\nstderr:\n{stderr}"
3127+
);
3128+
stdout
3129+
};
3130+
let install = |what: &str, dir: &Path, frozen: bool| {
3131+
let envs: &[(&str, &str)] = if frozen {
3132+
&[("BUNDLE_FROZEN", "true")]
3133+
} else {
3134+
&[]
3135+
};
3136+
let out = bundle_env(dir, &["install"], envs);
3137+
assert!(
3138+
out.status.success(),
3139+
"{command}: {what}: bundle install:\nstdout:\n{}\nstderr:\n{}",
3140+
String::from_utf8_lossy(&out.stdout),
3141+
String::from_utf8_lossy(&out.stderr)
3142+
);
3143+
};
3144+
let assert_pristine = |what: &str| {
3145+
assert_eq!(
3146+
std::fs::read_to_string(&gemfile_path).unwrap(),
3147+
String::from_utf8_lossy(&fx.pristine_gemfile),
3148+
"{command}: {what}: the Gemfile must come back byte for byte"
3149+
);
3150+
assert_eq!(
3151+
std::fs::read_to_string(&lock_path).unwrap(),
3152+
String::from_utf8_lossy(&fx.pristine_lock),
3153+
"{command}: {what}: the lock must come back byte for byte"
3154+
);
3155+
};
3156+
3157+
// Back to the upstream pair: the fixture's own hosted scan is not part
3158+
// of this scenario.
3159+
socket("unwind the fixture scan", &["rollback", PURL]);
3160+
assert_pristine("fixture unwind");
3161+
install("upstream pair", &fx.proj, false);
3162+
3163+
// The first patch generation covers the direct gem only; vendor it.
3164+
let stage = fx.tmp.path().join("generation-stage");
3165+
let vuln_gem = build_gem(
3166+
&stage.join(VULN_GEN2),
3167+
DEP,
3168+
DEP_VERSION,
3169+
"vuln_gem.rb",
3170+
&patched_lib(),
3171+
&[TRANSITIVE],
3172+
);
3173+
let vuln = || GenerationGem {
3174+
name: DEP,
3175+
uuid: VULN_GEN2,
3176+
deps: vec![format!("{TRANSITIVE}:>= 0")],
3177+
lib_file: "vuln_gem.rb",
3178+
orig: orig_lib(),
3179+
patched: patched_lib(),
3180+
gem: vuln_gem.clone(),
3181+
};
3182+
let tiny_patched = TINY_LIB.replace("tiny-ok", "tiny-patched");
3183+
let tiny = GenerationGem {
3184+
name: TRANSITIVE,
3185+
uuid: TINY_GEN2,
3186+
deps: vec![],
3187+
lib_file: "tiny_dep.rb",
3188+
orig: TINY_LIB.to_string(),
3189+
gem: build_gem(
3190+
&stage.join(TINY_GEN2),
3191+
TRANSITIVE,
3192+
"1.0.0",
3193+
"tiny_dep.rb",
3194+
&tiny_patched,
3195+
&[],
3196+
),
3197+
patched: tiny_patched,
3198+
};
3199+
mount_patch_generation(&fx._server, 2, &[vuln()]).await;
3200+
socket("vendored scan", &["scan", "--mode", "vendored", "--yes"]);
3201+
let lock = std::fs::read_to_string(&lock_path).unwrap();
3202+
assert!(
3203+
lock.starts_with(&format!(
3204+
"PATH\n remote: .socket/vendor/gem/{VULN_GEN2}/{DEP}-{DEP_VERSION}\n"
3205+
)),
3206+
"{command}: {DEP} must be vendored:\n{lock}"
3207+
);
3208+
install("vendored pair", &fx.proj, false);
3209+
3210+
// The next generation also patches the transitive gem. A hosted scan
3211+
// leaves the vendored gem alone and hosts the other one, whose section
3212+
// bundler sorts ahead of the upstream one.
3213+
mount_patch_generation(&fx._server, 1, &[vuln(), tiny]).await;
3214+
socket("hosted scan", &["scan", "--mode", "hosted", "--yes"]);
3215+
install("vendored and hosted pair", &fx.proj, false);
3216+
let lock = std::fs::read_to_string(&lock_path).unwrap();
3217+
let hosted_remote = format!("{api}/patch-registry/gem/{TOKEN}/{TINY_GEN2}/");
3218+
let remotes = gem_remotes(&lock);
3219+
assert_eq!(
3220+
remotes.first().copied(),
3221+
Some(hosted_remote.as_str()),
3222+
"{command}: the hosted section must be the lock's first GEM section:\n{lock}"
3223+
);
3224+
assert!(
3225+
lock.contains(&format!("{VULN_GEN2}/{DEP}-{DEP_VERSION}\n")),
3226+
"{command}: {DEP} must still be vendored:\n{lock}"
3227+
);
3228+
3229+
if command == "remove" {
3230+
// Un-vendoring one gem keeps the other hosted, and a cold checkout
3231+
// of that pair still installs.
3232+
socket("remove the vendored gem", &["remove", PURL, "--yes"]);
3233+
let lock = std::fs::read_to_string(&lock_path).unwrap();
3234+
assert!(
3235+
lock.contains(&format!(
3236+
"GEM\n remote: {hosted_remote}\n specs:\n {TRANSITIVE} (1.0.0)\n\n\
3237+
GEM\n remote: {upstream}/\n specs:\n {DEP} ({DEP_VERSION})\n"
3238+
)),
3239+
"{command}: {DEP} must return to the upstream section:\n{lock}"
3240+
);
3241+
let fresh = stage_fresh_checkout(&fx, "one-gem-left-hosted");
3242+
install("cold checkout with one gem left hosted", &fresh, true);
3243+
assert_eq!(
3244+
std::fs::read_to_string(fresh.join(fx.lock_name)).unwrap(),
3245+
lock,
3246+
"{command}: the frozen install leaves the lock as is"
3247+
);
3248+
socket("remove the hosted gem", &["remove", &tiny_purl, "--yes"]);
3249+
} else {
3250+
socket("rollback both gems", &["rollback"]);
3251+
}
3252+
assert_pristine("unwind of both gems");
3253+
let fresh = stage_fresh_checkout(&fx, "unwound");
3254+
install("cold checkout of the unwound pair", &fresh, true);
3255+
}
3256+
30613257
/// Bundler's modern `gems.rb`/`gems.locked` spelling, end to end: the
30623258
/// candidate list must read the pair, the rewriter must key its edits to it,
30633259
/// and the real bundler must install the patched gem from the redirected

0 commit comments

Comments
 (0)