diff --git a/crates/socket-patch-cli/tests/e2e_vex_lockfile/golang.rs b/crates/socket-patch-cli/tests/e2e_vex_lockfile/golang.rs index bc3ca8375..93cfe4bf4 100644 --- a/crates/socket-patch-cli/tests/e2e_vex_lockfile/golang.rs +++ b/crates/socket-patch-cli/tests/e2e_vex_lockfile/golang.rs @@ -160,6 +160,11 @@ impl Fx { /// [`Self::vex`] with `--cwd ` (a workspace member, say). fn vex_in(&self, dir: &Path, extra: &[&str]) -> Run { + self.vex_with(dir, extra, None) + } + + /// [`Self::vex_in`] under `GOWORK=` when one is given. + fn vex_with(&self, dir: &Path, extra: &[&str], gowork: Option<&Path>) -> Run { let out_path = dir.join("out.vex.json"); let _ = std::fs::remove_file(&out_path); let mut args: Vec = [ @@ -176,7 +181,11 @@ impl Fx { .map(|s| s.to_string()) .collect(); args.extend(extra.iter().map(|s| s.to_string())); - let out = self.command().args(&args).output().expect("invoke vex"); + let mut cmd = self.command(); + if let Some(gowork) = gowork { + cmd.env("GOWORK", gowork); + } + let out = cmd.args(&args).output().expect("invoke vex"); let env: Value = serde_json::from_slice(&out.stdout).unwrap_or_else(|e| { panic!( "envelope JSON on stdout ({e}). stdout:\n{}\nstderr:\n{}", @@ -1091,6 +1100,99 @@ fn golang_go_work_replace_pinned_by_root_go_sum_attests() { assert_attested(&fx, &run, GO_PURL, U, "redirected", "go.work + go.sum pin"); } +/// Under a `GOWORK` that names another work file (`ci.work`) go keeps the +/// workspace's checksums in that file's name with `.sum` appended +/// (`ci.work.sum`), and neither reads nor writes a `go.work.sum` beside it +/// (#2439). `vex` judges the hosted pin by the same file, with the work +/// file in the project root and one directory above it: +/// +/// - a `go.work.sum` go cannot parse is not read, so the pin in `go.sum` +/// still attests; +/// - a `ci.work.sum` go cannot parse stops the build, so nothing attests; +/// - the zip line in `ci.work.sum` (where go writes it, and checks the +/// download against it) is the pin; +/// - the zip line in `go.work.sum` alone pins nothing (go builds whatever +/// the proxy serves and records it in `ci.work.sum`): `go.sum` holds +/// only the `/go.mod` line, which is never the basis in a workspace. +#[test] +fn golang_named_gowork_reads_its_own_sum_file() { + let m = go_socket_module(U); + let zip_line = format!("{m} {GO_SVER} {GO_H1_ZIP}\n"); + let mod_line = format!("{m} {GO_SVER}/go.mod {GO_H1_MOD}\n"); + let both = go_sum_lines(U); + let broken = "github.com/x/y v1.0.0\n"; + // (case, go.sum, go.work.sum, ci.work.sum, the skip reason or attested) + let rows: [(&str, &str, Option<&str>, Option<&str>, Option<&str>); 5] = [ + ("both lines in go.sum", &both, None, None, None), + ( + "a go.work.sum go cannot parse", + &both, + Some(broken), + None, + None, + ), + ( + "a ci.work.sum go cannot parse", + &both, + None, + Some(broken), + Some("package_not_found"), + ), + ( + "the zip line in ci.work.sum", + &mod_line, + None, + Some(&zip_line), + None, + ), + ( + "the zip line in go.work.sum", + &mod_line, + Some(&zip_line), + None, + Some("package_not_found"), + ), + ]; + let mut wrong = Vec::new(); + for (case, go_sum, work_sum, ci_sum, omitted) in rows { + for (work, use_dir, dir) in [("ci.work", ".", ""), ("../ci.work", "./app", "../")] { + let what = format!("{case}, GOWORK={work}"); + let fx = Fx::new(); + // The project lies in a checkout at its parent, so a work file + // one directory up is inside the repository. + fx.put("../.git/HEAD", "ref: refs/heads/main\n"); + write_go_hosted(&fx, U, GoFile::GoMod); + fx.put("go.sum", go_sum); + fx.put(work, format!("go 1.21\n\nuse {use_dir}\n")); + if let Some(text) = work_sum { + fx.put(&format!("{dir}go.work.sum"), text); + } + if let Some(text) = ci_sum { + fx.put(&format!("{dir}ci.work.sum"), text); + } + fx.cache_go_mod(GO_MODULE, GO_VERSION); + write_redirect_ledger(&fx, GO_PURL, go_record(U), GO_HOSTED_EDITS); + let gowork = fx.cwd.join(work); + let run = fx.vex_with(&fx.cwd, &["--offline"], Some(&gowork)); + // Every row that goes the wrong way is named at once; a row + // that goes the right way is then held to its whole shape. + let attested = run.code == Some(0); + if attested != omitted.is_none() { + wrong.push(format!( + "{what}: attested {attested}, want {}", + omitted.is_none() + )); + continue; + } + match omitted { + None => assert_attested(&fx, &run, GO_PURL, U, "redirected", &what), + Some(reason) => assert_omitted(&run, GO_PURL, reason, &what), + } + } + } + assert!(wrong.is_empty(), "{wrong:#?}"); +} + /// LIMITATION (documented, root-only discovery): a Socket replace in a /// workspace MEMBER's go.mod (e.g. `get --mode hosted --cwd tools`) is not /// read from the workspace root — vex there finds nothing (exit 2, no false diff --git a/crates/socket-patch-core/src/crawlers/go_crawler.rs b/crates/socket-patch-core/src/crawlers/go_crawler.rs index c37b225e0..9ac4b2564 100644 --- a/crates/socket-patch-core/src/crawlers/go_crawler.rs +++ b/crates/socket-patch-core/src/crawlers/go_crawler.rs @@ -369,9 +369,7 @@ async fn go_sum_scope(cwd: &Path) -> Option> { } replaces.extend(go_mod_edit::parse_replace_entries(go_mod)); } - let mut work_sum = ws.work_file.clone().into_os_string(); - work_sum.push(".sum"); - match read_sum(Path::new(&work_sum)).await { + match read_sum(&go_workspace::sum_file(&ws.work_file)).await { SumFile::Text(text) => scope.extend(zip_lines(&text)), SumFile::Absent => listed = false, SumFile::Unreadable => return None, diff --git a/crates/socket-patch-core/src/hosted/engine.rs b/crates/socket-patch-core/src/hosted/engine.rs index 524e2edf4..0b660b4bd 100644 --- a/crates/socket-patch-core/src/hosted/engine.rs +++ b/crates/socket-patch-core/src/hosted/engine.rs @@ -1499,7 +1499,7 @@ async fn read_go_workspace( let mut touched: Vec = root.ancestors().map(|dir| dir.join("go.work")).collect(); if let Loaded::Workspace(ws) = &loaded { - touched.push(ws.work_dir().join("go.work.sum")); + touched.push(go_workspace::sum_file(&ws.work_file)); for member in &ws.members { touched.push(member.dir.join("go.mod")); touched.push(member.dir.join("go.sum")); diff --git a/crates/socket-patch-core/src/vendor/go_workspace.rs b/crates/socket-patch-core/src/vendor/go_workspace.rs index a5bdce762..841b4fa5a 100644 --- a/crates/socket-patch-core/src/vendor/go_workspace.rs +++ b/crates/socket-patch-core/src/vendor/go_workspace.rs @@ -186,7 +186,7 @@ pub struct GoWorkspace { pub work_file: PathBuf, /// Its text, normalized for reading. pub work: String, - /// `go.work.sum`, when readable. + /// The workspace's sum file ([`sum_file`]), when readable. pub work_sum: Option, /// The `use` members, in go.work order. pub members: Vec, @@ -258,8 +258,8 @@ pub enum Loaded { Invalid(String), } -/// Load the workspace in effect for `project_root` from disk: go.work, -/// go.work.sum, and every member's go.mod and go.sum. +/// Load the workspace in effect for `project_root` from disk: the work +/// file, its sum file ([`sum_file`]), and every member's go.mod and go.sum. pub async fn load(project_root: &Path) -> Loaded { load_with( project_root, @@ -286,6 +286,16 @@ pub async fn load_with(project_root: &Path, gowork: Option<&str>) -> Loaded { } } +/// The sum file of the workspace `work_file` governs, as go names it: the +/// work file's name with `.sum` appended. That is `go.work.sum` for a +/// `go.work`, and `ci.work.sum` under `GOWORK=/repo/ci.work`, where go +/// neither reads nor writes a `go.work.sum` beside it. +pub fn sum_file(work_file: &Path) -> PathBuf { + let mut name = work_file.as_os_str().to_os_string(); + name.push(".sum"); + PathBuf::from(name) +} + /// Read `work_file` and what it names. A `use` member outside `bound` /// reads as one without a go.mod or go.sum (missing). async fn load_file(work_file: PathBuf, bound: &crate::utils::repo_root::ReadBoundary) -> Loaded { @@ -325,9 +335,7 @@ async fn load_file(work_file: PathBuf, bound: &crate::utils::repo_root::ReadBoun go_sum, }); } - let work_sum = read_regular_to_string(&work_dir.join("go.work.sum")) - .await - .ok(); + let work_sum = read_regular_to_string(&sum_file(&work_file)).await.ok(); Loaded::Workspace(GoWorkspace { work_file, work: text, diff --git a/crates/socket-patch-core/src/vex/discover/golang.rs b/crates/socket-patch-core/src/vex/discover/golang.rs index cc249f1fc..bcc2fc0ae 100644 --- a/crates/socket-patch-core/src/vex/discover/golang.rs +++ b/crates/socket-patch-core/src/vex/discover/golang.rs @@ -494,9 +494,10 @@ struct Workspace { /// is a member). sum_files: Vec, /// The other sum files go verifies against, as the loader read them: - /// the governing `go.work.sum` and out-of-root members' `go.sum`, when - /// they lie inside the repository. `(text, whether its zip lines show - /// the selection)`. + /// the governing work file's sum file ([`go_workspace::sum_file`]: an + /// ancestor's `go.work.sum`, or `ci.work.sum` for a `GOWORK` naming + /// `ci.work`) and out-of-root members' `go.sum`, when they lie inside + /// the repository. `(text, whether its zip lines show the selection)`. outside_sums: Vec<(String, bool)>, /// The project root the model's paths are matched against (empty in /// memory, where the model's paths are root-relative). @@ -630,8 +631,8 @@ async fn read_workspace( /// The workspace go resolves for the project at `root` on disk /// ([`go_workspace::load_with`]), with every member wherever it lies. /// -/// The loader already read go.work, go.work.sum and every member's go.mod -/// and go.sum; of those, only the in-root members' go.mod is read again, +/// The loader already read the work file, its sum file and every member's +/// go.mod and go.sum; of those, only the in-root members' go.mod is read again, /// through the view: its Socket identities must be recognized (rule 11) /// and a go.mod go cannot parse diagnosed, and a member's replaces become /// refs when the root has no go.mod. The project's sum files are read @@ -665,9 +666,11 @@ async fn read_loaded_workspace( sum_files.push(sum); } } - // Sum files outside the project but inside the repository: the - // governing go.work.sum when the go.work is not the root's, and each - // out-of-root member's go.sum. Nothing outside the repository counts. + // Sum files the view's list above does not name, inside the + // repository: the governing work file's sum file when that file is not + // the root's go.work (an ancestor's go.work.sum, or `ci.work.sum` for a + // `GOWORK` naming `ci.work`, in the root or not), and each out-of-root + // member's go.sum. Nothing outside the repository counts. let repo = crate::utils::repo_root::find_git_repo(root).map_or_else(|| root.into(), |r| r.root); let repo = same_dir_key(&repo); let inside = |path: &Path| same_dir_key(path).starts_with(&repo); @@ -3041,4 +3044,78 @@ mod tests { assert_overridden(&out, Some("never this directory's go.work"), "malformed"); assert_eq!(out.refs[0].source_file, std::path::PathBuf::from("go.work")); } + + /// Under a `GOWORK` that names `ci.work` the workspace's sum file is + /// `ci.work.sum`, as for go: it decides the pin, and a `go.work.sum` + /// beside it is not read. + #[tokio::test] + async fn a_named_gowork_reads_its_own_sum_file() { + let m = gopatch(UUID_A); + let sver = "v1.4.2-socketpatch.1"; + let broken = "github.com/x/y v1.0.0\n"; + let zip_line = format!("{m} {sver} {ZIP_H1}\n"); + let mod_line = format!("{m} {sver}/go.mod {MOD_H1}\n"); + let both = go_sum_for(UUID_A, sver); + let mut wrong = Vec::new(); + // (case, go.sum, go.work.sum, ci.work.sum, pinned) + for (case, sum, work_sum, ci_sum, pinned) in [ + ("both lines in go.sum", &both, None, None, true), + ( + "a go.work.sum go cannot parse", + &both, + Some(broken), + None, + true, + ), + ( + "a ci.work.sum go cannot parse", + &both, + None, + Some(broken), + false, + ), + ( + "the zip line in ci.work.sum", + &mod_line, + None, + Some(zip_line.as_str()), + true, + ), + ( + "the zip line in go.work.sum", + &mod_line, + Some(zip_line.as_str()), + None, + false, + ), + ] { + for (work, use_dir, dir) in [("ci.work", ".", ""), ("../ci.work", "./app", "../")] { + let p = project_in_repo(); + p.write( + "go.mod", + format!("{BASE_GO_MOD}\nreplace {MODULE} {VERSION} => {m} {sver}\n"), + ) + .write("go.sum", sum) + .write(work, format!("go 1.21\n\nuse {use_dir}\n")); + if let Some(text) = work_sum { + p.write(&format!("{dir}go.work.sum"), text); + } + if let Some(text) = ci_sum { + p.write(&format!("{dir}ci.work.sum"), text); + } + let gowork = p.root().join(work).to_string_lossy().into_owned(); + let ctx = p.ctx(); + let mut out = Discovery::default(); + super::extract_gowork(&ctx, &mut out, Some(&gowork)).await; + assert_eq!(out.refs.len(), 1, "{case}, GOWORK={work}"); + let got = out.refs[0].locked_integrity.is_some(); + if got != pinned { + wrong.push(format!( + "{case}, GOWORK={work}: pinned {got}, go says {pinned}" + )); + } + } + } + assert!(wrong.is_empty(), "{wrong:#?}"); + } }