Skip to content

Commit 9bac91f

Browse files
Scrub ambient Go env from spawned test children (#2063)
A developer or runner that exports GOWORK, GOFLAGS=-modfile=..., GOMODCACHE or GOPATH (or sets them with `go env -w`) leaks them into every socket-patch and `go` child the CLI tests spawn. With GOWORK=<file> exported, every `go mod download` in the Go suites' staging fails; with GOFLAGS=-modfile=... Go vendor refuses to write its replace. e2e_golang_build failed 21/21 and e2e_vendor_golang_build 11/13 that way on a clean checkout. Add hermetic::Extra::Go (remove GOFLAGS, GOWORK, GOPATH, GOMODCACHE; set GOENV=off) and apply it to CRAWLER_EXTRAS, to every child of common::run_bin_with_env, and to the go/socket-patch spawn helpers of e2e_golang_build, e2e_golang_hosted_build and e2e_vendor_golang_build. Tests that need a value still set it after the scrub. A spawn_env_hygiene self-test pins the scrub. Part of #1981 (items 1-2). Claude-Session: https://claude.ai/code/session_011f9aeR7CFpkqZDdd9hfYmM Co-authored-by: Claude <noreply@anthropic.com>
1 parent d3f9ba9 commit 9bac91f

9 files changed

Lines changed: 69 additions & 10 deletions

File tree

‎crates/socket-patch-cli/tests/common/hermetic.rs‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,24 @@ pub enum Extra {
267267
/// fixture's `vendor/`. The pin outranks `XDG_CONFIG_HOME` and holds
268268
/// no config. Run it before the suite sets its own `COMPOSER_*` values.
269269
Composer,
270+
/// `GOFLAGS`, `GOWORK`, `GOPATH` and `GOMODCACHE` removed, then
271+
/// `GOENV=off`. The go crawler finds installed modules through
272+
/// `GOMODCACHE`/`GOPATH`, and Go apply, `vex` and `vendor` honor
273+
/// `GOFLAGS=-modfile` and `GOWORK` (`utils::go_env`), so an ambient
274+
/// value moves the crawl off the fixture or turns a replace into a
275+
/// refusal. `GOENV=off` stops the same keys arriving from the user's
276+
/// `go env -w` file. Tests that need a value set it after the scrub.
277+
Go,
270278
}
271279

272280
/// The [`Extra`] scrubs for a `socket-patch` child that crawls a real
273281
/// project tree: the binary's own crawlers read `VIRTUAL_ENV`, bundler's
274-
/// `BUNDLE_PATH`/`BUNDLE_APP_CONFIG`/`BUNDLE_GEMFILE` and Composer's vendor
275-
/// dir and home, so an ambient value moves discovery (and the installed
276-
/// evidence `vex` hashes) off the fixture. Any env the test sets after the
277-
/// scrub still wins. Apply it with [`scrub_crawler_inputs`].
278-
pub const CRAWLER_EXTRAS: [Extra; 3] = [Extra::Venv, Extra::Bundle, Extra::Composer];
282+
/// `BUNDLE_PATH`/`BUNDLE_APP_CONFIG`/`BUNDLE_GEMFILE`, Composer's vendor
283+
/// dir and home, and Go's module cache and flags, so an ambient value
284+
/// moves discovery (and the installed evidence `vex` hashes) off the
285+
/// fixture. Any env the test sets after the scrub still wins. Apply it
286+
/// with [`scrub_crawler_inputs`].
287+
pub const CRAWLER_EXTRAS: [Extra; 4] = [Extra::Venv, Extra::Bundle, Extra::Composer, Extra::Go];
279288

280289
/// Where [`scrub_crawler_inputs`] points `BUNDLE_USER_CONFIG`.
281290
pub const NO_BUNDLE_USER_CONFIG: &str = "/nonexistent/bundle-user-config";
@@ -365,6 +374,12 @@ pub fn scrub_extra(cmd: &mut Command, extras: &[Extra]) {
365374
}
366375
cmd.env("COMPOSER_HOME", "/nonexistent/composer-home");
367376
}
377+
Extra::Go => {
378+
for key in ["GOFLAGS", "GOWORK", "GOPATH", "GOMODCACHE"] {
379+
cmd.env_remove(key);
380+
}
381+
cmd.env("GOENV", "off");
382+
}
368383
}
369384
}
370385
}

