Skip to content

Commit b176867

Browse files
cmd/go: add -chaged to query for non-defaults in the env
DO NOT SUBMIT Fixes #34208 Change-Id: Ia836aae7176aee926fe47b09fad28d265f8e0404
1 parent 4a7f3ac commit b176867

File tree

13 files changed

+159
-101
lines changed

13 files changed

+159
-101
lines changed

src/cmd/go/alldocs.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/go_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func TestMain(m *testing.M) {
200200
defer removeAll(testTmpDir)
201201
}
202202

203-
testGOCACHE = cache.DefaultDir()
203+
testGOCACHE, _ = cache.DefaultDir()
204204
if testenv.HasGoBuild() {
205205
testBin = filepath.Join(testTmpDir, "testbin")
206206
if err := os.Mkdir(testBin, 0777); err != nil {

src/cmd/go/internal/bug/bug.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func printGoEnv(w io.Writer) {
106106
env := envcmd.MkEnv()
107107
env = append(env, envcmd.ExtraEnvVars()...)
108108
env = append(env, envcmd.ExtraEnvVarsCostly()...)
109-
envcmd.PrintEnv(w, env)
109+
envcmd.PrintEnv(w, env, false)
110110
}
111111

112112
func printGoDetails(w io.Writer) {

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ See golang.org to learn more about Go.
3939
// initDefaultCache does the work of finding the default cache
4040
// the first time Default is called.
4141
func initDefaultCache() {
42-
dir := DefaultDir()
42+
dir, _ := DefaultDir()
4343
if dir == "off" {
4444
if defaultDirErr != nil {
4545
base.Fatalf("build cache is required, but could not be located: %v", defaultDirErr)
@@ -67,14 +67,15 @@ func initDefaultCache() {
6767
}
6868

6969
var (
70-
defaultDirOnce sync.Once
71-
defaultDir string
72-
defaultDirErr error
70+
defaultDirOnce sync.Once
71+
defaultDir string
72+
defaultDirChanged bool
73+
defaultDirErr error
7374
)
7475

7576
// DefaultDir returns the effective GOCACHE setting.
7677
// It returns "off" if the cache is disabled.
77-
func DefaultDir() string {
78+
func DefaultDir() (string, bool) {
7879
// Save the result of the first call to DefaultDir for later use in
7980
// initDefaultCache. cmd/go/main.go explicitly sets GOCACHE so that
8081
// subprocesses will inherit it, but that means initDefaultCache can't
@@ -83,10 +84,12 @@ func DefaultDir() string {
8384
defaultDirOnce.Do(func() {
8485
defaultDir = cfg.Getenv("GOCACHE")
8586
if filepath.IsAbs(defaultDir) || defaultDir == "off" {
87+
defaultDirChanged = true
8688
return
8789
}
8890
if defaultDir != "" {
8991
defaultDir = "off"
92+
defaultDirChanged = true
9093
defaultDirErr = fmt.Errorf("GOCACHE is not an absolute path")
9194
return
9295
}
@@ -101,5 +104,5 @@ func DefaultDir() string {
101104
defaultDir = filepath.Join(dir, "go-build")
102105
})
103106

104-
return defaultDir
107+
return defaultDir, defaultDirChanged
105108
}

src/cmd/go/internal/cfg/cfg.go

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ var (
103103

104104
// GoPathError is set when GOPATH is not set. it contains an
105105
// explanation why GOPATH is unset.
106-
GoPathError string
106+
GoPathError string
107+
GOPATHChanged bool
107108
)
108109

109110
func defaultContext() build.Context {
@@ -113,7 +114,7 @@ func defaultContext() build.Context {
113114

114115
// Override defaults computed in go/build with defaults
115116
// from go environment configuration file, if known.
116-
ctxt.GOPATH = envOr("GOPATH", gopath(ctxt))
117+
ctxt.GOPATH, GOPATHChanged = EnvOrAndChanged("GOPATH", gopath(ctxt))
117118
ctxt.GOOS = Goos
118119
ctxt.GOARCH = Goarch
119120

@@ -267,8 +268,9 @@ func init() {
267268

268269
// An EnvVar is an environment variable Name=Value.
269270
type EnvVar struct {
270-
Name string
271-
Value string
271+
Name string
272+
Value string
273+
Changed bool
272274
}
273275

274276
// OrigEnv is the original environment of the program at startup.
@@ -280,31 +282,32 @@ var OrigEnv []string
280282
var CmdEnv []EnvVar
281283

282284
var envCache struct {
283-
once sync.Once
284-
m map[string]string
285+
once sync.Once
286+
m map[string]string
287+
goroot map[string]string
285288
}
286289

287290
// EnvFile returns the name of the Go environment configuration file.
288-
func EnvFile() (string, error) {
291+
func EnvFile() (string, bool, error) {
289292
if file := os.Getenv("GOENV"); file != "" {
290293
if file == "off" {
291-
return "", fmt.Errorf("GOENV=off")
294+
return "", true, fmt.Errorf("GOENV=off")
292295
}
293-
return file, nil
296+
return file, true, nil
294297
}
295298
dir, err := os.UserConfigDir()
296299
if err != nil {
297-
return "", err
300+
return "", false, err
298301
}
299302
if dir == "" {
300-
return "", fmt.Errorf("missing user-config dir")
303+
return "", false, fmt.Errorf("missing user-config dir")
301304
}
302-
return filepath.Join(dir, "go/env"), nil
305+
return filepath.Join(dir, "go/env"), false, nil
303306
}
304307

305308
func initEnvCache() {
306-
envCache.m = make(map[string]string)
307-
if file, _ := EnvFile(); file != "" {
309+
envCache.m, envCache.goroot = make(map[string]string), make(map[string]string)
310+
if file, _, _ := EnvFile(); file != "" {
308311
readEnvFile(file, "user")
309312
}
310313
goroot := findGOROOT(envCache.m["GOROOT"])
@@ -355,6 +358,7 @@ func readEnvFile(file string, source string) {
355358
if _, ok := envCache.m[string(key)]; ok {
356359
continue
357360
}
361+
envCache.goroot[string(key)] = string(val)
358362
}
359363
envCache.m[string(key)] = string(val)
360364
}
@@ -404,54 +408,62 @@ var (
404408

405409
GOROOT_FINAL string
406410

407-
GOBIN = Getenv("GOBIN")
408-
GOMODCACHE = envOr("GOMODCACHE", gopathDir("pkg/mod"))
411+
GOBIN = Getenv("GOBIN")
412+
GOMODCACHE, GOMODCACHEChanged = EnvOrAndChanged("GOMODCACHE", gopathDir("pkg/mod"))
409413

410414
// Used in envcmd.MkEnv and build ID computations.
411-
GOARM = envOr("GOARM", fmt.Sprint(buildcfg.GOARM))
412-
GO386 = envOr("GO386", buildcfg.GO386)
413-
GOAMD64 = envOr("GOAMD64", fmt.Sprintf("%s%d", "v", buildcfg.GOAMD64))
414-
GOMIPS = envOr("GOMIPS", buildcfg.GOMIPS)
415-
GOMIPS64 = envOr("GOMIPS64", buildcfg.GOMIPS64)
416-
GOPPC64 = envOr("GOPPC64", fmt.Sprintf("%s%d", "power", buildcfg.GOPPC64))
417-
GORISCV64 = envOr("GORISCV64", fmt.Sprintf("rva%du64", buildcfg.GORISCV64))
418-
GOWASM = envOr("GOWASM", fmt.Sprint(buildcfg.GOWASM))
419-
420-
GOPROXY = envOr("GOPROXY", "")
421-
GOSUMDB = envOr("GOSUMDB", "")
422-
GOPRIVATE = Getenv("GOPRIVATE")
423-
GONOPROXY = envOr("GONOPROXY", GOPRIVATE)
424-
GONOSUMDB = envOr("GONOSUMDB", GOPRIVATE)
425-
GOINSECURE = Getenv("GOINSECURE")
426-
GOVCS = Getenv("GOVCS")
415+
GOARM, goARMChanged = EnvOrAndChanged("GOARM", fmt.Sprint(buildcfg.GOARM))
416+
GO386, go386Changed = EnvOrAndChanged("GO386", buildcfg.GO386)
417+
GOAMD64, goAMD64Changed = EnvOrAndChanged("GOAMD64", fmt.Sprintf("%s%d", "v", buildcfg.GOAMD64))
418+
GOMIPS, goMIPSChanged = EnvOrAndChanged("GOMIPS", buildcfg.GOMIPS)
419+
GOMIPS64, goMIPS64Changed = EnvOrAndChanged("GOMIPS64", buildcfg.GOMIPS64)
420+
GOPPC64, goPPC64Changed = EnvOrAndChanged("GOPPC64", fmt.Sprintf("%s%d", "power", buildcfg.GOPPC64))
421+
GORISCV64, gORISCV64Changed = EnvOrAndChanged("GORISCV64", fmt.Sprintf("rva%du64", buildcfg.GORISCV64))
422+
GOWASM, goWASMChanged = EnvOrAndChanged("GOWASM", fmt.Sprint(buildcfg.GOWASM))
423+
424+
GOPROXY, GOPROXYChanged = EnvOrAndChanged("GOPROXY", "")
425+
GOSUMDB, GOSUMDBChanged = EnvOrAndChanged("GOSUMDB", "")
426+
GOPRIVATE = Getenv("GOPRIVATE")
427+
GONOPROXY, GONOPROXYChanged = EnvOrAndChanged("GONOPROXY", GOPRIVATE)
428+
GONOSUMDB, GONOSUMDBChanged = EnvOrAndChanged("GONOSUMDB", GOPRIVATE)
429+
GOINSECURE = Getenv("GOINSECURE")
430+
GOVCS = Getenv("GOVCS")
427431
)
428432

433+
func EnvOrAndChanged(name, def string) (string, bool) {
434+
val := Getenv(name)
435+
if val != "" {
436+
return val, envCache.goroot[name] != val
437+
}
438+
return def, false
439+
}
440+
429441
var SumdbDir = gopathDir("pkg/sumdb")
430442

431443
// GetArchEnv returns the name and setting of the
432444
// GOARCH-specific architecture environment variable.
433445
// If the current architecture has no GOARCH-specific variable,
434446
// GetArchEnv returns empty key and value.
435-
func GetArchEnv() (key, val string) {
447+
func GetArchEnv() (key, val string, changed bool) {
436448
switch Goarch {
437449
case "arm":
438-
return "GOARM", GOARM
450+
return "GOARM", GOARM, goARMChanged
439451
case "386":
440-
return "GO386", GO386
452+
return "GO386", GO386, go386Changed
441453
case "amd64":
442-
return "GOAMD64", GOAMD64
454+
return "GOAMD64", GOAMD64, goAMD64Changed
443455
case "mips", "mipsle":
444-
return "GOMIPS", GOMIPS
456+
return "GOMIPS", GOMIPS, goMIPSChanged
445457
case "mips64", "mips64le":
446-
return "GOMIPS64", GOMIPS64
458+
return "GOMIPS64", GOMIPS64, goMIPS64Changed
447459
case "ppc64", "ppc64le":
448-
return "GOPPC64", GOPPC64
460+
return "GOPPC64", GOPPC64, goPPC64Changed
449461
case "riscv64":
450-
return "GORISCV64", GORISCV64
462+
return "GORISCV64", GORISCV64, gORISCV64Changed
451463
case "wasm":
452-
return "GOWASM", GOWASM
464+
return "GOWASM", GOWASM, goWASMChanged
453465
}
454-
return "", ""
466+
return "", "", false
455467
}
456468

457469
// envOr returns Getenv(key) if set, or else def.

src/cmd/go/internal/clean/clean.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) {
153153
sh := work.NewShell("", fmt.Print)
154154

155155
if cleanCache {
156-
dir := cache.DefaultDir()
156+
dir, _ := cache.DefaultDir()
157157
if dir != "off" {
158158
// Remove the cache subdirectories but not the top cache directory.
159159
// The top cache directory may have been created with special permissions
@@ -180,7 +180,7 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) {
180180
// Instead of walking through the entire cache looking for test results,
181181
// we write a file to the cache indicating that all test results from before
182182
// right now are to be ignored.
183-
dir := cache.DefaultDir()
183+
dir, _ := cache.DefaultDir()
184184
if dir != "off" {
185185
f, err := lockedfile.Edit(filepath.Join(dir, "testexpire.txt"))
186186
if err == nil {

0 commit comments

Comments
 (0)