var CmdEnv []EnvVar
var envCache struct {
- once sync.Once
- m map[string]string
+ once sync.Once
+ m map[string]string
+ goroot map[string]string
}
// EnvFile returns the name of the Go environment configuration file,
func initEnvCache() {
envCache.m = make(map[string]string)
+ envCache.goroot = make(map[string]string)
if file, _, _ := EnvFile(); file != "" {
readEnvFile(file, "user")
}
key, val := line[:i], line[i+1:]
if source == "GOROOT" {
+ envCache.goroot[string(key)] = string(val)
// In the GOROOT/go.env file, do not overwrite fields loaded from the user's go/env file.
if _, ok := envCache.m[string(key)]; ok {
continue
// EnvOrAndChanged returns the environment variable value
// and reports whether it differs from the default value.
-func EnvOrAndChanged(name, def string) (string, bool) {
+func EnvOrAndChanged(name, def string) (v string, changed bool) {
val := Getenv(name)
if val != "" {
- return val, val != def
+ v = val
+ if g, ok := envCache.goroot[name]; ok {
+ changed = val != g
+ } else {
+ changed = val != def
+ }
+ return v, changed
}
return def, false
}
{Name: "GOROOT", Value: cfg.GOROOT},
{Name: "GOSUMDB", Value: cfg.GOSUMDB, Changed: cfg.GOSUMDBChanged},
{Name: "GOTMPDIR", Value: cfg.Getenv("GOTMPDIR")},
- {Name: "GOTOOLCHAIN", Value: cfg.Getenv("GOTOOLCHAIN")},
+ {Name: "GOTOOLCHAIN"},
{Name: "GOTOOLDIR", Value: build.ToolDir},
{Name: "GOVCS", Value: cfg.GOVCS},
{Name: "GOVERSION", Value: runtime.Version()},
case "GOCACHE":
env[i].Value, env[i].Changed = cache.DefaultDir()
case "GOTOOLCHAIN":
- if env[i].Value != "auto" {
- env[i].Changed = true
- }
+ env[i].Value, env[i].Changed = cfg.EnvOrAndChanged("GOTOOLCHAIN", "")
case "GODEBUG":
env[i].Changed = env[i].Value != ""
}
go env -changed -json GOARCH
[GOARCH:amd64] stdout '"GOARCH": "arm64"'
[!GOARCH:amd64] stdout '"GOARCH": "amd64"'
+
+env GOROOT=./a
+env GOPROXY=s
+go env -changed GOPROXY
+! stdout 'GOPROXY'
+env GOPROXY=s2
+go env -changed GOPROXY
+stdout 'GOPROXY=''?s2''?'
+
+-- a/go.env --
+GOPROXY=s