]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/moddeps: use a temporary directory for GOMODCACHE if needed
authorBryan C. Mills <bcmills@google.com>
Wed, 16 Jun 2021 20:33:29 +0000 (16:33 -0400)
committerBryan C. Mills <bcmills@google.com>
Mon, 21 Jun 2021 20:53:11 +0000 (20:53 +0000)
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 <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
src/cmd/internal/moddeps/moddeps_test.go

index 8d01b913c3784922ddf85201e677ff74389d41ab..56c3b2585c7fdc211a0c4e17827bf182eb938661 100644 (file)
@@ -5,6 +5,7 @@
 package moddeps_test
 
 import (
+       "bytes"
        "encoding/json"
        "fmt"
        "internal/testenv"
@@ -123,10 +124,38 @@ func TestAllDependencies(t *testing.T) {
                t.Skip("skipping because a diff command with support for --recursive and --unified flags is unavailable")
        }
 
+       // We're going to check the standard modules for tidiness, so we need a usable
+       // GOMODCACHE. If the default directory doesn't exist, use a temporary
+       // directory instead. (That can occur, for example, when running under
+       // run.bash with GO_TEST_SHORT=0: run.bash sets GOPATH=/nonexist-gopath, and
+       // GO_TEST_SHORT=0 causes it to run this portion of the test.)
+       var modcacheEnv []string
+       {
+               out, err := exec.Command(goBin, "env", "GOMODCACHE").Output()
+               if err != nil {
+                       t.Fatalf("%s env GOMODCACHE: %v", goBin, err)
+               }
+               modcacheOk := false
+               if gomodcache := string(bytes.TrimSpace(out)); gomodcache != "" {
+                       if _, err := os.Stat(gomodcache); err == nil {
+                               modcacheOk = true
+                       }
+               }
+               if !modcacheOk {
+                       modcacheEnv = []string{
+                               "GOMODCACHE=" + t.TempDir(),
+                               "GOFLAGS=" + os.Getenv("GOFLAGS") + " -modcacherw", // Allow t.TempDir() to clean up subdirectories.
+                       }
+               }
+       }
+
        // Build the bundle binary at the golang.org/x/tools
        // module version specified in GOROOT/src/cmd/go.mod.
        bundleDir := t.TempDir()
-       r := runner{Dir: filepath.Join(runtime.GOROOT(), "src/cmd")}
+       r := runner{
+               Dir: filepath.Join(runtime.GOROOT(), "src/cmd"),
+               Env: append(os.Environ(), modcacheEnv...),
+       }
        r.run(t, goBin, "build", "-mod=readonly", "-o", bundleDir, "golang.org/x/tools/cmd/bundle")
 
        var gorootCopyDir string
@@ -160,7 +189,7 @@ func TestAllDependencies(t *testing.T) {
                        }
                        r := runner{
                                Dir: filepath.Join(gorootCopyDir, rel),
-                               Env: append(os.Environ(),
+                               Env: append(append(os.Environ(), modcacheEnv...),
                                        // Set GOROOT.
                                        "GOROOT="+gorootCopyDir,
                                        // Explicitly override PWD and clear GOROOT_FINAL so that GOROOT=gorootCopyDir is definitely used.