cmd: ensure that GOPATH is always valid in subprocesses that execute 'go build'

GOPATH/pkg contains, among other things, the module cache (and associated lockfiles).

Fixes #30776

Change-Id: I305cb3c0daab8cedd2e6ad235d4733f66af18723
Reviewed-on: https://go-review.googlesource.com/c/go/+/167082
Reviewed-by: Jay Conrod <jayconrod@google.com>
This commit is contained in:
Bryan C. Mills 2019-03-12 14:43:22 -04:00
parent 49448badb6
commit 334e750917
4 changed files with 7 additions and 3 deletions

View file

@ -161,7 +161,8 @@ TEXT ·x(SB),0,$0
`)
cmd := exec.Command(testenv.GoToolPath(t), "build")
cmd.Dir = tmpdir
cmd.Env = append(os.Environ(), []string{"GOARCH=amd64", "GOOS=linux"}...)
cmd.Env = append(os.Environ(),
"GOARCH=amd64", "GOOS=linux", "GOPATH="+filepath.Join(tmpdir, "_gopath"))
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected build to fail, but it succeeded")