‎crates/socket-patch-cli/tests/common/mod.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ pub fn run_bin_with_env(
107107
// would otherwise read the developer's `~/.gradle` / `~/.m2` (and any
108108
// `GRADLE_USER_HOME` / `GRADLE_RO_DEP_CACHE`) into every test.
109109
jvm_env::isolate_cli(&mut cmd);
110+
// No ambient Go module cache, flags or `go env -w` file either: an
111+
// exported `GOFLAGS=-modfile=...` or `GOMODCACHE` would move the go
112+
// crawl and Go apply off the fixture.
113+
hermetic::scrub_extra(&mut cmd, &[hermetic::Extra::Go]);
110114
// Caller-supplied env lands last so explicit injections (runtime
111115
// gates, discovery roots) survive the scrub.
112116
for (k, v) in env {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ fn run_socket(cwd: &Path, args: &[&str], modcache: &Path) -> (i32, String, Strin
8888
fn go(dir: &Path, args: &[&str], env: &[(&str, &str)]) -> std::process::Output {
8989
let mut cmd = Command::new("go");
9090
cmd.args(args).current_dir(dir);
91+
// No ambient `GOWORK`/`GOFLAGS`/`go env -w` file: an exported
92+
// `GOWORK=<file>` fails every `go mod download` in `stage`.
93+
common::hermetic::scrub_extra(&mut cmd, &[common::hermetic::Extra::Go]);
9194
cache_env::isolate(&mut cmd);
9295
for (k, v) in env {
9396
cmd.env(k, v);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ impl Machine {
9797
fn go(dir: &Path, machine: &Machine, args: &[&str], env: &[(&str, &str)]) -> std::process::Output {
9898
let mut cmd = Command::new("go");
9999
cmd.args(args).current_dir(dir);
100+
// No ambient `GOWORK`/`GOFLAGS`: an exported `GOWORK=<file>` fails
101+
// every staging `go` command.
102+
common::hermetic::scrub_extra(&mut cmd, &[common::hermetic::Extra::Go]);
100103
cache_env::isolate(&mut cmd);
101104
cmd.env("GOMODCACHE", &machine.modcache);
102105
cmd.env("GOCACHE", &machine.gocache);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ fn has_command(cmd: &str) -> bool {
305305
/// immediately instead of letting a developer's ambient shell decide what got
306306
/// tested. The crawler's own ambient inputs are scrubbed too
307307
/// (`hermetic::CRAWLER_EXTRAS`: an activated venv, `BUNDLE_*`,
308-
/// `COMPOSER_*`): the hosted scan discovers packages unscoped under the cwd.
308+
/// `COMPOSER_*`, `GOFLAGS`/`GOMODCACHE` and friends): the hosted scan discovers packages unscoped under the cwd.
309309
fn run(cwd: &Path, args: &[&str]) -> (i32, String, String) {
310310
let mut cmd = hermetic::binary_command();
311311
hermetic::scrub_crawler_inputs(&mut cmd);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const PATCHED_LIB: &str = "package upstream\n\nfunc Greeting() string { return \
6464
/// through it).
6565
fn run_socket(cwd: &Path, args: &[&str], modcache: &Path) -> (i32, String, String) {
6666
let mut cmd = hermetic_command(&binary());
67-
hermetic::scrub_extra(&mut cmd, &[hermetic::Extra::Venv]);
67+
hermetic::scrub_extra(&mut cmd, &[hermetic::Extra::Venv, hermetic::Extra::Go]);
6868
cmd.current_dir(cwd);
6969
cmd.env("GOMODCACHE", modcache);
7070
let _fixture = prebuilt_common::prepare_command(
@@ -103,6 +103,9 @@ fn go_env<'a>(modcache: &'a str, proxy: &'a str) -> Vec<(&'a str, &'a str)> {
103103
fn go(dir: &Path, args: &[&str], env: &[(&str, &str)]) -> Output {
104104
let mut cmd = Command::new("go");
105105
cmd.args(args).current_dir(dir);
106+
// No ambient `GOWORK`/`GOFLAGS`/`go env -w` file: an exported
107+
// `GOWORK=<file>` fails every `go mod download` in `stage`.
108+
hermetic::scrub_extra(&mut cmd, &[hermetic::Extra::Go]);
106109
cache_env::isolate(&mut cmd);
107110
for (k, v) in env {
108111
cmd.env(k, v);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ fn has_corepack_pm(pm: &str) -> bool {
325325
/// test process and fails if one reaches the child), and forces
326326
/// `SOCKET_NO_CONFIG=1` to block the socket-cli
327327
/// config.json. The crawler's own ambient inputs are scrubbed too
328-
/// (`hermetic::CRAWLER_EXTRAS`: an activated venv, `BUNDLE_*`, `COMPOSER_*`),
329-
/// so an ambient BUNDLE_PATH can't move the gem leg's crawl off
328+
/// (`hermetic::CRAWLER_EXTRAS`: an activated venv, `BUNDLE_*`, `COMPOSER_*`,
329+
/// `GOFLAGS`/`GOMODCACHE` and friends), so an ambient BUNDLE_PATH can't move the gem leg's crawl off
330330
/// `vendor/bundle`.
331331
fn run_socket(cwd: &Path, args: &[&str]) -> (i32, String, String) {
332332
let mut cmd = hermetic::binary_command();

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,36 @@ fn crawler_inputs_sweep_bundler_and_composer_overrides() {
708708
);
709709
}
710710

711+
#[test]
712+
#[serial]
713+
fn crawler_inputs_sweep_go_module_cache_and_flags() {
714+
with_ambient(
715+
&[
716+
("GOFLAGS", "-modfile=/ambient/alt.mod"),
717+
("GOWORK", "/ambient/go.work"),
718+
("GOPATH", "/ambient/gopath"),
719+
("GOMODCACHE", "/ambient/modcache"),
720+
("GOENV", "/ambient/go-env"),
721+
],
722+
|| {
723+
let mut cmd = hermetic::binary_command();
724+
hermetic::scrub_crawler_inputs(&mut cmd);
725+
let env = effective_env(&cmd);
726+
for key in ["GOFLAGS", "GOWORK", "GOPATH", "GOMODCACHE"] {
727+
assert_eq!(get(&env, key), None, "{key}");
728+
}
729+
// The user's `go env -w` file is switched off, not left to
730+
// supply the same keys.
731+
assert_eq!(get(&env, "GOENV"), Some("off"));
732+
733+
// A test's own pin, set after the scrub, survives.
734+
cmd.env("GOMODCACHE", "/fixture/modcache");
735+
let env = effective_env(&cmd);
736+
assert_eq!(get(&env, "GOMODCACHE"), Some("/fixture/modcache"));
737+
},
738+
);
739+
}
740+
711741
#[test]
712742
fn no_private_bundler_sweeps() {
713743
let found: Vec<String> = test_sources()

‎crates/socket-patch-cli/tests/vex_e2e_common/mod.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
//! - Every run is hermetic: ambient `SOCKET_*` is scrubbed, telemetry and
5353
//! the socket-cli config are off, ambient API tokens are vetoed (only
5454
//! [`VexRun::api_token`] authenticates) and the crawler's ambient inputs
55-
//! (`VIRTUAL_ENV`, `BUNDLE_*`, `COMPOSER_*`; `hermetic::CRAWLER_EXTRAS`)
55+
//! (`VIRTUAL_ENV`, `BUNDLE_*`, `COMPOSER_*`, Go's `GO*` module/flag vars;
56+
//! `hermetic::CRAWLER_EXTRAS`)
5657
//! are removed. Package-manager homes/caches the installed-tree lookups need go in
5758
//! [`VexRun::envs`].
5859
//! - `--json` is the default ([`VexRun::human`] opts out), so

0 commit comments

Comments
 (0)