Skip to content

Commit 761edf7

Browse files
author
Bryan C. Mills
committed
cmd/internal/moddeps: use a temporary directory for GOMODCACHE if needed
CL 328770 should be sufficient to fix the specific failure in the report, but when attempting to reproduce it I noticed a related failure mode, triggered by the environment variables set in src/run.bash. The failure mode is currently masked on the Go project builders due to the lack of any 'longtest' builder running as a non-root user (#10719). It is also masked from Go contributors running 'run.bash' locally because 'run.bash' does not actually run all of the tests unless GO_TEST_SHORT=0 is set in the environment (#29266, #46054). Fixes #46695 Change-Id: I272c09dae462734590dce59b3d3c5b6d3f733c92 Reviewed-on: https://go-review.googlesource.com/c/go/+/328771 Trust: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent a040042 commit 761edf7

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/cmd/internal/moddeps/moddeps_test.go

+31-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package moddeps_test
66

77
import (
8+
"bytes"
89
"encoding/json"
910
"fmt"
1011
"internal/testenv"
@@ -123,10 +124,38 @@ func TestAllDependencies(t *testing.T) {
123124
t.Skip("skipping because a diff command with support for --recursive and --unified flags is unavailable")
124125
}
125126

127+
// We're going to check the standard modules for tidiness, so we need a usable
128+
// GOMODCACHE. If the default directory doesn't exist, use a temporary
129+
// directory instead. (That can occur, for example, when running under
130+
// run.bash with GO_TEST_SHORT=0: run.bash sets GOPATH=/nonexist-gopath, and
131+
// GO_TEST_SHORT=0 causes it to run this portion of the test.)
132+
var modcacheEnv []string
133+
{
134+
out, err := exec.Command(goBin, "env", "GOMODCACHE").Output()
135+
if err != nil {
136+
t.Fatalf("%s env GOMODCACHE: %v", goBin, err)
137+
}
138+
modcacheOk := false
139+
if gomodcache := string(bytes.TrimSpace(out)); gomodcache != "" {
140+
if _, err := os.Stat(gomodcache); err == nil {
141+
modcacheOk = true
142+
}
143+
}
144+
if !modcacheOk {
145+
modcacheEnv = []string{
146+
"GOMODCACHE=" + t.TempDir(),
147+
"GOFLAGS=" + os.Getenv("GOFLAGS") + " -modcacherw", // Allow t.TempDir() to clean up subdirectories.
148+
}
149+
}
150+
}
151+
126152
// Build the bundle binary at the golang.org/x/tools
127153
// module version specified in GOROOT/src/cmd/go.mod.
128154
bundleDir := t.TempDir()
129-
r := runner{Dir: filepath.Join(runtime.GOROOT(), "src/cmd")}
155+
r := runner{
156+
Dir: filepath.Join(runtime.GOROOT(), "src/cmd"),
157+
Env: append(os.Environ(), modcacheEnv...),
158+
}
130159
r.run(t, goBin, "build", "-mod=readonly", "-o", bundleDir, "golang.org/x/tools/cmd/bundle")
131160

132161
var gorootCopyDir string
@@ -160,7 +189,7 @@ func TestAllDependencies(t *testing.T) {
160189
}
161190
r := runner{
162191
Dir: filepath.Join(gorootCopyDir, rel),
163-
Env: append(os.Environ(),
192+
Env: append(append(os.Environ(), modcacheEnv...),
164193
// Set GOROOT.
165194
"GOROOT="+gorootCopyDir,
166195
// Explicitly override PWD and clear GOROOT_FINAL so that GOROOT=gorootCopyDir is definitely used.

0 commit comments

Comments
 (0)