From c38d122fd4df8481a5b57a696945d65b7d4fc28e Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Thu, 13 Apr 2023 03:19:20 +0000 Subject: [PATCH] cmd/internal/moddeps: preserve PWD more carefully in commands MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On macOS, TMPDIR is typically a symlink, and the GOROOT for the buildlet is in TMPDIR as well. PWD must be preserved in order for os.Getwd (and functions based on it) to report paths that remain relative to GOROOT, and paths relative to GOROOT are necessary in order for filepath.Rel to report subdirectories as subdirectories (rather than paths with long "../../…" prefixes). Fortunately, the (*Cmd).Environ method added for #50599 makes preserving PWD somewhat easier. This fixes 'go test cmd/internal/moddeps' on the new darwin-amd64-longtest builder. For #35678. Change-Id: Ibaa458bc9a94b44ba455519bb8da445af07fe0d2 Reviewed-on: https://go-review.googlesource.com/c/go/+/484295 Auto-Submit: Bryan Mills Reviewed-by: Dmitri Shuralyov Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot Reviewed-by: Michael Pratt --- src/cmd/internal/moddeps/moddeps_test.go | 25 ++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/cmd/internal/moddeps/moddeps_test.go b/src/cmd/internal/moddeps/moddeps_test.go index 41220645c6e..b9ed292cf9b 100644 --- a/src/cmd/internal/moddeps/moddeps_test.go +++ b/src/cmd/internal/moddeps/moddeps_test.go @@ -13,6 +13,8 @@ import ( "io/fs" "os" "path/filepath" + "slices" + "sort" "strings" "sync" "testing" @@ -48,13 +50,15 @@ func TestAllDependencies(t *testing.T) { // This short test does NOT ensure that the vendored contents match // the unmodified contents of the corresponding dependency versions. t.Run(m.Path+"(quick)", func(t *testing.T) { + t.Logf("module %s in directory %s", m.Path, m.Dir) + if m.hasVendor { // Load all of the packages in the module to ensure that their // dependencies are vendored. If any imported package is missing, // 'go list -deps' will fail when attempting to load it. cmd := testenv.Command(t, goBin, "list", "-mod=vendor", "-deps", "./...") - cmd.Env = append(os.Environ(), "GO111MODULE=on", "GOWORK=off") cmd.Dir = m.Dir + cmd.Env = append(cmd.Environ(), "GO111MODULE=on", "GOWORK=off") cmd.Stderr = new(strings.Builder) _, err := cmd.Output() if err != nil { @@ -67,8 +71,8 @@ func TestAllDependencies(t *testing.T) { // There is no vendor directory, so the module must have no dependencies. // Check that the list of active modules contains only the main module. cmd := testenv.Command(t, goBin, "list", "-mod=readonly", "-m", "all") - cmd.Env = append(os.Environ(), "GO111MODULE=on", "GOWORK=off") cmd.Dir = m.Dir + cmd.Env = append(cmd.Environ(), "GO111MODULE=on", "GOWORK=off") cmd.Stderr = new(strings.Builder) out, err := cmd.Output() if err != nil { @@ -170,6 +174,8 @@ func TestAllDependencies(t *testing.T) { } t.Run(m.Path+"(thorough)", func(t *testing.T) { + t.Logf("module %s in directory %s", m.Path, m.Dir) + defer func() { if t.Failed() { // The test failed, which means it's possible the GOROOT copy @@ -189,8 +195,7 @@ func TestAllDependencies(t *testing.T) { Env: append(append(os.Environ(), modcacheEnv...), // Set GOROOT. "GOROOT="+gorootCopyDir, - // Explicitly override PWD and clear GOROOT_FINAL so that GOROOT=gorootCopyDir is definitely used. - "PWD="+filepath.Join(gorootCopyDir, rel), + // Explicitly clear GOROOT_FINAL so that GOROOT=gorootCopyDir is definitely used. "GOROOT_FINAL=", // Add GOROOTcopy/bin and bundleDir to front of PATH. "PATH="+filepath.Join(gorootCopyDir, "bin")+string(filepath.ListSeparator)+ @@ -249,6 +254,7 @@ func packagePattern(modulePath string) string { // deemed safe to share for the purpose of the TestAllDependencies test. func makeGOROOTCopy(t *testing.T) string { t.Helper() + gorootCopyDir := t.TempDir() err := filepath.Walk(testenv.GOROOT(t), func(src string, info os.FileInfo, err error) error { if err != nil { @@ -309,6 +315,7 @@ func makeGOROOTCopy(t *testing.T) string { if err != nil { t.Fatal(err) } + t.Logf("copied GOROOT from %s to %s", testenv.GOROOT(t), gorootCopyDir) return gorootCopyDir } @@ -322,7 +329,10 @@ func (r runner) run(t *testing.T, args ...string) { t.Helper() cmd := testenv.Command(t, args[0], args[1:]...) cmd.Dir = r.Dir - cmd.Env = r.Env + cmd.Env = slices.Clip(r.Env) + if r.Dir != "" { + cmd.Env = append(cmd.Env, "PWD="+r.Dir) + } out, err := cmd.CombinedOutput() if err != nil { t.Logf("> %s\n", strings.Join(args, " ")) @@ -462,8 +472,8 @@ func findGorootModules(t *testing.T) []gorootModule { // Use 'go list' to describe the module contained in this directory (but // not its dependencies). cmd := testenv.Command(t, goBin, "list", "-json", "-m") - cmd.Env = append(os.Environ(), "GO111MODULE=on", "GOWORK=off") cmd.Dir = dir + cmd.Env = append(cmd.Environ(), "GO111MODULE=on", "GOWORK=off") cmd.Stderr = new(strings.Builder) out, err := cmd.Output() if err != nil { @@ -507,6 +517,9 @@ func findGorootModules(t *testing.T) []gorootModule { break } } + sort.Slice(goroot.modules, func(i, j int) bool { + return goroot.modules[i].Dir < goroot.modules[j].Dir + }) }) if goroot.err != nil { t.Fatal(goroot.err)