Skip to content

Commit c38d122

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
cmd/internal/moddeps: preserve PWD more carefully in commands
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 <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent e8fe3b7 commit c38d122

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/cmd/internal/moddeps/moddeps_test.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"io/fs"
1414
"os"
1515
"path/filepath"
16+
"slices"
17+
"sort"
1618
"strings"
1719
"sync"
1820
"testing"
@@ -48,13 +50,15 @@ func TestAllDependencies(t *testing.T) {
4850
// This short test does NOT ensure that the vendored contents match
4951
// the unmodified contents of the corresponding dependency versions.
5052
t.Run(m.Path+"(quick)", func(t *testing.T) {
53+
t.Logf("module %s in directory %s", m.Path, m.Dir)
54+
5155
if m.hasVendor {
5256
// Load all of the packages in the module to ensure that their
5357
// dependencies are vendored. If any imported package is missing,
5458
// 'go list -deps' will fail when attempting to load it.
5559
cmd := testenv.Command(t, goBin, "list", "-mod=vendor", "-deps", "./...")
56-
cmd.Env = append(os.Environ(), "GO111MODULE=on", "GOWORK=off")
5760
cmd.Dir = m.Dir
61+
cmd.Env = append(cmd.Environ(), "GO111MODULE=on", "GOWORK=off")
5862
cmd.Stderr = new(strings.Builder)
5963
_, err := cmd.Output()
6064
if err != nil {
@@ -67,8 +71,8 @@ func TestAllDependencies(t *testing.T) {
6771
// There is no vendor directory, so the module must have no dependencies.
6872
// Check that the list of active modules contains only the main module.
6973
cmd := testenv.Command(t, goBin, "list", "-mod=readonly", "-m", "all")
70-
cmd.Env = append(os.Environ(), "GO111MODULE=on", "GOWORK=off")
7174
cmd.Dir = m.Dir
75+
cmd.Env = append(cmd.Environ(), "GO111MODULE=on", "GOWORK=off")
7276
cmd.Stderr = new(strings.Builder)
7377
out, err := cmd.Output()
7478
if err != nil {
@@ -170,6 +174,8 @@ func TestAllDependencies(t *testing.T) {
170174
}
171175

172176
t.Run(m.Path+"(thorough)", func(t *testing.T) {
177+
t.Logf("module %s in directory %s", m.Path, m.Dir)
178+
173179
defer func() {
174180
if t.Failed() {
175181
// The test failed, which means it's possible the GOROOT copy
@@ -189,8 +195,7 @@ func TestAllDependencies(t *testing.T) {
189195
Env: append(append(os.Environ(), modcacheEnv...),
190196
// Set GOROOT.
191197
"GOROOT="+gorootCopyDir,
192-
// Explicitly override PWD and clear GOROOT_FINAL so that GOROOT=gorootCopyDir is definitely used.
193-
"PWD="+filepath.Join(gorootCopyDir, rel),
198+
// Explicitly clear GOROOT_FINAL so that GOROOT=gorootCopyDir is definitely used.
194199
"GOROOT_FINAL=",
195200
// Add GOROOTcopy/bin and bundleDir to front of PATH.
196201
"PATH="+filepath.Join(gorootCopyDir, "bin")+string(filepath.ListSeparator)+
@@ -249,6 +254,7 @@ func packagePattern(modulePath string) string {
249254
// deemed safe to share for the purpose of the TestAllDependencies test.
250255
func makeGOROOTCopy(t *testing.T) string {
251256
t.Helper()
257+
252258
gorootCopyDir := t.TempDir()
253259
err := filepath.Walk(testenv.GOROOT(t), func(src string, info os.FileInfo, err error) error {
254260
if err != nil {
@@ -309,6 +315,7 @@ func makeGOROOTCopy(t *testing.T) string {
309315
if err != nil {
310316
t.Fatal(err)
311317
}
318+
t.Logf("copied GOROOT from %s to %s", testenv.GOROOT(t), gorootCopyDir)
312319
return gorootCopyDir
313320
}
314321

@@ -322,7 +329,10 @@ func (r runner) run(t *testing.T, args ...string) {
322329
t.Helper()
323330
cmd := testenv.Command(t, args[0], args[1:]...)
324331
cmd.Dir = r.Dir
325-
cmd.Env = r.Env
332+
cmd.Env = slices.Clip(r.Env)
333+
if r.Dir != "" {
334+
cmd.Env = append(cmd.Env, "PWD="+r.Dir)
335+
}
326336
out, err := cmd.CombinedOutput()
327337
if err != nil {
328338
t.Logf("> %s\n", strings.Join(args, " "))
@@ -462,8 +472,8 @@ func findGorootModules(t *testing.T) []gorootModule {
462472
// Use 'go list' to describe the module contained in this directory (but
463473
// not its dependencies).
464474
cmd := testenv.Command(t, goBin, "list", "-json", "-m")
465-
cmd.Env = append(os.Environ(), "GO111MODULE=on", "GOWORK=off")
466475
cmd.Dir = dir
476+
cmd.Env = append(cmd.Environ(), "GO111MODULE=on", "GOWORK=off")
467477
cmd.Stderr = new(strings.Builder)
468478
out, err := cmd.Output()
469479
if err != nil {
@@ -507,6 +517,9 @@ func findGorootModules(t *testing.T) []gorootModule {
507517
break
508518
}
509519
}
520+
sort.Slice(goroot.modules, func(i, j int) bool {
521+
return goroot.modules[i].Dir < goroot.modules[j].Dir
522+
})
510523
})
511524
if goroot.err != nil {
512525
t.Fatal(goroot.err)

0 commit comments

Comments
 (0)