]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: env changed flag respects $GOROOT/go.env
authorqiulaidongfeng <2645477756@qq.com>
Thu, 23 May 2024 19:17:21 +0000 (19:17 +0000)
committerGopher Robot <gobot@golang.org>
Fri, 24 May 2024 17:13:51 +0000 (17:13 +0000)
From $GOROOT/go.env file got GOPROXY and GOSUMDB and GOTOOLCHAIN default value.

Fixes #67542

Change-Id: I0cb2e1ab6a32963288ae463a9b0bd92ac6719447
GitHub-Last-Rev: fda9be48b9e3bd3b124648eec1ef4ecfccac6d50
GitHub-Pull-Request: golang/go#67564
Reviewed-on: https://go-review.googlesource.com/c/go/+/587160
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
src/cmd/go/internal/cfg/cfg.go
src/cmd/go/internal/envcmd/env.go
src/cmd/go/testdata/script/env_changed.txt

index 002d0006ed41fc6d1164aa7e529b885f330a586a..3715a19a96dfaa8eeb1b845c79e5c90066271b65 100644 (file)
@@ -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,
@@ -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")
        }
@@ -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
@@ -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
 }
index c4836b2f9528883eefaa9f0130f30825dd7a7ab9..f19577c4df1875600cf0f25d5d9c85e0a8a259ee 100644 (file)
@@ -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()},
@@ -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 != ""
                }
index 7b7b154dae6bf99606a7cb0f3b02f5d039a33261..a3d368cd39e4807d1a7afe636ecffd746c2dc904 100644 (file)
@@ -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