Skip to content

Commit f0e9754

Browse files
committed
cmd/go: add env -w and env -u to set and unset default env vars
Setting environment variables for go command configuration is too difficult and system-specific. This CL adds go env -w, to change the default settings more easily, in a portable way. It also adds go env -u, to unset those changes. See https://golang.org/design/30411-env for details. Fixes #30411. Change-Id: I36e83f55b666459f8f7f482432a4a6ee015da71d Reviewed-on: https://go-review.googlesource.com/c/go/+/171137 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
1 parent e40dffe commit f0e9754

File tree

21 files changed

+671
-140
lines changed

21 files changed

+671
-140
lines changed

src/cmd/go/alldocs.go

+37-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/go_test.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ package main_test
66

77
import (
88
"bytes"
9-
"cmd/go/internal/cache"
10-
"cmd/internal/sys"
119
"context"
1210
"debug/elf"
1311
"debug/macho"
@@ -28,6 +26,10 @@ import (
2826
"strings"
2927
"testing"
3028
"time"
29+
30+
"cmd/go/internal/cache"
31+
"cmd/go/internal/cfg"
32+
"cmd/internal/sys"
3133
)
3234

3335
var (
@@ -119,6 +121,8 @@ var testCtx = context.Background()
119121
// The TestMain function creates a go command for testing purposes and
120122
// deletes it after the tests have been run.
121123
func TestMain(m *testing.M) {
124+
// $GO_GCFLAGS a compiler debug flag known to cmd/dist, make.bash, etc.
125+
// It is not a standard go command flag; use os.Getenv, not cfg.Getenv.
122126
if os.Getenv("GO_GCFLAGS") != "" {
123127
fmt.Fprintf(os.Stderr, "testing: warning: no tests to run\n") // magic string for cmd/go
124128
fmt.Printf("cmd/go test is not compatible with $GO_GCFLAGS being set\n")
@@ -256,6 +260,7 @@ func TestMain(m *testing.M) {
256260
}
257261
}
258262
// Don't let these environment variables confuse the test.
263+
os.Setenv("GOENV", "off")
259264
os.Unsetenv("GOBIN")
260265
os.Unsetenv("GOPATH")
261266
os.Unsetenv("GIT_ALLOW_PROTOCOL")
@@ -264,7 +269,7 @@ func TestMain(m *testing.M) {
264269
// Setting HOME to a non-existent directory will break
265270
// those systems. Disable ccache and use real compiler. Issue 17668.
266271
os.Setenv("CCACHE_DISABLE", "1")
267-
if os.Getenv("GOCACHE") == "" {
272+
if cfg.Getenv("GOCACHE") == "" {
268273
os.Setenv("GOCACHE", testGOCACHE) // because $HOME is gone
269274
}
270275

@@ -5258,7 +5263,7 @@ func TestCacheVet(t *testing.T) {
52585263
if strings.Contains(os.Getenv("GODEBUG"), "gocacheverify") {
52595264
t.Skip("GODEBUG gocacheverify")
52605265
}
5261-
if os.Getenv("GOCACHE") == "off" {
5266+
if cfg.Getenv("GOCACHE") == "off" {
52625267
tooSlow(t)
52635268
tg.makeTempdir()
52645269
tg.setenv("GOCACHE", tg.path("cache"))

src/cmd/go/internal/base/goflags.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package base
77
import (
88
"flag"
99
"fmt"
10-
"os"
1110
"runtime"
1211
"strings"
1312

@@ -62,7 +61,7 @@ func InitGOFLAGS() {
6261
// (Both will show the GOFLAGS setting if let succeed.)
6362
hideErrors := cfg.CmdName == "env" || cfg.CmdName == "bug"
6463

65-
goflags = strings.Fields(os.Getenv("GOFLAGS"))
64+
goflags = strings.Fields(cfg.Getenv("GOFLAGS"))
6665
if goflags == nil {
6766
goflags = []string{} // avoid work on later InitGOFLAGS call
6867
}

src/cmd/go/internal/cache/default.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"sync"
1313

1414
"cmd/go/internal/base"
15+
"cmd/go/internal/cfg"
1516
)
1617

1718
// Default returns the default cache to use, or nil if no cache should be used.
@@ -73,7 +74,7 @@ func DefaultDir() string {
7374
// otherwise distinguish between an explicit "off" and a UserCacheDir error.
7475

7576
defaultDirOnce.Do(func() {
76-
defaultDir = os.Getenv("GOCACHE")
77+
defaultDir = cfg.Getenv("GOCACHE")
7778
if filepath.IsAbs(defaultDir) || defaultDir == "off" {
7879
return
7980
}

0 commit comments

Comments
 (0)