Skip to content

Commit

Permalink
cmd/internal/moddeps: use a temporary directory for GOMODCACHE if needed
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Bryan C. Mills committed Jun 21, 2021
1 parent a040042 commit 761edf7
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/cmd/internal/moddeps/moddeps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package moddeps_test

import (
"bytes"
"encoding/json"
"fmt"
"internal/testenv"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 761edf7

Please sign in to comment.