Skip to content

Commit 3a552c5

Browse files
Fix vendored gem directories losing files git ignores (#2348) (#2451)
* Start fix for gitignored vendored gem files Refs #2348. Assisted-by: Claude Code:claude-fable-5-2 * Test that a vendored gem dir commits whole Four of the five cases fail on main: a file of a plain gem's vendored directory that an ignore rule matches is left out of the commit, a rule that ignores the vendored dir is not refused, a .gitignore the gem ships keeps its rules, and the uuid dir gets no git metadata. The fifth pins what repair does for a copy vendored before the fix. Refs #2348. Assisted-by: Claude Code:claude-fable-5-2 * Keep a vendored gem dir whole in the commit A gem vendored as its own directory got none of the ignore handling a gem vendored as platform gems has, so a project rule (the .rspec of GitHub's Rails.gitignore) or a .gitignore the gem ships left files of the copy out of the commit. Every fresh clone then failed vendor --check and vex with vendor_inventory_mismatch. Every vendored gem now gets the uuid dir's .gitignore and .gitattributes, has the .gitignore files it ships emptied in the copy, and is refused (vendor_artifact_gitignored) when git would still leave a file out. An in-sync re-run and repair put the pair back. Repair keeps a shipped .gitignore that the ledger inventory of an earlier release recorded, so such a copy still verifies. Refs #2348. Assisted-by: Claude Code:claude-fable-5-2 * Prove a vendored gem dir through a git clone A real-Bundler case commits and clones a project whose .gitignore has the .rspec rule of GitHub's Rails.gitignore, with a gem that ships that file (diff-lcs 1.5.1). The clone holds the whole copy, vendor --check passes there, and a frozen bundle install loads the patched gem. It fails against main's backend. The CLI contract and the ecosystem guide now describe the uuid dir's git metadata and the emptied .gitignore for every vendored gem. Refs #2348. Assisted-by: Claude Code:claude-fable-5-2 * Empty only a gem .gitignore that hides files Review findings on the vendored gem dir fix. A gem can ship a .gitignore as data (a template it copies into new projects). In a gem's own tree only a .gitignore whose rules match a file of the copy is emptied now; the rest stay byte for byte. Repair restores each one as the ledger inventory records it. A copy that an earlier release vendored keeps the gem's .gitignore as shipped. The in-sync run that writes the uuid dir's pair for it now warns vendor_gem_copy_gitignored when git would still leave a file out, and names git add -f. Repair writes the pair for a healthy copy too, as the docs say. The check for a rule that ignores the uuid dir moved behind the Bazel hub branch, so a gem committed as its archive is asked what it was asked before. Refs #2348. Assisted-by: Claude Code:claude-fable-5-2 * Read a shipped .gitignore the way git does The check for whether a .gitignore the gem ships hides a file asked about the file before its directories, so a negation looked like it re-included a file under an ignored directory. Git never looks inside such a directory. The copy then kept that .gitignore, and the fresh vendor refused the gem where it could have emptied the file. Directories are now asked top-down, first match settles it. Refs #2348. Assisted-by: Claude Code:claude-fable-5-2 --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 7beca2f commit 3a552c5

6 files changed

Lines changed: 995 additions & 105 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Large diffs are not rendered by default.

‎crates/socket-patch-cli/src/commands/vendored_backend/repair.rs‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,16 +510,18 @@ impl VendoredBackend<'_> {
510510
entry
511511
};
512512
let health = check_vendored_artifact(&common.cwd, &entry, &record).await;
513-
// An npm-family `<uuid>/.gitignore` and `.gitattributes` are not
514-
// part of the artifact: a missing, edited or older one is
515-
// simply rewritten, and git must still commit the artifact with
516-
// them. A broken artifact gets both after its redownload.
513+
// An npm-family or gem `<uuid>/.gitignore` and `.gitattributes`
514+
// are not part of the artifact: a missing, edited or older one
515+
// is simply rewritten, and git must still commit the artifact
516+
// with them. A broken artifact gets both after its redownload.
517517
if !common.dry_run
518518
&& (health == ArtifactHealth::Healthy || health.is_workspace_copy_issue())
519519
{
520-
if let Err(e) =
521-
vendor::redownload::restore_npm_uuid_metadata(&entry, &common.cwd).await
522-
{
520+
let metadata = async {
521+
vendor::redownload::restore_npm_uuid_metadata(&entry, &common.cwd).await?;
522+
vendor::redownload::restore_gem_uuid_metadata(&entry, &common.cwd).await
523+
};
524+
if let Err(e) = metadata.await {
523525
fail(
524526
env,
525527
common.json,

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

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4121,6 +4121,198 @@ async fn platform_gems_capstone(bundler: &bundler_e2e::Bundler, declared: &str)
41214121
assert!(!proj.join(".socket/vendor").exists());
41224122
}
41234123

4124+
// ── a vendored gem dir through a real commit and clone (#2348) ────────
4125+
4126+
/// `git <args>` in `cwd`, with an identity of its own and the machine's
4127+
/// git config kept out; panics on failure.
4128+
fn git(cwd: &Path, args: &[&str]) -> String {
4129+
let out = Command::new("git")
4130+
.current_dir(cwd)
4131+
.env("GIT_CONFIG_GLOBAL", "/dev/null")
4132+
.env("GIT_CONFIG_SYSTEM", "/dev/null")
4133+
.args(["-c", "user.name=e2e", "-c", "user.email=e2e@example.com"])
4134+
.args([
4135+
"-c",
4136+
"init.defaultBranch=main",
4137+
"-c",
4138+
"protocol.file.allow=always",
4139+
])
4140+
.args(args)
4141+
.output()
4142+
.expect("failed to run git");
4143+
assert!(
4144+
out.status.success(),
4145+
"git {args:?} failed:\n{}",
4146+
String::from_utf8_lossy(&out.stderr)
4147+
);
4148+
String::from_utf8_lossy(&out.stdout).into_owned()
4149+
}
4150+
4151+
/// A project whose `.gitignore` has a rule that matches a file the gem
4152+
/// ships: diff-lcs 1.5.1 ships `.rspec`, which GitHub's stock
4153+
/// `Rails.gitignore` names. The vendoring checkout holds the file either
4154+
/// way, so the proof is a real `git commit` and `git clone`: the clone
4155+
/// holds the whole copy, `vendor --check` passes there as it does in the
4156+
/// checkout that vendored, and a frozen `bundle install` loads the patched
4157+
/// gem.
4158+
#[test]
4159+
#[ignore = "host capstone: shells out to a real bundler >= 1.17; the unpinned `test` job \
4160+
skips it, the e2e job runs it with a pinned toolchain via --ignored"]
4161+
fn gem_vendor_file_a_project_rule_ignores_reaches_a_git_clone() {
4162+
const GEM: &str = "diff-lcs";
4163+
const VERSION: &str = "1.5.1";
4164+
const RB: &str = "lib/diff/lcs.rb";
4165+
// Lines of GitHub's `Rails.gitignore`, and the gem home of this test.
4166+
const RULES: &str = "*.rbc\n.rspec\n/log/*\n/coverage/\n/vendor/bundle\n";
4167+
let Some(bundler) = gate("gitignored-file") else {
4168+
return;
4169+
};
4170+
if Command::new("git").arg("--version").output().is_err() {
4171+
println!("SKIP e2e_vendor_gem_build: no git on PATH");
4172+
return;
4173+
}
4174+
4175+
let tmp = tempfile::tempdir().unwrap();
4176+
let proj = tmp.path().join("proj");
4177+
std::fs::create_dir_all(&proj).unwrap();
4178+
std::fs::write(
4179+
proj.join("Gemfile"),
4180+
format!("source \"https://rubygems.org\"\n\ngem \"{GEM}\", \"{VERSION}\"\n"),
4181+
)
4182+
.unwrap();
4183+
let path_config = argv(&bundler.config_local_args("path", "vendor/bundle"))
4184+
.iter()
4185+
.map(|arg| arg.to_string())
4186+
.collect::<Vec<_>>();
4187+
let config = bundle(&proj, &argv(&path_config), false);
4188+
let install = bundle(&proj, &["install"], false);
4189+
if !config.status.success() || !install.status.success() {
4190+
println!(
4191+
"SKIP e2e_vendor_gem_build: could not install {GEM} {VERSION} (registry \
4192+
unreachable?):\n{}",
4193+
String::from_utf8_lossy(&install.stderr)
4194+
);
4195+
return;
4196+
}
4197+
git(&proj, &["init", "-q"]);
4198+
std::fs::write(proj.join(".gitignore"), RULES).unwrap();
4199+
4200+
let mut ruby = Command::new("ruby");
4201+
ruby.args(["-e", "puts Gem.ruby_api_version"]);
4202+
cache_env::isolate(&mut ruby);
4203+
let api = ruby.output().expect("failed to run ruby");
4204+
let api = String::from_utf8_lossy(&api.stdout).trim().to_string();
4205+
let gem_dir = proj
4206+
.join("vendor/bundle/ruby")
4207+
.join(&api)
4208+
.join("gems")
4209+
.join(format!("{GEM}-{VERSION}"));
4210+
assert!(
4211+
gem_dir.join(".rspec").is_file(),
4212+
"{GEM} {VERSION} ships .rspec (test premise)"
4213+
);
4214+
let orig = std::fs::read(gem_dir.join(RB)).expect("the installed file");
4215+
let marker = format!(
4216+
"\nmodule Diff\n module LCS\n SOCKET_PATCH_VENDOR_E2E = \"{UUID}\"\n end\nend\n"
4217+
);
4218+
let patched: Vec<u8> = [orig.as_slice(), marker.as_bytes()].concat();
4219+
let purl = format!("pkg:gem/{GEM}@{VERSION}");
4220+
stage_patch_with_vuln(&proj, &purl, RB, &orig, &patched);
4221+
4222+
let cwd = proj.to_str().unwrap();
4223+
let (code, stdout, stderr) =
4224+
run_socket(&proj, &["vendor", "--json", "--offline", "--cwd", cwd]);
4225+
assert_eq!(
4226+
code, 0,
4227+
"vendor failed.\nstdout:\n{stdout}\nstderr:\n{stderr}"
4228+
);
4229+
let env = parse_json_envelope(&stdout);
4230+
assert_eq!(env["summary"]["applied"], 1, "one package vendored: {env}");
4231+
4232+
// What the vendoring checkout says: every file of the copy is one git
4233+
// commits, and the check passes.
4234+
let uuid_dir = format!(".socket/vendor/gem/{UUID}");
4235+
let copy_rel = format!("{uuid_dir}/{GEM}-{VERSION}");
4236+
assert_eq!(
4237+
std::fs::read_to_string(proj.join(&uuid_dir).join(".gitignore")).unwrap(),
4238+
"!*\n"
4239+
);
4240+
assert!(proj.join(&uuid_dir).join(".gitattributes").is_file());
4241+
let ignored = git(&proj, &["status", "--short", "--ignored", "--", ".socket"]);
4242+
assert!(
4243+
!ignored.lines().any(|line| line.starts_with("!!")),
4244+
"git ignores files under .socket/:\n{ignored}"
4245+
);
4246+
let (code, stdout, stderr) = run_socket(&proj, &["vendor", "--check", "--cwd", cwd]);
4247+
assert_eq!(
4248+
code, 0,
4249+
"check in the vendoring checkout:\n{stdout}\n{stderr}"
4250+
);
4251+
4252+
// Commit what the next steps name and clone it.
4253+
git(&proj, &["add", "-A"]);
4254+
git(&proj, &["commit", "-q", "-m", "vendor"]);
4255+
let clone = tmp.path().join("clone");
4256+
git(tmp.path(), &["clone", "-q", cwd, clone.to_str().unwrap()]);
4257+
assert!(
4258+
!clone.join("vendor").exists(),
4259+
"the clone must not carry an installed tree (test bug)"
4260+
);
4261+
assert!(
4262+
clone.join(&copy_rel).join(".rspec").is_file(),
4263+
"the clone lost the file the project's rule matches"
4264+
);
4265+
4266+
let clone_cwd = clone.to_str().unwrap();
4267+
let (code, stdout, stderr) = run_socket(&clone, &["vendor", "--check", "--cwd", clone_cwd]);
4268+
assert_eq!(
4269+
code, 0,
4270+
"vendor --check must pass in a fresh clone.\nstdout:\n{stdout}\nstderr:\n{stderr}"
4271+
);
4272+
assert!(
4273+
!format!("{stdout}{stderr}").contains("vendor_inventory_mismatch"),
4274+
"{stdout}\n{stderr}"
4275+
);
4276+
4277+
// The clone installs frozen from the vendored path and loads the
4278+
// patched file.
4279+
let config = bundle(&clone, &argv(&path_config), false);
4280+
assert!(config.status.success(), "bundle config in the clone");
4281+
let lock_committed = std::fs::read(clone.join("Gemfile.lock")).unwrap();
4282+
let ci = bundle(&clone, &["install"], true);
4283+
assert!(
4284+
ci.status.success(),
4285+
"frozen `bundle install` in the clone.\nstdout:\n{}\nstderr:\n{}",
4286+
String::from_utf8_lossy(&ci.stdout),
4287+
String::from_utf8_lossy(&ci.stderr)
4288+
);
4289+
assert_eq!(
4290+
std::fs::read(clone.join("Gemfile.lock")).unwrap(),
4291+
lock_committed
4292+
);
4293+
let probe = bundle(
4294+
&clone,
4295+
&[
4296+
"exec",
4297+
"ruby",
4298+
"-e",
4299+
"require \"diff/lcs\"\nputs Diff::LCS::SOCKET_PATCH_VENDOR_E2E\n\
4300+
puts $LOADED_FEATURES.grep(%r{/diff/lcs\\.rb\\z})",
4301+
],
4302+
false,
4303+
);
4304+
let probe_out = String::from_utf8_lossy(&probe.stdout).into_owned();
4305+
assert!(
4306+
probe.status.success() && probe_out.contains(UUID),
4307+
"the clone must load the patched gem.\nstdout:\n{probe_out}\nstderr:\n{}",
4308+
String::from_utf8_lossy(&probe.stderr)
4309+
);
4310+
assert!(
4311+
probe_out.contains(&format!("{copy_rel}/{RB}")),
4312+
"loaded from the vendored path:\n{probe_out}"
4313+
);
4314+
}
4315+
41244316
/// #2435: a gem vendored as platform gems without the patch of the ruby
41254317
/// gem (not offered to the run) is vendored through the patch of the first
41264318
/// platform the lock names, whose uuid names its directory. When that

0 commit comments

Comments
 (0)