1
0
mirror of https://github.com/golang/go synced 2024-11-23 14:50:07 -07:00

cmd/go: prefer $GOTMPDIR over operating system tmp dir for temp files

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>
This commit is contained in:
Russ Cox 2017-11-01 13:31:44 -04:00
parent 2ff3e9c8d8
commit efb1a7524e
6 changed files with 34 additions and 3 deletions

View 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)
}

View File

@ -331,6 +331,8 @@
// 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
//
@ -1103,6 +1105,12 @@
// 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:
//

View 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")

View 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"},

View 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:

View 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)
}