diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index 7403b92cd14a3c..a0d7b4a8ea5df5 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -2405,8 +2405,9 @@ // The name of checksum database to use and optionally its public key and // URL. See https://golang.org/ref/mod#authenticating. // GOTMPDIR -// The directory where the go command will write -// temporary source files, packages, and binaries. +// The directory where the go command will write temporary source files, +// packages, and binaries. +// Also used to specify the prefix of TmpDir function in testing package. // GOTOOLCHAIN // Controls which Go toolchain is used. See https://go.dev/doc/toolchain. // GOVCS diff --git a/src/cmd/go/internal/help/helpdoc.go b/src/cmd/go/internal/help/helpdoc.go index 7f8565a3cbab82..a49578f47f2e75 100644 --- a/src/cmd/go/internal/help/helpdoc.go +++ b/src/cmd/go/internal/help/helpdoc.go @@ -573,8 +573,9 @@ General-purpose environment variables: The name of checksum database to use and optionally its public key and URL. See https://golang.org/ref/mod#authenticating. GOTMPDIR - The directory where the go command will write - temporary source files, packages, and binaries. + The directory where the go command will write temporary source files, + packages, and binaries. + Also used to specify the prefix of TmpDir function in testing package. GOTOOLCHAIN Controls which Go toolchain is used. See https://go.dev/doc/toolchain. GOVCS diff --git a/src/testing/testing.go b/src/testing/testing.go index 3475bfca4a6976..18b63164f4ec6f 100644 --- a/src/testing/testing.go +++ b/src/testing/testing.go @@ -1362,7 +1362,7 @@ func (c *common) TempDir() string { return -1 } pattern = strings.Map(mapper, pattern) - c.tempDir, c.tempDirErr = os.MkdirTemp("", pattern) + c.tempDir, c.tempDirErr = os.MkdirTemp(os.Getenv("GOTMPDIR"), pattern) if c.tempDirErr == nil { c.Cleanup(func() { if err := removeAll(c.tempDir); err != nil { diff --git a/src/testing/testing_test.go b/src/testing/testing_test.go index f4f5817a37ddb3..97324724a3414b 100644 --- a/src/testing/testing_test.go +++ b/src/testing/testing_test.go @@ -147,6 +147,40 @@ func testTempDir(t *testing.T) { } } +func TestTempDirGOTMPDIR(t *testing.T) { + customTmpDir := filepath.Join(os.TempDir(), "custom-gotmpdir-test") + if err := os.MkdirAll(customTmpDir, 0777); err != nil { + t.Fatal(err) + } + defer os.RemoveAll(customTmpDir) + + t.Setenv("GOTMPDIR", customTmpDir) + + dir := t.TempDir() + if dir == "" { + t.Fatal("expected dir") + } + + if !strings.HasPrefix(dir, customTmpDir) { + t.Errorf("TempDir did not use GOTMPDIR: got %q, want prefix %q", dir, customTmpDir) + } + + fi, err := os.Stat(dir) + if err != nil { + t.Fatal(err) + } + if !fi.IsDir() { + t.Errorf("dir %q is not a dir", dir) + } + + t.Setenv("GOTMPDIR", "another-custom-gotmpdir-test") + + dir2 := t.TempDir() + if filepath.Dir(dir) != filepath.Dir(dir2) { + t.Fatalf("calls to TempDir after changed GOTMPDIR do not share a parent; got %q, %q", dir, dir2) + } +} + func TestSetenv(t *testing.T) { tests := []struct { name string