]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: prefer $GOTMPDIR over operating system tmp dir for temp files
authorRuss Cox <rsc@golang.org>
Wed, 1 Nov 2017 17:31:44 +0000 (13:31 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 3 Nov 2017 17:45:09 +0000 (17:45 +0000)
We build and run executables in the work directory,
and some users have $TMPDIR set noexec.

Fixes #8451.

Change-Id: I76bf2ddec84e9cb37ad9a6feb53a1a84b47aa263
Reviewed-on: https://go-review.googlesource.com/75475
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
src/cmd/dist/util.go
src/cmd/go/alldocs.go
src/cmd/go/go_test.go
src/cmd/go/internal/envcmd/env.go
src/cmd/go/internal/help/helpdoc.go
src/cmd/go/internal/work/action.go

index 296b993de815d4fbf618dba9acf236497e4576e9..7e27bbb064489d676419cadbd4d3232a7dc5ee46 100644 (file)
@@ -326,7 +326,7 @@ func xreaddirfiles(dir string) []string {
 // xworkdir creates a new temporary directory to hold object files
 // and returns the name of that directory.
 func xworkdir() string {
-       name, err := ioutil.TempDir("", "go-tool-dist-")
+       name, err := ioutil.TempDir(os.Getenv("GOTMPDIR"), "go-tool-dist-")
        if err != nil {
                fatalf("%v", err)
        }
index a61aba824994c07d25f76eaf0b52e35f5f822653..0124199e181c8a09b264484509cf9f5817223946 100644 (file)
 // The -json flag prints the environment in JSON format
 // instead of as a shell script.
 //
+// For more about environment variables, see 'go help environment'.
+//
 //
 // Start a bug report
 //
 //             See https://golang.org/doc/articles/race_detector.html.
 //     GOROOT
 //             The root of the go tree.
+//     GOTMPDIR
+//             The directory where the go command will write
+//             temporary source files, packages, and binaries.
+//     GOCACHE
+//             The directory where the go command will store
+//             cached information for reuse in future builds.
 //
 // Environment variables for use with cgo:
 //
index 51696db6c219497dbe525659dacbf87f8f5ba626..297865901993deb5cc97e9e65fe17ee657125753 100644 (file)
@@ -4690,6 +4690,20 @@ func TestUpxCompression(t *testing.T) {
        }
 }
 
+func TestGOTMPDIR(t *testing.T) {
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.parallel()
+       tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
+       tg.makeTempdir()
+       tg.setenv("GOTMPDIR", tg.tempdir)
+       tg.setenv("GOCACHE", "off")
+
+       // complex/x is a trivial non-main package.
+       tg.run("build", "-work", "-x", "complex/w")
+       tg.grepStderr("WORK="+regexp.QuoteMeta(tg.tempdir), "did not work in $GOTMPDIR")
+}
+
 func TestBuildCache(t *testing.T) {
        if strings.Contains(os.Getenv("GODEBUG"), "gocacheverify") {
                t.Skip("GODEBUG gocacheverify")
index cd3f9000c2952f30e3b462a623f3ef5e432890a6..90ab2d718fe0c31d57d7e38bbc019af078afc68d 100644 (file)
@@ -32,6 +32,8 @@ each named variable on its own line.
 
 The -json flag prints the environment in JSON format
 instead of as a shell script.
+
+For more about environment variables, see 'go help environment'.
        `,
 }
 
@@ -48,6 +50,7 @@ func MkEnv() []cfg.EnvVar {
        env := []cfg.EnvVar{
                {Name: "GOARCH", Value: cfg.Goarch},
                {Name: "GOBIN", Value: cfg.GOBIN},
+               {Name: "GOCACHE", Value: cache.DefaultDir()},
                {Name: "GOEXE", Value: cfg.ExeSuffix},
                {Name: "GOHOSTARCH", Value: runtime.GOARCH},
                {Name: "GOHOSTOS", Value: runtime.GOOS},
@@ -55,8 +58,8 @@ func MkEnv() []cfg.EnvVar {
                {Name: "GOPATH", Value: cfg.BuildContext.GOPATH},
                {Name: "GORACE", Value: os.Getenv("GORACE")},
                {Name: "GOROOT", Value: cfg.GOROOT},
+               {Name: "GOTMPDIR", Value: os.Getenv("GOTMPDIR")},
                {Name: "GOTOOLDIR", Value: base.ToolDir},
-               {Name: "GOCACHE", Value: cache.DefaultDir()},
 
                // disable escape codes in clang errors
                {Name: "TERM", Value: "dumb"},
index 508ff60862b54a5fb6b812c700428dd1b0156a80..76f3137c1263a5babf5789873536e1ed4e23bff9 100644 (file)
@@ -471,6 +471,12 @@ General-purpose environment variables:
                See https://golang.org/doc/articles/race_detector.html.
        GOROOT
                The root of the go tree.
+       GOTMPDIR
+               The directory where the go command will write
+               temporary source files, packages, and binaries.
+       GOCACHE
+               The directory where the go command will store
+               cached information for reuse in future builds.
 
 Environment variables for use with cgo:
 
index 71d5ef3e79bb4d0cdd12a6b351bf53ebf771c335..883c45434058598ab82330a174ac3500ed1c30da 100644 (file)
@@ -200,7 +200,7 @@ func (b *Builder) Init() {
        if cfg.BuildN {
                b.WorkDir = "$WORK"
        } else {
-               b.WorkDir, err = ioutil.TempDir("", "go-build")
+               b.WorkDir, err = ioutil.TempDir(os.Getenv("GOTMPDIR"), "go-build")
                if err != nil {
                        base.Fatalf("%s", err)
                }