Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions crates/socket-patch-cli/CLI_CONTRACT.md

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions crates/socket-patch-cli/src/commands/vendored_backend/repair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,16 +510,18 @@ impl VendoredBackend<'_> {
entry
};
let health = check_vendored_artifact(&common.cwd, &entry, &record).await;
// An npm-family `<uuid>/.gitignore` and `.gitattributes` are not
// part of the artifact: a missing, edited or older one is
// simply rewritten, and git must still commit the artifact with
// them. A broken artifact gets both after its redownload.
// An npm-family or gem `<uuid>/.gitignore` and `.gitattributes`
// are not part of the artifact: a missing, edited or older one
// is simply rewritten, and git must still commit the artifact
// with them. A broken artifact gets both after its redownload.
if !common.dry_run
&& (health == ArtifactHealth::Healthy || health.is_workspace_copy_issue())
{
if let Err(e) =
vendor::redownload::restore_npm_uuid_metadata(&entry, &common.cwd).await
{
let metadata = async {
vendor::redownload::restore_npm_uuid_metadata(&entry, &common.cwd).await?;
vendor::redownload::restore_gem_uuid_metadata(&entry, &common.cwd).await
};
if let Err(e) = metadata.await {
fail(
env,
common.json,
Expand Down
192 changes: 192 additions & 0 deletions crates/socket-patch-cli/tests/e2e_vendor_gem_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4121,6 +4121,198 @@ async fn platform_gems_capstone(bundler: &bundler_e2e::Bundler, declared: &str)
assert!(!proj.join(".socket/vendor").exists());
}

// ── a vendored gem dir through a real commit and clone (#2348) ────────

/// `git <args>` in `cwd`, with an identity of its own and the machine's
/// git config kept out; panics on failure.
fn git(cwd: &Path, args: &[&str]) -> String {
let out = Command::new("git")
.current_dir(cwd)
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env("GIT_CONFIG_SYSTEM", "/dev/null")
.args(["-c", "user.name=e2e", "-c", "user.email=e2e@example.com"])
.args([
"-c",
"init.defaultBranch=main",
"-c",
"protocol.file.allow=always",
])
.args(args)
.output()
.expect("failed to run git");
assert!(
out.status.success(),
"git {args:?} failed:\n{}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8_lossy(&out.stdout).into_owned()
}

/// A project whose `.gitignore` has a rule that matches a file the gem
/// ships: diff-lcs 1.5.1 ships `.rspec`, which GitHub's stock
/// `Rails.gitignore` names. The vendoring checkout holds the file either
/// way, so the proof is a real `git commit` and `git clone`: the clone
/// holds the whole copy, `vendor --check` passes there as it does in the
/// checkout that vendored, and a frozen `bundle install` loads the patched
/// gem.
#[test]
#[ignore = "host capstone: shells out to a real bundler >= 1.17; the unpinned `test` job \
skips it, the e2e job runs it with a pinned toolchain via --ignored"]
fn gem_vendor_file_a_project_rule_ignores_reaches_a_git_clone() {
const GEM: &str = "diff-lcs";
const VERSION: &str = "1.5.1";
const RB: &str = "lib/diff/lcs.rb";
// Lines of GitHub's `Rails.gitignore`, and the gem home of this test.
const RULES: &str = "*.rbc\n.rspec\n/log/*\n/coverage/\n/vendor/bundle\n";
let Some(bundler) = gate("gitignored-file") else {
return;
};
if Command::new("git").arg("--version").output().is_err() {
println!("SKIP e2e_vendor_gem_build: no git on PATH");
return;
}

let tmp = tempfile::tempdir().unwrap();
let proj = tmp.path().join("proj");
std::fs::create_dir_all(&proj).unwrap();
std::fs::write(
proj.join("Gemfile"),
format!("source \"https://rubygems.org\"\n\ngem \"{GEM}\", \"{VERSION}\"\n"),
)
.unwrap();
let path_config = argv(&bundler.config_local_args("path", "vendor/bundle"))
.iter()
.map(|arg| arg.to_string())
.collect::<Vec<_>>();
let config = bundle(&proj, &argv(&path_config), false);
let install = bundle(&proj, &["install"], false);
if !config.status.success() || !install.status.success() {
println!(
"SKIP e2e_vendor_gem_build: could not install {GEM} {VERSION} (registry \
unreachable?):\n{}",
String::from_utf8_lossy(&install.stderr)
);
return;
}
git(&proj, &["init", "-q"]);
std::fs::write(proj.join(".gitignore"), RULES).unwrap();

let mut ruby = Command::new("ruby");
ruby.args(["-e", "puts Gem.ruby_api_version"]);
cache_env::isolate(&mut ruby);
let api = ruby.output().expect("failed to run ruby");
let api = String::from_utf8_lossy(&api.stdout).trim().to_string();
let gem_dir = proj
.join("vendor/bundle/ruby")
.join(&api)
.join("gems")
.join(format!("{GEM}-{VERSION}"));
assert!(
gem_dir.join(".rspec").is_file(),
"{GEM} {VERSION} ships .rspec (test premise)"
);
let orig = std::fs::read(gem_dir.join(RB)).expect("the installed file");
let marker = format!(
"\nmodule Diff\n module LCS\n SOCKET_PATCH_VENDOR_E2E = \"{UUID}\"\n end\nend\n"
);
let patched: Vec<u8> = [orig.as_slice(), marker.as_bytes()].concat();
let purl = format!("pkg:gem/{GEM}@{VERSION}");
stage_patch_with_vuln(&proj, &purl, RB, &orig, &patched);

let cwd = proj.to_str().unwrap();
let (code, stdout, stderr) =
run_socket(&proj, &["vendor", "--json", "--offline", "--cwd", cwd]);
assert_eq!(
code, 0,
"vendor failed.\nstdout:\n{stdout}\nstderr:\n{stderr}"
);
let env = parse_json_envelope(&stdout);
assert_eq!(env["summary"]["applied"], 1, "one package vendored: {env}");

// What the vendoring checkout says: every file of the copy is one git
// commits, and the check passes.
let uuid_dir = format!(".socket/vendor/gem/{UUID}");
let copy_rel = format!("{uuid_dir}/{GEM}-{VERSION}");
assert_eq!(
std::fs::read_to_string(proj.join(&uuid_dir).join(".gitignore")).unwrap(),
"!*\n"
);
assert!(proj.join(&uuid_dir).join(".gitattributes").is_file());
let ignored = git(&proj, &["status", "--short", "--ignored", "--", ".socket"]);
assert!(
!ignored.lines().any(|line| line.starts_with("!!")),
"git ignores files under .socket/:\n{ignored}"
);
let (code, stdout, stderr) = run_socket(&proj, &["vendor", "--check", "--cwd", cwd]);
assert_eq!(
code, 0,
"check in the vendoring checkout:\n{stdout}\n{stderr}"
);

// Commit what the next steps name and clone it.
git(&proj, &["add", "-A"]);
git(&proj, &["commit", "-q", "-m", "vendor"]);
let clone = tmp.path().join("clone");
git(tmp.path(), &["clone", "-q", cwd, clone.to_str().unwrap()]);
assert!(
!clone.join("vendor").exists(),
"the clone must not carry an installed tree (test bug)"
);
assert!(
clone.join(&copy_rel).join(".rspec").is_file(),
"the clone lost the file the project's rule matches"
);

let clone_cwd = clone.to_str().unwrap();
let (code, stdout, stderr) = run_socket(&clone, &["vendor", "--check", "--cwd", clone_cwd]);
assert_eq!(
code, 0,
"vendor --check must pass in a fresh clone.\nstdout:\n{stdout}\nstderr:\n{stderr}"
);
assert!(
!format!("{stdout}{stderr}").contains("vendor_inventory_mismatch"),
"{stdout}\n{stderr}"
);

// The clone installs frozen from the vendored path and loads the
// patched file.
let config = bundle(&clone, &argv(&path_config), false);
assert!(config.status.success(), "bundle config in the clone");
let lock_committed = std::fs::read(clone.join("Gemfile.lock")).unwrap();
let ci = bundle(&clone, &["install"], true);
assert!(
ci.status.success(),
"frozen `bundle install` in the clone.\nstdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&ci.stdout),
String::from_utf8_lossy(&ci.stderr)
);
assert_eq!(
std::fs::read(clone.join("Gemfile.lock")).unwrap(),
lock_committed
);
let probe = bundle(
&clone,
&[
"exec",
"ruby",
"-e",
"require \"diff/lcs\"\nputs Diff::LCS::SOCKET_PATCH_VENDOR_E2E\n\
puts $LOADED_FEATURES.grep(%r{/diff/lcs\\.rb\\z})",
],
false,
);
let probe_out = String::from_utf8_lossy(&probe.stdout).into_owned();
assert!(
probe.status.success() && probe_out.contains(UUID),
"the clone must load the patched gem.\nstdout:\n{probe_out}\nstderr:\n{}",
String::from_utf8_lossy(&probe.stderr)
);
assert!(
probe_out.contains(&format!("{copy_rel}/{RB}")),
"loaded from the vendored path:\n{probe_out}"
);
}

/// #2435: a gem vendored as platform gems without the patch of the ruby
/// gem (not offered to the run) is vendored through the patch of the first
/// platform the lock names, whose uuid names its directory. When that
Expand Down
Loading
Loading