From 20a92460d52553749d6b77a3c549225b3794d75e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Fri, 8 Mar 2024 11:17:38 +0000 Subject: [PATCH] all: use cmd.Environ rather than os.Environ Added in Go 1.19, this keeps os/exec's default environment logic, such as ensuring that $PWD is always set. --- bench_test.go | 10 +++++----- internal/linker/linker.go | 2 +- scripts/bench_literals.go | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bench_test.go b/bench_test.go index f8e5f960..7a9199e9 100644 --- a/bench_test.go +++ b/bench_test.go @@ -79,12 +79,12 @@ func BenchmarkBuild(b *testing.B) { garbleCache := filepath.Join(tdir, "garble-cache") qt.Assert(b, qt.IsNil(os.RemoveAll(garbleCache))) qt.Assert(b, qt.IsNil(os.Mkdir(garbleCache, 0o777))) - env := append(os.Environ(), + env := []string{ "RUN_GARBLE_MAIN=true", - "GOCACHE="+goCache, - "GARBLE_CACHE="+garbleCache, + "GOCACHE=" + goCache, + "GARBLE_CACHE=" + garbleCache, "GARBLE_WRITE_ALLOCS=true", - ) + } args := []string{"build", "-v", "-o=" + outputBin, sourceDir} for _, cached := range []bool{false, true} { @@ -95,7 +95,7 @@ func BenchmarkBuild(b *testing.B) { } cmd := exec.Command(os.Args[0], args...) - cmd.Env = env + cmd.Env = append(cmd.Environ(), env...) cmd.Dir = sourceDir cachedStart := time.Now() diff --git a/internal/linker/linker.go b/internal/linker/linker.go index dccba869..bc8fa82d 100644 --- a/internal/linker/linker.go +++ b/internal/linker/linker.go @@ -196,7 +196,7 @@ func buildLinker(workingDir string, overlay map[string]string, outputLinkPath st // go version -m ~/tip/pkg/tool/linux_amd64/link // // and which can be done from Go via debug/buildinfo.ReadFile. - cmd.Env = append(os.Environ(), + cmd.Env = append(cmd.Environ(), "GOENV=off", "GOOS=", "GOARCH=", "GOEXPERIMENT=", "GOFLAGS=", ) // Building cmd/link is possible from anywhere, but to avoid any possible side effects build in a temp directory diff --git a/scripts/bench_literals.go b/scripts/bench_literals.go index d75505bb..58d103ca 100644 --- a/scripts/bench_literals.go +++ b/scripts/bench_literals.go @@ -142,7 +142,7 @@ func main() { garbleBin := buildTestGarble(tdir) args := append([]string{"-seed", garbleSeed, "-literals", "test", "-bench"}, os.Args[1:]...) cmd := exec.Command(garbleBin, args...) - cmd.Env = append(os.Environ(), + cmd.Env = append(cmd.Environ(), // Explicitly specify package for obfuscation to avoid affecting testing package. "GOGARBLE="+moduleName, "GARBLE_TEST_LITERALS_OBFUSCATOR_MAP="+strings.Join(packageToObfuscatorIndex, ","),