Commit efb1a752 authored by Russ Cox's avatar Russ Cox

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: 's avatarDavid Crawshaw <crawshaw@golang.org>
parent 2ff3e9c8
......@@ -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)
}
......
......@@ -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:
//
......
......@@ -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")
......
......@@ -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"},
......
......@@ -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:
......
......@@ -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)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment