From 32ae33786eccde1672d4ba373c80e1bc282bfbf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Tue, 26 Sep 2023 11:44:07 +0100 Subject: [PATCH] testscript: propagate GORACE like we already do with GOCOVERDIR Do both in a loop to deduplicate code. While here, only set them if they aren't empty; this way we don't unnecessarily pollute Vars with entries such as `GOCOVERDIR=` when they don't do anything useful. --- testscript/testscript.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/testscript/testscript.go b/testscript/testscript.go index cc7ce45d..dd0ec285 100644 --- a/testscript/testscript.go +++ b/testscript/testscript.go @@ -444,16 +444,27 @@ func (ts *TestScript) setup() string { "/=" + string(os.PathSeparator), ":=" + string(os.PathListSeparator), "$=$", - - // If we are collecting coverage profiles for merging into the main one, - // ensure the environment variable is forwarded to sub-processes. - "GOCOVERDIR=" + os.Getenv("GOCOVERDIR"), }, WorkDir: ts.workdir, Values: make(map[interface{}]interface{}), Cd: ts.workdir, ts: ts, } + + // These env vars affect how a Go program behaves at run-time; + // If the user or `go test` wrapper set them, we should propagate them + // so that sub-process commands run via the test binary see them as well. + for _, name := range []string{ + // If we are collecting coverage profiles, e.g. `go test -coverprofile`. + "GOCOVERDIR", + // If the user set GORACE when running a command like `go test -race`, + // such as GORACE=atexit_sleep_ms=10 to avoid the default 1s sleeps. + "GORACE", + } { + if val := os.Getenv(name); val != "" { + env.Vars = append(env.Vars, name+"="+val) + } + } // Must preserve SYSTEMROOT on Windows: https://github.com/golang/go/issues/25513 et al if runtime.GOOS == "windows" { env.Vars = append(env.Vars,