Skip to content

cmd/go: env changed flag respects $GOROOT/go.env #67564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/cmd/go/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,9 @@ var OrigEnv []string
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,
Expand All @@ -310,6 +311,7 @@ func EnvFile() (string, bool, error) {

func initEnvCache() {
envCache.m = make(map[string]string)
envCache.goroot = make(map[string]string)
if file, _, _ := EnvFile(); file != "" {
readEnvFile(file, "user")
}
Expand Down Expand Up @@ -357,6 +359,7 @@ func readEnvFile(file string, source string) {
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
Expand Down Expand Up @@ -433,10 +436,16 @@ var (

// 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
}
Expand Down
6 changes: 2 additions & 4 deletions src/cmd/go/internal/envcmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func MkEnv() []cfg.EnvVar {
{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()},
Expand All @@ -128,9 +128,7 @@ func MkEnv() []cfg.EnvVar {
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 != ""
}
Expand Down
11 changes: 11 additions & 0 deletions src/cmd/go/testdata/script/env_changed.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,14 @@ go env -changed -json GOOS
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