From 2954eb14555cde4568de96174521291d6884b0e4 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Sun, 11 Feb 2024 13:03:37 +0800 Subject: [PATCH 01/28] cmd/go: add -chaged to query for non-defaults in the env DO NOT SUBMIT Fixes #34208 Change-Id: Ia836aae7176aee926fe47b09fad28d265f8e0404 --- src/cmd/go/alldocs.go | 2 +- src/cmd/go/go_test.go | 2 +- src/cmd/go/internal/bug/bug.go | 2 +- src/cmd/go/internal/cache/default.go | 15 +- src/cmd/go/internal/cfg/cfg.go | 102 ++-- src/cmd/go/internal/clean/clean.go | 4 +- src/cmd/go/internal/envcmd/env.go | 123 +++-- src/cmd/go/internal/envcmd/env_test.go | 2 +- src/cmd/go/internal/load/pkg.go | 2 +- src/cmd/go/internal/modindex/read.go | 3 +- src/cmd/go/internal/test/test.go | 2 +- src/cmd/go/internal/work/exec.go | 4 +- src/cmd/go/internal/work/shell.go | 3 +- src/cmd/go/testdata/counters.txt | 690 +++++++++++++++++++++++++ 14 files changed, 853 insertions(+), 103 deletions(-) create mode 100644 src/cmd/go/testdata/counters.txt diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index 7800c72af3b0fd..bb128f8903f3c1 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -456,7 +456,7 @@ // // Usage: // -// go env [-json] [-u] [-w] [var ...] +// go env [-json] [-changed] [-u] [-w] [var ...] // // Env prints Go environment information. // diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index 5e5d53903395cf..cf7b873f167650 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -196,7 +196,7 @@ func TestMain(m *testing.M) { defer removeAll(testTmpDir) } - testGOCACHE = cache.DefaultDir() + testGOCACHE, _ = cache.DefaultDir() if testenv.HasGoBuild() { testBin = filepath.Join(testTmpDir, "testbin") if err := os.Mkdir(testBin, 0777); err != nil { diff --git a/src/cmd/go/internal/bug/bug.go b/src/cmd/go/internal/bug/bug.go index ed1813605e5de4..d3f9065d3da4e5 100644 --- a/src/cmd/go/internal/bug/bug.go +++ b/src/cmd/go/internal/bug/bug.go @@ -106,7 +106,7 @@ func printGoEnv(w io.Writer) { env := envcmd.MkEnv() env = append(env, envcmd.ExtraEnvVars()...) env = append(env, envcmd.ExtraEnvVarsCostly()...) - envcmd.PrintEnv(w, env) + envcmd.PrintEnv(w, env, false) } func printGoDetails(w io.Writer) { diff --git a/src/cmd/go/internal/cache/default.go b/src/cmd/go/internal/cache/default.go index b5650eac669b46..cd8c9cf3a57593 100644 --- a/src/cmd/go/internal/cache/default.go +++ b/src/cmd/go/internal/cache/default.go @@ -39,7 +39,7 @@ See golang.org to learn more about Go. // initDefaultCache does the work of finding the default cache // the first time Default is called. func initDefaultCache() { - dir := DefaultDir() + dir, _ := DefaultDir() if dir == "off" { if defaultDirErr != nil { base.Fatalf("build cache is required, but could not be located: %v", defaultDirErr) @@ -67,14 +67,15 @@ func initDefaultCache() { } var ( - defaultDirOnce sync.Once - defaultDir string - defaultDirErr error + defaultDirOnce sync.Once + defaultDir string + defaultDirChanged bool + defaultDirErr error ) // DefaultDir returns the effective GOCACHE setting. // It returns "off" if the cache is disabled. -func DefaultDir() string { +func DefaultDir() (string, bool) { // Save the result of the first call to DefaultDir for later use in // initDefaultCache. cmd/go/main.go explicitly sets GOCACHE so that // subprocesses will inherit it, but that means initDefaultCache can't @@ -83,10 +84,12 @@ func DefaultDir() string { defaultDirOnce.Do(func() { defaultDir = cfg.Getenv("GOCACHE") if filepath.IsAbs(defaultDir) || defaultDir == "off" { + defaultDirChanged = true return } if defaultDir != "" { defaultDir = "off" + defaultDirChanged = true defaultDirErr = fmt.Errorf("GOCACHE is not an absolute path") return } @@ -101,5 +104,5 @@ func DefaultDir() string { defaultDir = filepath.Join(dir, "go-build") }) - return defaultDir + return defaultDir, defaultDirChanged } diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index afb595a0c6aa4c..efee390a6c02d6 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -101,7 +101,8 @@ var ( // GoPathError is set when GOPATH is not set. it contains an // explanation why GOPATH is unset. - GoPathError string + GoPathError string + GOPATHChanged bool ) func defaultContext() build.Context { @@ -111,7 +112,7 @@ func defaultContext() build.Context { // Override defaults computed in go/build with defaults // from go environment configuration file, if known. - ctxt.GOPATH = envOr("GOPATH", gopath(ctxt)) + ctxt.GOPATH, GOPATHChanged = EnvOrAndChanged("GOPATH", gopath(ctxt)) ctxt.GOOS = Goos ctxt.GOARCH = Goarch @@ -262,8 +263,9 @@ func init() { // An EnvVar is an environment variable Name=Value. type EnvVar struct { - Name string - Value string + Name string + Value string + Changed bool } // OrigEnv is the original environment of the program at startup. @@ -275,31 +277,32 @@ 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. -func EnvFile() (string, error) { +func EnvFile() (string, bool, error) { if file := os.Getenv("GOENV"); file != "" { if file == "off" { - return "", fmt.Errorf("GOENV=off") + return "", true, fmt.Errorf("GOENV=off") } - return file, nil + return file, true, nil } dir, err := os.UserConfigDir() if err != nil { - return "", err + return "", false, err } if dir == "" { - return "", fmt.Errorf("missing user-config dir") + return "", false, fmt.Errorf("missing user-config dir") } - return filepath.Join(dir, "go/env"), nil + return filepath.Join(dir, "go/env"), false, nil } func initEnvCache() { - envCache.m = make(map[string]string) - if file, _ := EnvFile(); file != "" { + envCache.m, envCache.goroot = make(map[string]string), make(map[string]string) + if file, _, _ := EnvFile(); file != "" { readEnvFile(file, "user") } goroot := findGOROOT(envCache.m["GOROOT"]) @@ -350,6 +353,7 @@ func readEnvFile(file string, source string) { if _, ok := envCache.m[string(key)]; ok { continue } + envCache.goroot[string(key)] = string(val) } envCache.m[string(key)] = string(val) } @@ -397,57 +401,67 @@ var ( GOROOTpkg string GOROOTsrc string - GOBIN = Getenv("GOBIN") - GOMODCACHE = envOr("GOMODCACHE", gopathDir("pkg/mod")) + GOROOT_FINAL string + + GOBIN = Getenv("GOBIN") + GOMODCACHE, GOMODCACHEChanged = EnvOrAndChanged("GOMODCACHE", gopathDir("pkg/mod")) // Used in envcmd.MkEnv and build ID computations. - GOARM = envOr("GOARM", fmt.Sprint(buildcfg.GOARM)) - GOARM64 = envOr("GOARM64", fmt.Sprint(buildcfg.GOARM64)) - GO386 = envOr("GO386", buildcfg.GO386) - GOAMD64 = envOr("GOAMD64", fmt.Sprintf("%s%d", "v", buildcfg.GOAMD64)) - GOMIPS = envOr("GOMIPS", buildcfg.GOMIPS) - GOMIPS64 = envOr("GOMIPS64", buildcfg.GOMIPS64) - GOPPC64 = envOr("GOPPC64", fmt.Sprintf("%s%d", "power", buildcfg.GOPPC64)) - GORISCV64 = envOr("GORISCV64", fmt.Sprintf("rva%du64", buildcfg.GORISCV64)) - GOWASM = envOr("GOWASM", fmt.Sprint(buildcfg.GOWASM)) - - GOPROXY = envOr("GOPROXY", "") - GOSUMDB = envOr("GOSUMDB", "") - GOPRIVATE = Getenv("GOPRIVATE") - GONOPROXY = envOr("GONOPROXY", GOPRIVATE) - GONOSUMDB = envOr("GONOSUMDB", GOPRIVATE) - GOINSECURE = Getenv("GOINSECURE") - GOVCS = Getenv("GOVCS") + GOARM64, goARM64Changed = EnvOrAndChanged("GOARM64", fmt.Sprint(buildcfg.GOARM64)) + GOARM, goARMChanged = EnvOrAndChanged("GOARM", fmt.Sprint(buildcfg.GOARM)) + GO386, go386Changed = EnvOrAndChanged("GO386", buildcfg.GO386) + GOAMD64, goAMD64Changed = EnvOrAndChanged("GOAMD64", fmt.Sprintf("%s%d", "v", buildcfg.GOAMD64)) + GOMIPS, goMIPSChanged = EnvOrAndChanged("GOMIPS", buildcfg.GOMIPS) + GOMIPS64, goMIPS64Changed = EnvOrAndChanged("GOMIPS64", buildcfg.GOMIPS64) + GOPPC64, goPPC64Changed = EnvOrAndChanged("GOPPC64", fmt.Sprintf("%s%d", "power", buildcfg.GOPPC64)) + GORISCV64, gORISCV64Changed = EnvOrAndChanged("GORISCV64", fmt.Sprintf("rva%du64", buildcfg.GORISCV64)) + GOWASM, goWASMChanged = EnvOrAndChanged("GOWASM", fmt.Sprint(buildcfg.GOWASM)) + + GOPROXY, GOPROXYChanged = EnvOrAndChanged("GOPROXY", "") + GOSUMDB, GOSUMDBChanged = EnvOrAndChanged("GOSUMDB", "") + GOPRIVATE = Getenv("GOPRIVATE") + GONOPROXY, GONOPROXYChanged = EnvOrAndChanged("GONOPROXY", GOPRIVATE) + GONOSUMDB, GONOSUMDBChanged = EnvOrAndChanged("GONOSUMDB", GOPRIVATE) + GOINSECURE = Getenv("GOINSECURE") + GOVCS = Getenv("GOVCS") ) +func EnvOrAndChanged(name, def string) (string, bool) { + val := Getenv(name) + if val != "" { + return val, envCache.goroot[name] != val + } + return def, false +} + var SumdbDir = gopathDir("pkg/sumdb") // GetArchEnv returns the name and setting of the // GOARCH-specific architecture environment variable. // If the current architecture has no GOARCH-specific variable, // GetArchEnv returns empty key and value. -func GetArchEnv() (key, val string) { +func GetArchEnv() (key, val string, changed bool) { switch Goarch { case "arm": - return "GOARM", GOARM + return "GOARM", GOARM, goARMChanged case "arm64": - return "GOARM64", GOARM64 + return "GOARM64", GOARM64, goARM64Changed case "386": - return "GO386", GO386 + return "GO386", GO386, go386Changed case "amd64": - return "GOAMD64", GOAMD64 + return "GOAMD64", GOAMD64, goAMD64Changed case "mips", "mipsle": - return "GOMIPS", GOMIPS + return "GOMIPS", GOMIPS, goMIPSChanged case "mips64", "mips64le": - return "GOMIPS64", GOMIPS64 + return "GOMIPS64", GOMIPS64, goMIPS64Changed case "ppc64", "ppc64le": - return "GOPPC64", GOPPC64 + return "GOPPC64", GOPPC64, goPPC64Changed case "riscv64": - return "GORISCV64", GORISCV64 + return "GORISCV64", GORISCV64, gORISCV64Changed case "wasm": - return "GOWASM", GOWASM + return "GOWASM", GOWASM, goWASMChanged } - return "", "" + return "", "", false } // envOr returns Getenv(key) if set, or else def. diff --git a/src/cmd/go/internal/clean/clean.go b/src/cmd/go/internal/clean/clean.go index b021b784dada5c..de2ef9dcb957bb 100644 --- a/src/cmd/go/internal/clean/clean.go +++ b/src/cmd/go/internal/clean/clean.go @@ -153,7 +153,7 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) { sh := work.NewShell("", fmt.Print) if cleanCache { - dir := cache.DefaultDir() + dir, _ := cache.DefaultDir() if dir != "off" { // Remove the cache subdirectories but not the top cache directory. // 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) { // Instead of walking through the entire cache looking for test results, // we write a file to the cache indicating that all test results from before // right now are to be ignored. - dir := cache.DefaultDir() + dir, _ := cache.DefaultDir() if dir != "off" { f, err := lockedfile.Edit(filepath.Join(dir, "testexpire.txt")) if err == nil { diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index bff3fe5d55be08..27dce5a76a5d44 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -32,7 +32,7 @@ import ( ) var CmdEnv = &base.Command{ - UsageLine: "go env [-json] [-u] [-w] [var ...]", + UsageLine: "go env [-json] [-changed] [-u] [-w] [var ...]", Short: "print Go environment information", Long: ` Env prints Go environment information. @@ -64,19 +64,20 @@ func init() { } var ( - envJson = CmdEnv.Flag.Bool("json", false, "") - envU = CmdEnv.Flag.Bool("u", false, "") - envW = CmdEnv.Flag.Bool("w", false, "") + envJson = CmdEnv.Flag.Bool("json", false, "") + envU = CmdEnv.Flag.Bool("u", false, "") + envW = CmdEnv.Flag.Bool("w", false, "") + envChanged = CmdEnv.Flag.Bool("changed", false, "") ) func MkEnv() []cfg.EnvVar { - envFile, _ := cfg.EnvFile() + envFile, envFileChanged, _ := cfg.EnvFile() env := []cfg.EnvVar{ {Name: "GO111MODULE", Value: cfg.Getenv("GO111MODULE")}, {Name: "GOARCH", Value: cfg.Goarch}, {Name: "GOBIN", Value: cfg.GOBIN}, - {Name: "GOCACHE", Value: cache.DefaultDir()}, - {Name: "GOENV", Value: envFile}, + {Name: "GOCACHE"}, + {Name: "GOENV", Value: envFile, Changed: envFileChanged}, {Name: "GOEXE", Value: cfg.ExeSuffix}, // List the raw value of GOEXPERIMENT, not the cleaned one. @@ -90,15 +91,15 @@ func MkEnv() []cfg.EnvVar { {Name: "GOHOSTARCH", Value: runtime.GOARCH}, {Name: "GOHOSTOS", Value: runtime.GOOS}, {Name: "GOINSECURE", Value: cfg.GOINSECURE}, - {Name: "GOMODCACHE", Value: cfg.GOMODCACHE}, - {Name: "GONOPROXY", Value: cfg.GONOPROXY}, - {Name: "GONOSUMDB", Value: cfg.GONOSUMDB}, + {Name: "GOMODCACHE", Value: cfg.GOMODCACHE, Changed: cfg.GOMODCACHEChanged}, + {Name: "GONOPROXY", Value: cfg.GONOPROXY, Changed: cfg.GONOPROXYChanged}, + {Name: "GONOSUMDB", Value: cfg.GONOSUMDB, Changed: cfg.GONOSUMDBChanged}, {Name: "GOOS", Value: cfg.Goos}, - {Name: "GOPATH", Value: cfg.BuildContext.GOPATH}, + {Name: "GOPATH", Value: cfg.BuildContext.GOPATH, Changed: cfg.GOPATHChanged}, {Name: "GOPRIVATE", Value: cfg.GOPRIVATE}, - {Name: "GOPROXY", Value: cfg.GOPROXY}, + {Name: "GOPROXY", Value: cfg.GOPROXY, Changed: cfg.GOPROXYChanged}, {Name: "GOROOT", Value: cfg.GOROOT}, - {Name: "GOSUMDB", Value: cfg.GOSUMDB}, + {Name: "GOSUMDB", Value: cfg.GOSUMDB, Changed: cfg.GOSUMDBChanged}, {Name: "GOTMPDIR", Value: cfg.Getenv("GOTMPDIR")}, {Name: "GOTOOLCHAIN", Value: cfg.Getenv("GOTOOLCHAIN")}, {Name: "GOTOOLDIR", Value: build.ToolDir}, @@ -107,31 +108,63 @@ func MkEnv() []cfg.EnvVar { {Name: "GODEBUG", Value: os.Getenv("GODEBUG")}, } + for i := range env { + switch env[i].Name { + case "GO111MODULE": + if env[i].Value != "on" && env[i].Value != "" { + env[i].Changed = true + } + case "GOARCH": + if env[i].Value != runtime.GOARCH { + env[i].Changed = true + } + case "GOBIN", "GOEXPERIMENT", "GOFLAGS", "GOINSECURE", "GOPRIVATE", "GOTMPDIR", "GOVCS": + if env[i].Value != "" { + env[i].Changed = true + } + case "GOCACHE": + env[i].Value, env[i].Changed = cache.DefaultDir() + case "GOOS": + if env[i].Value != runtime.GOOS { + env[i].Changed = true + } + case "GOTOOLCHAIN": + if env[i].Value != "auto" { + env[i].Changed = true + } + } + } + if work.GccgoBin != "" { - env = append(env, cfg.EnvVar{Name: "GCCGO", Value: work.GccgoBin}) + env = append(env, cfg.EnvVar{Name: "GCCGO", Value: work.GccgoBin, Changed: true}) } else { env = append(env, cfg.EnvVar{Name: "GCCGO", Value: work.GccgoName}) } - key, val := cfg.GetArchEnv() + key, val, changde := cfg.GetArchEnv() if key != "" { - env = append(env, cfg.EnvVar{Name: key, Value: val}) + env = append(env, cfg.EnvVar{Name: key, Value: val, Changed: changde}) } cc := cfg.Getenv("CC") + ccChanged := true if cc == "" { + ccChanged = false cc = cfg.DefaultCC(cfg.Goos, cfg.Goarch) } cxx := cfg.Getenv("CXX") + cxxChanged := true if cxx == "" { + cxxChanged = false cxx = cfg.DefaultCXX(cfg.Goos, cfg.Goarch) } - env = append(env, cfg.EnvVar{Name: "AR", Value: envOr("AR", "ar")}) - env = append(env, cfg.EnvVar{Name: "CC", Value: cc}) - env = append(env, cfg.EnvVar{Name: "CXX", Value: cxx}) + ar, arChanged := cfg.EnvOrAndChanged("AR", "ar") + env = append(env, cfg.EnvVar{Name: "AR", Value: ar, Changed: arChanged}) + env = append(env, cfg.EnvVar{Name: "CC", Value: cc, Changed: ccChanged}) + env = append(env, cfg.EnvVar{Name: "CXX", Value: cxx, Changed: cxxChanged}) if cfg.BuildContext.CgoEnabled { - env = append(env, cfg.EnvVar{Name: "CGO_ENABLED", Value: "1"}) + env = append(env, cfg.EnvVar{Name: "CGO_ENABLED", Value: "1", Changed: true}) } else { env = append(env, cfg.EnvVar{Name: "CGO_ENABLED", Value: "0"}) } @@ -139,14 +172,6 @@ func MkEnv() []cfg.EnvVar { return env } -func envOr(name, def string) string { - val := cfg.Getenv(name) - if val != "" { - return val - } - return def -} - func findEnv(env []cfg.EnvVar, name string) string { for _, e := range env { if e.Name == name { @@ -208,12 +233,12 @@ func ExtraEnvVarsCostly() []cfg.EnvVar { return []cfg.EnvVar{ // Note: Update the switch in runEnv below when adding to this list. - {Name: "CGO_CFLAGS", Value: join(cflags)}, - {Name: "CGO_CPPFLAGS", Value: join(cppflags)}, - {Name: "CGO_CXXFLAGS", Value: join(cxxflags)}, - {Name: "CGO_FFLAGS", Value: join(fflags)}, - {Name: "CGO_LDFLAGS", Value: join(ldflags)}, - {Name: "PKG_CONFIG", Value: b.PkgconfigCmd()}, + {Name: "CGO_CFLAGS", Value: join(cflags), Changed: cfg.Getenv("CGO_CFLAGS") != ""}, + {Name: "CGO_CPPFLAGS", Value: join(cppflags), Changed: cfg.Getenv("CGO_CPPFLAGS") != ""}, + {Name: "CGO_CXXFLAGS", Value: join(cxxflags), Changed: cfg.Getenv("CGO_CXXFLAGS") != ""}, + {Name: "CGO_FFLAGS", Value: join(fflags), Changed: cfg.Getenv("CGO_FFLAGS") != ""}, + {Name: "CGO_LDFLAGS", Value: join(ldflags), Changed: cfg.Getenv("CGO_LDFLAGS") != ""}, + {Name: "PKG_CONFIG", Value: b.PkgconfigCmd(), Changed: cfg.Getenv("PKG_CONFIG") != ""}, {Name: "GOGCCFLAGS", Value: join(cmd[3:])}, } } @@ -296,6 +321,16 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) { env = append(env, ExtraEnvVarsCostly()...) } + if *envChanged { + if *envJson { + printEnvAsJSON(env, true) + return + } + + PrintEnv(os.Stdout, env, true) + return + } + if len(args) > 0 { if *envJson { var es []cfg.EnvVar @@ -303,7 +338,7 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) { e := cfg.EnvVar{Name: name, Value: findEnv(env, name)} es = append(es, e) } - printEnvAsJSON(es) + printEnvAsJSON(es, false) } else { for _, name := range args { fmt.Printf("%s\n", findEnv(env, name)) @@ -313,11 +348,11 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) { } if *envJson { - printEnvAsJSON(env) + printEnvAsJSON(env, false) return } - PrintEnv(os.Stdout, env) + PrintEnv(os.Stdout, env, false) } func runEnvW(args []string) { @@ -423,12 +458,15 @@ func checkBuildConfig(add map[string]string, del map[string]bool) error { } // PrintEnv prints the environment variables to w. -func PrintEnv(w io.Writer, env []cfg.EnvVar) { +func PrintEnv(w io.Writer, env []cfg.EnvVar, onlyChanged bool) { for _, e := range env { if e.Name != "TERM" { if runtime.GOOS != "plan9" && bytes.Contains([]byte(e.Value), []byte{0}) { base.Fatalf("go: internal error: encountered null byte in environment variable %s on non-plan9 platform", e.Name) } + if onlyChanged && !e.Changed { + continue + } switch runtime.GOOS { default: fmt.Fprintf(w, "%s=%s\n", e.Name, shellQuote(e.Value)) @@ -503,12 +541,15 @@ func batchEscape(s string) string { return b.String() } -func printEnvAsJSON(env []cfg.EnvVar) { +func printEnvAsJSON(env []cfg.EnvVar, onlyChanged bool) { m := make(map[string]string) for _, e := range env { if e.Name == "TERM" { continue } + if onlyChanged && !e.Changed { + break + } m[e.Name] = e.Value } enc := json.NewEncoder(os.Stdout) @@ -591,7 +632,7 @@ func checkEnvWrite(key, val string) error { } func readEnvFileLines(mustExist bool) []string { - file, err := cfg.EnvFile() + file, _, err := cfg.EnvFile() if file == "" { if mustExist { base.Fatalf("go: cannot find go env config: %v", err) @@ -655,7 +696,7 @@ func updateEnvFile(add map[string]string, del map[string]bool) { } } - file, err := cfg.EnvFile() + file, _, err := cfg.EnvFile() if file == "" { base.Fatalf("go: cannot find go env config: %v", err) } diff --git a/src/cmd/go/internal/envcmd/env_test.go b/src/cmd/go/internal/envcmd/env_test.go index 7419cf3fc20e73..2f6470b87165db 100644 --- a/src/cmd/go/internal/envcmd/env_test.go +++ b/src/cmd/go/internal/envcmd/env_test.go @@ -56,7 +56,7 @@ func FuzzPrintEnvEscape(f *testing.F) { if runtime.GOOS == "windows" { b.WriteString("@echo off\n") } - PrintEnv(&b, []cfg.EnvVar{{Name: "var", Value: s}}) + PrintEnv(&b, []cfg.EnvVar{{Name: "var", Value: s}}, false) var want string if runtime.GOOS == "windows" { fmt.Fprintf(&b, "echo \"%%var%%\"\n") diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index f241e93ee8b498..8841d97e5e85a9 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -2437,7 +2437,7 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) { appendSetting("GOEXPERIMENT", cfg.RawGOEXPERIMENT) } appendSetting("GOOS", cfg.BuildContext.GOOS) - if key, val := cfg.GetArchEnv(); key != "" && val != "" { + if key, val, _ := cfg.GetArchEnv(); key != "" && val != "" { appendSetting(key, val) } diff --git a/src/cmd/go/internal/modindex/read.go b/src/cmd/go/internal/modindex/read.go index bda3fb4338c49e..9d8c48f2b04623 100644 --- a/src/cmd/go/internal/modindex/read.go +++ b/src/cmd/go/internal/modindex/read.go @@ -147,7 +147,8 @@ func GetPackage(modroot, pkgdir string) (*IndexPackage, error) { // using the index, for instance because the index is disabled, or the package // is not in a module. func GetModule(modroot string) (*Module, error) { - if !enabled || cache.DefaultDir() == "off" { + dir, _ := cache.DefaultDir() + if !enabled || dir == "off" { return nil, errDisabled } if modroot == "" { diff --git a/src/cmd/go/internal/test/test.go b/src/cmd/go/internal/test/test.go index ac9d2721f5e781..a13070a91e7b04 100644 --- a/src/cmd/go/internal/test/test.go +++ b/src/cmd/go/internal/test/test.go @@ -837,7 +837,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) { // Read testcache expiration time, if present. // (We implement go clean -testcache by writing an expiration date // instead of searching out and deleting test result cache entries.) - if dir := cache.DefaultDir(); dir != "off" { + if dir, _ := cache.DefaultDir(); dir != "off" { if data, _ := lockedfile.Read(filepath.Join(dir, "testexpire.txt")); len(data) > 0 && data[len(data)-1] == '\n' { if t, err := strconv.ParseInt(string(data[:len(data)-1]), 10, 64); err == nil { testCacheExpire = time.Unix(0, t) diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go index a3d1533899431c..7edfac7ab4dbaa 100644 --- a/src/cmd/go/internal/work/exec.go +++ b/src/cmd/go/internal/work/exec.go @@ -337,7 +337,7 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID { } // GOARM, GOMIPS, etc. - key, val := cfg.GetArchEnv() + key, val, _ := cfg.GetArchEnv() fmt.Fprintf(h, "%s=%s\n", key, val) if cfg.CleanGOEXPERIMENT != "" { @@ -1412,7 +1412,7 @@ func (b *Builder) printLinkerConfig(h io.Writer, p *load.Package) { } // GOARM, GOMIPS, etc. - key, val := cfg.GetArchEnv() + key, val, _ := cfg.GetArchEnv() fmt.Fprintf(h, "%s=%s\n", key, val) if cfg.CleanGOEXPERIMENT != "" { diff --git a/src/cmd/go/internal/work/shell.go b/src/cmd/go/internal/work/shell.go index 60817d9c3b3e9b..1fac8e3a45e8e2 100644 --- a/src/cmd/go/internal/work/shell.go +++ b/src/cmd/go/internal/work/shell.go @@ -114,7 +114,8 @@ func (sh *Shell) moveOrCopyFile(dst, src string, perm fs.FileMode, force bool) e // Otherwise fall back to standard copy. // If the source is in the build cache, we need to copy it. - if strings.HasPrefix(src, cache.DefaultDir()) { + dir, _ := cache.DefaultDir() + if strings.HasPrefix(src, dir) { return sh.CopyFile(dst, src, perm, force) } diff --git a/src/cmd/go/testdata/counters.txt b/src/cmd/go/testdata/counters.txt new file mode 100644 index 00000000000000..6ac1bccff979e1 --- /dev/null +++ b/src/cmd/go/testdata/counters.txt @@ -0,0 +1,690 @@ +go/flag:C +go/subcommand:unknown +go/flag:fixdocs +go/flag:fixreadme +go/flag:flaky +go/flag:proxy +go/flag:test.bench +go/flag:test.benchmem +go/flag:test.benchtime +go/flag:test.blockprofile +go/flag:test.blockprofilerate +go/flag:test.count +go/flag:test.coverprofile +go/flag:test.cpu +go/flag:test.cpuprofile +go/flag:test.failfast +go/flag:test.fullpath +go/flag:test.fuzz +go/flag:test.fuzzcachedir +go/flag:test.fuzzminimizetime +go/flag:test.fuzztime +go/flag:test.fuzzworker +go/flag:test.gocoverdir +go/flag:test.list +go/flag:test.memprofile +go/flag:test.memprofilerate +go/flag:test.mutexprofile +go/flag:test.mutexprofilefraction +go/flag:test.outputdir +go/flag:test.paniconexit0 +go/flag:test.parallel +go/flag:test.run +go/flag:test.short +go/flag:test.shuffle +go/flag:test.skip +go/flag:test.testlogfile +go/flag:test.timeout +go/flag:test.trace +go/flag:test.v +go/flag:testsum +go/flag:testwork +go/flag:update +go/subcommand:help +go/subcommand:bug +go/flag:bug-C +go/flag:bug-v +go/subcommand:help-bug +go/subcommand:build +go/flag:build-C +go/flag:build-a +go/flag:build-asan +go/flag:build-asmflags +go/flag:build-buildmode +go/flag:build-buildvcs +go/flag:build-compiler +go/flag:build-cover +go/flag:build-covermode +go/flag:build-coverpkg +go/flag:build-debug-actiongraph +go/flag:build-debug-runtime-trace +go/flag:build-debug-trace +go/flag:build-gccgoflags +go/flag:build-gcflags +go/flag:build-installsuffix +go/flag:build-ldflags +go/flag:build-linkshared +go/flag:build-mod +go/flag:build-modcacherw +go/flag:build-modfile +go/flag:build-msan +go/flag:build-n +go/flag:build-o +go/flag:build-overlay +go/flag:build-p +go/flag:build-pgo +go/flag:build-pkgdir +go/flag:build-race +go/flag:build-tags +go/flag:build-toolexec +go/flag:build-trimpath +go/flag:build-v +go/flag:build-work +go/flag:build-x +go/subcommand:help-build +go/subcommand:clean +go/flag:clean-C +go/flag:clean-a +go/flag:clean-asan +go/flag:clean-asmflags +go/flag:clean-buildmode +go/flag:clean-buildvcs +go/flag:clean-cache +go/flag:clean-compiler +go/flag:clean-debug-actiongraph +go/flag:clean-debug-runtime-trace +go/flag:clean-debug-trace +go/flag:clean-fuzzcache +go/flag:clean-gccgoflags +go/flag:clean-gcflags +go/flag:clean-i +go/flag:clean-installsuffix +go/flag:clean-ldflags +go/flag:clean-linkshared +go/flag:clean-mod +go/flag:clean-modcache +go/flag:clean-modcacherw +go/flag:clean-modfile +go/flag:clean-msan +go/flag:clean-n +go/flag:clean-overlay +go/flag:clean-p +go/flag:clean-pgo +go/flag:clean-pkgdir +go/flag:clean-r +go/flag:clean-race +go/flag:clean-tags +go/flag:clean-testcache +go/flag:clean-toolexec +go/flag:clean-trimpath +go/flag:clean-v +go/flag:clean-work +go/flag:clean-x +go/subcommand:help-clean +go/subcommand:doc +go/subcommand:help-doc +go/subcommand:env +go/flag:env-C +go/flag:env-changed +go/flag:env-json +go/flag:env-n +go/flag:env-u +go/flag:env-w +go/flag:env-x +go/subcommand:help-env +go/subcommand:fix +go/flag:fix-C +go/flag:fix-a +go/flag:fix-asan +go/flag:fix-asmflags +go/flag:fix-buildmode +go/flag:fix-buildvcs +go/flag:fix-compiler +go/flag:fix-debug-actiongraph +go/flag:fix-debug-runtime-trace +go/flag:fix-debug-trace +go/flag:fix-fix +go/flag:fix-gccgoflags +go/flag:fix-gcflags +go/flag:fix-installsuffix +go/flag:fix-ldflags +go/flag:fix-linkshared +go/flag:fix-mod +go/flag:fix-modcacherw +go/flag:fix-modfile +go/flag:fix-msan +go/flag:fix-n +go/flag:fix-overlay +go/flag:fix-p +go/flag:fix-pgo +go/flag:fix-pkgdir +go/flag:fix-race +go/flag:fix-tags +go/flag:fix-toolexec +go/flag:fix-trimpath +go/flag:fix-v +go/flag:fix-work +go/flag:fix-x +go/subcommand:help-fix +go/subcommand:fmt +go/flag:fmt-C +go/flag:fmt-mod +go/flag:fmt-modcacherw +go/flag:fmt-modfile +go/flag:fmt-n +go/flag:fmt-overlay +go/flag:fmt-x +go/subcommand:help-fmt +go/subcommand:generate +go/flag:generate-C +go/flag:generate-a +go/flag:generate-asan +go/flag:generate-asmflags +go/flag:generate-buildmode +go/flag:generate-buildvcs +go/flag:generate-compiler +go/flag:generate-debug-actiongraph +go/flag:generate-debug-runtime-trace +go/flag:generate-debug-trace +go/flag:generate-gccgoflags +go/flag:generate-gcflags +go/flag:generate-installsuffix +go/flag:generate-ldflags +go/flag:generate-linkshared +go/flag:generate-mod +go/flag:generate-modcacherw +go/flag:generate-modfile +go/flag:generate-msan +go/flag:generate-n +go/flag:generate-overlay +go/flag:generate-p +go/flag:generate-pgo +go/flag:generate-pkgdir +go/flag:generate-race +go/flag:generate-run +go/flag:generate-skip +go/flag:generate-tags +go/flag:generate-toolexec +go/flag:generate-trimpath +go/flag:generate-v +go/flag:generate-work +go/flag:generate-x +go/subcommand:help-generate +go/subcommand:get +go/flag:get-C +go/flag:get-a +go/flag:get-asan +go/flag:get-asmflags +go/flag:get-buildmode +go/flag:get-buildvcs +go/flag:get-compiler +go/flag:get-d +go/flag:get-debug-actiongraph +go/flag:get-debug-runtime-trace +go/flag:get-debug-trace +go/flag:get-f +go/flag:get-fix +go/flag:get-gccgoflags +go/flag:get-gcflags +go/flag:get-insecure +go/flag:get-installsuffix +go/flag:get-ldflags +go/flag:get-linkshared +go/flag:get-m +go/flag:get-modcacherw +go/flag:get-modfile +go/flag:get-msan +go/flag:get-n +go/flag:get-overlay +go/flag:get-p +go/flag:get-pgo +go/flag:get-pkgdir +go/flag:get-race +go/flag:get-t +go/flag:get-tags +go/flag:get-toolexec +go/flag:get-trimpath +go/flag:get-u +go/flag:get-v +go/flag:get-work +go/flag:get-x +go/subcommand:help-get +go/subcommand:install +go/flag:install-C +go/flag:install-a +go/flag:install-asan +go/flag:install-asmflags +go/flag:install-buildmode +go/flag:install-buildvcs +go/flag:install-compiler +go/flag:install-cover +go/flag:install-covermode +go/flag:install-coverpkg +go/flag:install-debug-actiongraph +go/flag:install-debug-runtime-trace +go/flag:install-debug-trace +go/flag:install-gccgoflags +go/flag:install-gcflags +go/flag:install-installsuffix +go/flag:install-ldflags +go/flag:install-linkshared +go/flag:install-mod +go/flag:install-modcacherw +go/flag:install-modfile +go/flag:install-msan +go/flag:install-n +go/flag:install-overlay +go/flag:install-p +go/flag:install-pgo +go/flag:install-pkgdir +go/flag:install-race +go/flag:install-tags +go/flag:install-toolexec +go/flag:install-trimpath +go/flag:install-v +go/flag:install-work +go/flag:install-x +go/subcommand:help-install +go/subcommand:list +go/flag:list-C +go/flag:list-a +go/flag:list-asan +go/flag:list-asmflags +go/flag:list-buildmode +go/flag:list-buildvcs +go/flag:list-compiled +go/flag:list-compiler +go/flag:list-cover +go/flag:list-covermode +go/flag:list-coverpkg +go/flag:list-debug-actiongraph +go/flag:list-debug-runtime-trace +go/flag:list-debug-trace +go/flag:list-deps +go/flag:list-e +go/flag:list-export +go/flag:list-f +go/flag:list-find +go/flag:list-gccgoflags +go/flag:list-gcflags +go/flag:list-installsuffix +go/flag:list-json +go/flag:list-ldflags +go/flag:list-linkshared +go/flag:list-m +go/flag:list-mod +go/flag:list-modcacherw +go/flag:list-modfile +go/flag:list-msan +go/flag:list-n +go/flag:list-overlay +go/flag:list-p +go/flag:list-pgo +go/flag:list-pkgdir +go/flag:list-race +go/flag:list-retracted +go/flag:list-reuse +go/flag:list-tags +go/flag:list-test +go/flag:list-toolexec +go/flag:list-trimpath +go/flag:list-u +go/flag:list-v +go/flag:list-versions +go/flag:list-work +go/flag:list-x +go/subcommand:help-list +go/subcommand:help-mod +go/subcommand:mod-download +go/flag:mod-download-C +go/flag:mod-download-json +go/flag:mod-download-modcacherw +go/flag:mod-download-modfile +go/flag:mod-download-overlay +go/flag:mod-download-reuse +go/flag:mod-download-x +go/subcommand:mod-help-download +go/subcommand:help-mod-download +go/subcommand:mod-edit +go/flag:mod-edit-C +go/flag:mod-edit-dropexclude +go/flag:mod-edit-dropreplace +go/flag:mod-edit-droprequire +go/flag:mod-edit-dropretract +go/flag:mod-edit-exclude +go/flag:mod-edit-fmt +go/flag:mod-edit-go +go/flag:mod-edit-json +go/flag:mod-edit-modcacherw +go/flag:mod-edit-modfile +go/flag:mod-edit-module +go/flag:mod-edit-n +go/flag:mod-edit-overlay +go/flag:mod-edit-print +go/flag:mod-edit-replace +go/flag:mod-edit-require +go/flag:mod-edit-retract +go/flag:mod-edit-toolchain +go/flag:mod-edit-x +go/subcommand:mod-help-edit +go/subcommand:help-mod-edit +go/subcommand:mod-graph +go/flag:mod-graph-C +go/flag:mod-graph-go +go/flag:mod-graph-modcacherw +go/flag:mod-graph-modfile +go/flag:mod-graph-overlay +go/flag:mod-graph-x +go/subcommand:mod-help-graph +go/subcommand:help-mod-graph +go/subcommand:mod-init +go/flag:mod-init-C +go/flag:mod-init-modcacherw +go/flag:mod-init-modfile +go/flag:mod-init-overlay +go/subcommand:mod-help-init +go/subcommand:help-mod-init +go/subcommand:mod-tidy +go/flag:mod-tidy-C +go/flag:mod-tidy-compat +go/flag:mod-tidy-e +go/flag:mod-tidy-go +go/flag:mod-tidy-modcacherw +go/flag:mod-tidy-modfile +go/flag:mod-tidy-overlay +go/flag:mod-tidy-v +go/flag:mod-tidy-x +go/subcommand:mod-help-tidy +go/subcommand:help-mod-tidy +go/subcommand:mod-vendor +go/flag:mod-vendor-C +go/flag:mod-vendor-e +go/flag:mod-vendor-modcacherw +go/flag:mod-vendor-modfile +go/flag:mod-vendor-o +go/flag:mod-vendor-overlay +go/flag:mod-vendor-v +go/subcommand:mod-help-vendor +go/subcommand:help-mod-vendor +go/subcommand:mod-verify +go/flag:mod-verify-C +go/flag:mod-verify-modcacherw +go/flag:mod-verify-modfile +go/flag:mod-verify-overlay +go/subcommand:mod-help-verify +go/subcommand:help-mod-verify +go/subcommand:mod-why +go/flag:mod-why-C +go/flag:mod-why-m +go/flag:mod-why-modcacherw +go/flag:mod-why-modfile +go/flag:mod-why-overlay +go/flag:mod-why-vendor +go/subcommand:mod-help-why +go/subcommand:help-mod-why +go/subcommand:help-work +go/subcommand:work-edit +go/flag:work-edit-C +go/flag:work-edit-dropreplace +go/flag:work-edit-dropuse +go/flag:work-edit-fmt +go/flag:work-edit-go +go/flag:work-edit-json +go/flag:work-edit-print +go/flag:work-edit-replace +go/flag:work-edit-toolchain +go/flag:work-edit-use +go/subcommand:work-help-edit +go/subcommand:help-work-edit +go/subcommand:work-init +go/flag:work-init-C +go/flag:work-init-modcacherw +go/flag:work-init-modfile +go/flag:work-init-overlay +go/subcommand:work-help-init +go/subcommand:help-work-init +go/subcommand:work-sync +go/flag:work-sync-C +go/flag:work-sync-modcacherw +go/flag:work-sync-modfile +go/flag:work-sync-overlay +go/subcommand:work-help-sync +go/subcommand:help-work-sync +go/subcommand:work-use +go/flag:work-use-C +go/flag:work-use-modcacherw +go/flag:work-use-modfile +go/flag:work-use-overlay +go/flag:work-use-r +go/subcommand:work-help-use +go/subcommand:help-work-use +go/subcommand:work-vendor +go/flag:work-vendor-C +go/flag:work-vendor-e +go/flag:work-vendor-modcacherw +go/flag:work-vendor-modfile +go/flag:work-vendor-o +go/flag:work-vendor-overlay +go/flag:work-vendor-v +go/subcommand:work-help-vendor +go/subcommand:help-work-vendor +go/subcommand:run +go/flag:run-C +go/flag:run-a +go/flag:run-asan +go/flag:run-asmflags +go/flag:run-buildmode +go/flag:run-buildvcs +go/flag:run-compiler +go/flag:run-cover +go/flag:run-covermode +go/flag:run-coverpkg +go/flag:run-debug-actiongraph +go/flag:run-debug-runtime-trace +go/flag:run-debug-trace +go/flag:run-exec +go/flag:run-gccgoflags +go/flag:run-gcflags +go/flag:run-installsuffix +go/flag:run-ldflags +go/flag:run-linkshared +go/flag:run-mod +go/flag:run-modcacherw +go/flag:run-modfile +go/flag:run-msan +go/flag:run-n +go/flag:run-overlay +go/flag:run-p +go/flag:run-pgo +go/flag:run-pkgdir +go/flag:run-race +go/flag:run-tags +go/flag:run-toolexec +go/flag:run-trimpath +go/flag:run-v +go/flag:run-work +go/flag:run-x +go/subcommand:help-run +go/subcommand:test +go/flag:test-C +go/flag:test-a +go/flag:test-asan +go/flag:test-asmflags +go/flag:test-bench +go/flag:test-benchmem +go/flag:test-benchtime +go/flag:test-blockprofile +go/flag:test-blockprofilerate +go/flag:test-buildmode +go/flag:test-buildvcs +go/flag:test-c +go/flag:test-compiler +go/flag:test-count +go/flag:test-cover +go/flag:test-covermode +go/flag:test-coverpkg +go/flag:test-coverprofile +go/flag:test-cpu +go/flag:test-cpuprofile +go/flag:test-debug-actiongraph +go/flag:test-debug-runtime-trace +go/flag:test-debug-trace +go/flag:test-exec +go/flag:test-failfast +go/flag:test-fullpath +go/flag:test-fuzz +go/flag:test-fuzzminimizetime +go/flag:test-fuzztime +go/flag:test-gccgoflags +go/flag:test-gcflags +go/flag:test-installsuffix +go/flag:test-json +go/flag:test-ldflags +go/flag:test-linkshared +go/flag:test-list +go/flag:test-memprofile +go/flag:test-memprofilerate +go/flag:test-mod +go/flag:test-modcacherw +go/flag:test-modfile +go/flag:test-msan +go/flag:test-mutexprofile +go/flag:test-mutexprofilefraction +go/flag:test-n +go/flag:test-o +go/flag:test-outputdir +go/flag:test-overlay +go/flag:test-p +go/flag:test-parallel +go/flag:test-pgo +go/flag:test-pkgdir +go/flag:test-race +go/flag:test-run +go/flag:test-short +go/flag:test-shuffle +go/flag:test-skip +go/flag:test-tags +go/flag:test-test.bench +go/flag:test-test.benchmem +go/flag:test-test.benchtime +go/flag:test-test.blockprofile +go/flag:test-test.blockprofilerate +go/flag:test-test.count +go/flag:test-test.coverprofile +go/flag:test-test.cpu +go/flag:test-test.cpuprofile +go/flag:test-test.failfast +go/flag:test-test.fullpath +go/flag:test-test.fuzz +go/flag:test-test.fuzzminimizetime +go/flag:test-test.fuzztime +go/flag:test-test.list +go/flag:test-test.memprofile +go/flag:test-test.memprofilerate +go/flag:test-test.mutexprofile +go/flag:test-test.mutexprofilefraction +go/flag:test-test.outputdir +go/flag:test-test.parallel +go/flag:test-test.run +go/flag:test-test.short +go/flag:test-test.shuffle +go/flag:test-test.skip +go/flag:test-test.timeout +go/flag:test-test.trace +go/flag:test-test.v +go/flag:test-timeout +go/flag:test-toolexec +go/flag:test-trace +go/flag:test-trimpath +go/flag:test-v +go/flag:test-vet +go/flag:test-work +go/flag:test-x +go/subcommand:help-test +go/subcommand:tool-addr2line +go/subcommand:tool-asm +go/subcommand:tool-buildid +go/subcommand:tool-cgo +go/subcommand:tool-compile +go/subcommand:tool-covdata +go/subcommand:tool-cover +go/subcommand:tool-dist +go/subcommand:tool-distpack +go/subcommand:tool-doc +go/subcommand:tool-fix +go/subcommand:tool-link +go/subcommand:tool-nm +go/subcommand:tool-objdump +go/subcommand:tool-pack +go/subcommand:tool-pprof +go/subcommand:tool-preprofile +go/subcommand:tool-test2json +go/subcommand:tool-trace +go/subcommand:tool-vet +go/subcommand:tool-unknown +go/subcommand:tool +go/flag:tool-C +go/flag:tool-n +go/subcommand:help-tool +go/subcommand:version +go/flag:version-C +go/flag:version-m +go/flag:version-v +go/subcommand:help-version +go/subcommand:vet +go/flag:vet-C +go/flag:vet-a +go/flag:vet-asan +go/flag:vet-asmflags +go/flag:vet-buildmode +go/flag:vet-buildvcs +go/flag:vet-compiler +go/flag:vet-debug-actiongraph +go/flag:vet-debug-runtime-trace +go/flag:vet-debug-trace +go/flag:vet-gccgoflags +go/flag:vet-gcflags +go/flag:vet-installsuffix +go/flag:vet-ldflags +go/flag:vet-linkshared +go/flag:vet-mod +go/flag:vet-modcacherw +go/flag:vet-modfile +go/flag:vet-msan +go/flag:vet-n +go/flag:vet-overlay +go/flag:vet-p +go/flag:vet-pgo +go/flag:vet-pkgdir +go/flag:vet-race +go/flag:vet-tags +go/flag:vet-toolexec +go/flag:vet-trimpath +go/flag:vet-v +go/flag:vet-vettool +go/flag:vet-work +go/flag:vet-x +go/subcommand:help-vet +go/subcommand:help-buildconstraint +go/subcommand:help-buildmode +go/subcommand:help-c +go/subcommand:help-cache +go/subcommand:help-environment +go/subcommand:help-filetype +go/subcommand:help-go.mod +go/subcommand:help-gopath +go/subcommand:help-goproxy +go/subcommand:help-importpath +go/subcommand:help-modules +go/subcommand:help-module-auth +go/subcommand:help-packages +go/subcommand:help-private +go/subcommand:help-testflag +go/subcommand:help-testfunc +go/subcommand:help-vcs +go/errors:gomodcache-entry-relative +go/errors:gopath-entry-relative +go/errors:help-unknown-topic +go/errors:invalid-toolchain-in-file +go/toolchain/select-exec +go/toolchain/switch-exec From 2ef3b75f46b3281ed34eda54e6c7376da540a6ff Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Sun, 11 Feb 2024 15:30:32 +0800 Subject: [PATCH 02/28] n Change-Id: Ic6a570aa27e0445e4eb0190fbf221e46c1d84782 --- src/cmd/go/internal/envcmd/env.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index 27dce5a76a5d44..e924997aab10ab 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -548,7 +548,7 @@ func printEnvAsJSON(env []cfg.EnvVar, onlyChanged bool) { continue } if onlyChanged && !e.Changed { - break + continue } m[e.Name] = e.Value } From d9a48ce90bb3d129f5b379d808fa33a991e8dac7 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Sun, 11 Feb 2024 21:04:45 +0800 Subject: [PATCH 03/28] add test / Change-Id: Iee56fe799ceda0a9f650378537c3c195242788c6 --- src/cmd/go/testdata/script/env_changed.txt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/cmd/go/testdata/script/env_changed.txt diff --git a/src/cmd/go/testdata/script/env_changed.txt b/src/cmd/go/testdata/script/env_changed.txt new file mode 100644 index 00000000000000..be64b31b4c0b14 --- /dev/null +++ b/src/cmd/go/testdata/script/env_changed.txt @@ -0,0 +1,18 @@ +# Test query for non-defaults in the env + +env GOTOOLCHAIN=local +env GOSUMDB=nodefault +env GOPROXY=nodefault +env GO111MODULE=auto + +go env -changed +stdout 'GOTOOLCHAIN=local' +stdout 'GOSUMDB=nodefault' +stdout 'GOPROXY=nodefault' +stdout 'GO111MODULE=auto' + +go env -changed -json +stdout '"GOTOOLCHAIN": "local"' +stdout '"GOSUMDB": "nodefault"' +stdout '"GOPROXY": "nodefault"' +stdout '"GO111MODULE": "auto"' From b57f11364922ae5061c667ccacc20d779828d633 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Sun, 11 Feb 2024 21:13:50 +0800 Subject: [PATCH 04/28] fix Change-Id: Ib018bdc5932c25241692c43618e34536847080c1 --- src/cmd/go/testdata/script/env_changed.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd/go/testdata/script/env_changed.txt b/src/cmd/go/testdata/script/env_changed.txt index be64b31b4c0b14..ce6534079ab487 100644 --- a/src/cmd/go/testdata/script/env_changed.txt +++ b/src/cmd/go/testdata/script/env_changed.txt @@ -1,4 +1,4 @@ -# Test query for non-defaults in the env +# Test query for non-defaults in the env env GOTOOLCHAIN=local env GOSUMDB=nodefault From e0dc862ef26e87902d210d5579c6cdb976e6cb0f Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Sun, 11 Feb 2024 21:55:20 +0800 Subject: [PATCH 05/28] n Change-Id: I18ba443f3ae728196b0fe67bf70318ec6f68d559 --- src/cmd/go/alldocs.go | 3 +++ src/cmd/go/internal/envcmd/env.go | 13 +++++-------- src/cmd/go/testdata/script/env_changed.txt | 9 +++++---- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index bb128f8903f3c1..4897d4c66117f4 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -476,6 +476,9 @@ // form NAME=VALUE and changes the default settings // of the named environment variables to the given values. // +// The -changed flag output rusult of +// query for non-defaults in the env +// // For more about environment variables, see 'go help environment'. // // # Update packages to use new APIs diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index e924997aab10ab..0076ccfaf596b6 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -53,6 +53,9 @@ The -w flag requires one or more arguments of the form NAME=VALUE and changes the default settings of the named environment variables to the given values. +The -changed flag output rusult of +query for non-defaults in the env + For more about environment variables, see 'go help environment'. `, } @@ -108,26 +111,20 @@ func MkEnv() []cfg.EnvVar { {Name: "GODEBUG", Value: os.Getenv("GODEBUG")}, } + // See go.dev/issue/34208 + // GOOS and GOARCH are deliberately not set Changed for i := range env { switch env[i].Name { case "GO111MODULE": if env[i].Value != "on" && env[i].Value != "" { env[i].Changed = true } - case "GOARCH": - if env[i].Value != runtime.GOARCH { - env[i].Changed = true - } case "GOBIN", "GOEXPERIMENT", "GOFLAGS", "GOINSECURE", "GOPRIVATE", "GOTMPDIR", "GOVCS": if env[i].Value != "" { env[i].Changed = true } case "GOCACHE": env[i].Value, env[i].Changed = cache.DefaultDir() - case "GOOS": - if env[i].Value != runtime.GOOS { - env[i].Changed = true - } case "GOTOOLCHAIN": if env[i].Value != "auto" { env[i].Changed = true diff --git a/src/cmd/go/testdata/script/env_changed.txt b/src/cmd/go/testdata/script/env_changed.txt index ce6534079ab487..212d1efb60cdd8 100644 --- a/src/cmd/go/testdata/script/env_changed.txt +++ b/src/cmd/go/testdata/script/env_changed.txt @@ -6,10 +6,11 @@ env GOPROXY=nodefault env GO111MODULE=auto go env -changed -stdout 'GOTOOLCHAIN=local' -stdout 'GOSUMDB=nodefault' -stdout 'GOPROXY=nodefault' -stdout 'GO111MODULE=auto' +# LUCI linux-amd64-boringcrypto output like GOTOOLCHAIN='local' +stdout 'GOTOOLCHAIN=local|GOTOOLCHAIN='local'' +stdout 'GOSUMDB=nodefault|GOSUMDB='nodefault'' +stdout 'GOPROXY=nodefault|GOPROXY='nodefault'' +stdout 'GO111MODULE=auto|GO111MODULE='auto'' go env -changed -json stdout '"GOTOOLCHAIN": "local"' From b4405dd7c8b438f94656ea62dd63df9c7080c9bc Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Sun, 11 Feb 2024 22:12:13 +0800 Subject: [PATCH 06/28] fix Change-Id: I2ae131834202615fbb96e67815b2a6f75767c7d5 --- src/cmd/go/testdata/script/env_changed.txt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/cmd/go/testdata/script/env_changed.txt b/src/cmd/go/testdata/script/env_changed.txt index 212d1efb60cdd8..df00585d344877 100644 --- a/src/cmd/go/testdata/script/env_changed.txt +++ b/src/cmd/go/testdata/script/env_changed.txt @@ -7,10 +7,11 @@ env GO111MODULE=auto go env -changed # LUCI linux-amd64-boringcrypto output like GOTOOLCHAIN='local' -stdout 'GOTOOLCHAIN=local|GOTOOLCHAIN='local'' -stdout 'GOSUMDB=nodefault|GOSUMDB='nodefault'' -stdout 'GOPROXY=nodefault|GOPROXY='nodefault'' -stdout 'GO111MODULE=auto|GO111MODULE='auto'' +# LUCI linux-arm64 output like GOTOOLCHAIN='local' +stdout 'GOTOOLCHAIN=local|GOTOOLCHAIN='local'|GOTOOLCHAIN='local'' +stdout 'GOSUMDB=nodefault|GOSUMDB='nodefault'|GOPROXY='nodefault' +stdout 'GOPROXY=nodefault|GOPROXY='nodefault'|GOSUMDB='nodefault'' +stdout 'GO111MODULE=auto|GO111MODULE='auto'|GO111MODULE='auto'' go env -changed -json stdout '"GOTOOLCHAIN": "local"' From f80d4d55d93fe345d882c1e8e20dc0311b79acf9 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Sun, 11 Feb 2024 22:38:38 +0800 Subject: [PATCH 07/28] fix Change-Id: If00590d250794fc474613ad543e0b31a6388609f --- src/cmd/go/testdata/script/env_changed.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cmd/go/testdata/script/env_changed.txt b/src/cmd/go/testdata/script/env_changed.txt index df00585d344877..cfca10691e7a96 100644 --- a/src/cmd/go/testdata/script/env_changed.txt +++ b/src/cmd/go/testdata/script/env_changed.txt @@ -6,12 +6,12 @@ env GOPROXY=nodefault env GO111MODULE=auto go env -changed -# LUCI linux-amd64-boringcrypto output like GOTOOLCHAIN='local' -# LUCI linux-arm64 output like GOTOOLCHAIN='local' -stdout 'GOTOOLCHAIN=local|GOTOOLCHAIN='local'|GOTOOLCHAIN='local'' -stdout 'GOSUMDB=nodefault|GOSUMDB='nodefault'|GOPROXY='nodefault' -stdout 'GOPROXY=nodefault|GOPROXY='nodefault'|GOSUMDB='nodefault'' -stdout 'GO111MODULE=auto|GO111MODULE='auto'|GO111MODULE='auto'' +# linux output like GOTOOLCHAIN='local' +# windows output like GOTOOLCHAIN=local +stdout 'GOTOOLCHAIN=local|GOTOOLCHAIN=''local''' +stdout 'GOSUMDB=nodefault|GOSUMDB=''nodefault''' +stdout 'GOPROXY=nodefault|GOPROXY=''nodefault''' +stdout 'GO111MODULE=auto|GO111MODULE=''auto''' go env -changed -json stdout '"GOTOOLCHAIN": "local"' From 52003f9d90870c799827c0a98dfb9c4c047cfb50 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Mon, 12 Feb 2024 11:25:39 +0800 Subject: [PATCH 08/28] n Change-Id: I7e1706138924f145a5f7b69d7b6163fa37c33aa1 --- src/cmd/go/internal/envcmd/env.go | 17 +++++++++++++++++ src/cmd/go/testdata/script/env_changed.txt | 15 +++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index 0076ccfaf596b6..107194964e9f49 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -169,6 +169,15 @@ func MkEnv() []cfg.EnvVar { return env } +func findCfgEnv(env []cfg.EnvVar, name string) cfg.EnvVar { + for _, e := range env { + if e.Name == name { + return e + } + } + return cfg.EnvVar{} +} + func findEnv(env []cfg.EnvVar, name string) string { for _, e := range env { if e.Name == name { @@ -319,6 +328,14 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) { } if *envChanged { + if len(args) > 0 { + var es []cfg.EnvVar + for _, name := range args { + es = append(es, findCfgEnv(env, name)) + } + env = es + } + if *envJson { printEnvAsJSON(env, true) return diff --git a/src/cmd/go/testdata/script/env_changed.txt b/src/cmd/go/testdata/script/env_changed.txt index cfca10691e7a96..788560289b5249 100644 --- a/src/cmd/go/testdata/script/env_changed.txt +++ b/src/cmd/go/testdata/script/env_changed.txt @@ -18,3 +18,18 @@ stdout '"GOTOOLCHAIN": "local"' stdout '"GOSUMDB": "nodefault"' stdout '"GOPROXY": "nodefault"' stdout '"GO111MODULE": "auto"' + +[GOOS:windows] env GOOS=linux +[!GOOS:windows] env GOOS=windows +[GOARCH:amd64] env GOARCH=arm64 +[!GOARCH:amd64] env GOARCH=amd64 + +go env -changed GOOS +! stdout 'GOOS' +go env -changed GOARCH +! stdout 'GOARCH' + +go env -changed -json GOOS +stdout '^{}$' +go env -changed -json GOARCH +stdout '^{}$' From 55860925c60f207cfbca9c6623346fe4e0143d07 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Mon, 12 Feb 2024 12:38:03 +0800 Subject: [PATCH 09/28] n Change-Id: Ib19f4c4e596a8342a28bc325a7bab101ecd28136 --- src/cmd/go/internal/cfg/cfg.go | 10 +++---- src/cmd/go/internal/envcmd/env.go | 35 +++++++++++++++++----- src/cmd/go/internal/work/exec.go | 14 ++++----- src/cmd/go/testdata/script/env_changed.txt | 6 ++++ 4 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index efee390a6c02d6..7610b4ae6a1605 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -277,9 +277,8 @@ var OrigEnv []string var CmdEnv []EnvVar var envCache struct { - once sync.Once - m map[string]string - goroot map[string]string + once sync.Once + m map[string]string } // EnvFile returns the name of the Go environment configuration file. @@ -301,7 +300,7 @@ func EnvFile() (string, bool, error) { } func initEnvCache() { - envCache.m, envCache.goroot = make(map[string]string), make(map[string]string) + envCache.m = make(map[string]string) if file, _, _ := EnvFile(); file != "" { readEnvFile(file, "user") } @@ -353,7 +352,6 @@ func readEnvFile(file string, source string) { if _, ok := envCache.m[string(key)]; ok { continue } - envCache.goroot[string(key)] = string(val) } envCache.m[string(key)] = string(val) } @@ -429,7 +427,7 @@ var ( func EnvOrAndChanged(name, def string) (string, bool) { val := Getenv(name) if val != "" { - return val, envCache.goroot[name] != val + return val, true } return def, false } diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index 107194964e9f49..3141451f2fd2f3 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -237,16 +237,37 @@ func ExtraEnvVarsCostly() []cfg.EnvVar { return q } - return []cfg.EnvVar{ + ret := []cfg.EnvVar{ // Note: Update the switch in runEnv below when adding to this list. - {Name: "CGO_CFLAGS", Value: join(cflags), Changed: cfg.Getenv("CGO_CFLAGS") != ""}, - {Name: "CGO_CPPFLAGS", Value: join(cppflags), Changed: cfg.Getenv("CGO_CPPFLAGS") != ""}, - {Name: "CGO_CXXFLAGS", Value: join(cxxflags), Changed: cfg.Getenv("CGO_CXXFLAGS") != ""}, - {Name: "CGO_FFLAGS", Value: join(fflags), Changed: cfg.Getenv("CGO_FFLAGS") != ""}, - {Name: "CGO_LDFLAGS", Value: join(ldflags), Changed: cfg.Getenv("CGO_LDFLAGS") != ""}, - {Name: "PKG_CONFIG", Value: b.PkgconfigCmd(), Changed: cfg.Getenv("PKG_CONFIG") != ""}, + {Name: "CGO_CFLAGS", Value: join(cflags)}, + {Name: "CGO_CPPFLAGS", Value: join(cppflags)}, + {Name: "CGO_CXXFLAGS", Value: join(cxxflags)}, + {Name: "CGO_FFLAGS", Value: join(fflags)}, + {Name: "CGO_LDFLAGS", Value: join(ldflags)}, + {Name: "PKG_CONFIG", Value: b.PkgconfigCmd()}, {Name: "GOGCCFLAGS", Value: join(cmd[3:])}, } + + for i := range ret { + switch ret[i].Name { + // GOGCCFLAGS cannot be modified + case "GOGCCFLAGS": + case "CGO_CPPFLAGS": + if ret[i].Value != "" { + ret[i].Changed = true + } + case "PKG_CONFIG": + if ret[i].Value != cfg.DefaultPkgConfig { + ret[i].Changed = true + } + default: + if ret[i].Value != work.DefaultCFlags { + ret[i].Changed = true + } + } + } + + return ret } // argKey returns the KEY part of the arg KEY=VAL, or else arg itself. diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go index 7edfac7ab4dbaa..ccf955b0a47371 100644 --- a/src/cmd/go/internal/work/exec.go +++ b/src/cmd/go/internal/work/exec.go @@ -46,7 +46,7 @@ import ( "cmd/internal/sys" ) -const defaultCFlags = "-O2 -g" +const DefaultCFlags = "-O2 -g" // actionList returns the list of actions in the dag rooted at root // as visited in a depth-first post-order traversal. @@ -2453,13 +2453,13 @@ func (b *Builder) gccSupportsFlag(compiler []string, flag string) bool { cmdArgs := str.StringList(compiler, flag) if strings.HasPrefix(flag, "-Wl,") /* linker flag */ { - ldflags, err := buildFlags("LDFLAGS", defaultCFlags, nil, checkLinkerFlags) + ldflags, err := buildFlags("LDFLAGS", DefaultCFlags, nil, checkLinkerFlags) if err != nil { return false } cmdArgs = append(cmdArgs, ldflags...) } else { /* compiler flag, add "-c" */ - cflags, err := buildFlags("CFLAGS", defaultCFlags, nil, checkCompilerFlags) + cflags, err := buildFlags("CFLAGS", DefaultCFlags, nil, checkCompilerFlags) if err != nil { return false } @@ -2700,16 +2700,16 @@ func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, l if cppflags, err = buildFlags("CPPFLAGS", "", p.CgoCPPFLAGS, checkCompilerFlags); err != nil { return } - if cflags, err = buildFlags("CFLAGS", defaultCFlags, p.CgoCFLAGS, checkCompilerFlags); err != nil { + if cflags, err = buildFlags("CFLAGS", DefaultCFlags, p.CgoCFLAGS, checkCompilerFlags); err != nil { return } - if cxxflags, err = buildFlags("CXXFLAGS", defaultCFlags, p.CgoCXXFLAGS, checkCompilerFlags); err != nil { + if cxxflags, err = buildFlags("CXXFLAGS", DefaultCFlags, p.CgoCXXFLAGS, checkCompilerFlags); err != nil { return } - if fflags, err = buildFlags("FFLAGS", defaultCFlags, p.CgoFFLAGS, checkCompilerFlags); err != nil { + if fflags, err = buildFlags("FFLAGS", DefaultCFlags, p.CgoFFLAGS, checkCompilerFlags); err != nil { return } - if ldflags, err = buildFlags("LDFLAGS", defaultCFlags, p.CgoLDFLAGS, checkLinkerFlags); err != nil { + if ldflags, err = buildFlags("LDFLAGS", DefaultCFlags, p.CgoLDFLAGS, checkLinkerFlags); err != nil { return } diff --git a/src/cmd/go/testdata/script/env_changed.txt b/src/cmd/go/testdata/script/env_changed.txt index 788560289b5249..bbcecd666de7b6 100644 --- a/src/cmd/go/testdata/script/env_changed.txt +++ b/src/cmd/go/testdata/script/env_changed.txt @@ -4,6 +4,8 @@ env GOTOOLCHAIN=local env GOSUMDB=nodefault env GOPROXY=nodefault env GO111MODULE=auto +env CGO_CFLAGS=nodefault +env CGO_CPPFLAGS=nodefault go env -changed # linux output like GOTOOLCHAIN='local' @@ -12,12 +14,16 @@ stdout 'GOTOOLCHAIN=local|GOTOOLCHAIN=''local''' stdout 'GOSUMDB=nodefault|GOSUMDB=''nodefault''' stdout 'GOPROXY=nodefault|GOPROXY=''nodefault''' stdout 'GO111MODULE=auto|GO111MODULE=''auto''' +stdout 'CGO_CFLAGS=nodefault|CGO_CFLAGS=''nodefault''' +stdout 'CGO_CPPFLAGS=nodefault|CGO_CPPFLAGS=''nodefault''' go env -changed -json stdout '"GOTOOLCHAIN": "local"' stdout '"GOSUMDB": "nodefault"' stdout '"GOPROXY": "nodefault"' stdout '"GO111MODULE": "auto"' +stdout '"CGO_CFLAGS": "nodefault"' +stdout '"CGO_CPPFLAGS": "nodefault"' [GOOS:windows] env GOOS=linux [!GOOS:windows] env GOOS=windows From 3c011a3dd0f5b32e58db8b94b64e35543222ac55 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Mon, 12 Feb 2024 14:06:06 +0800 Subject: [PATCH 10/28] n Change-Id: I711aaa2e560a615eca7d0dfba2140d2d186380d8 --- src/cmd/go/internal/cfg/cfg.go | 25 ++----------------------- src/go/build/build.go | 22 ++-------------------- src/go/build/deps_test.go | 6 ++++-- src/internal/cfg/cfg.go | 25 +++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 45 deletions(-) diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index 7610b4ae6a1605..1a493e19c77ee1 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -112,7 +112,7 @@ func defaultContext() build.Context { // Override defaults computed in go/build with defaults // from go environment configuration file, if known. - ctxt.GOPATH, GOPATHChanged = EnvOrAndChanged("GOPATH", gopath(ctxt)) + ctxt.GOPATH, GOPATHChanged = EnvOrAndChanged("GOPATH", cfg.DefaultGOPATH()) ctxt.GOOS = Goos ctxt.GOARCH = Goarch @@ -427,7 +427,7 @@ var ( func EnvOrAndChanged(name, def string) (string, bool) { val := Getenv(name) if val != "" { - return val, true + return val, val != def } return def, false } @@ -577,27 +577,6 @@ func gopathDir(rel string) string { return filepath.Join(list[0], rel) } -func gopath(ctxt build.Context) string { - if len(ctxt.GOPATH) > 0 { - return ctxt.GOPATH - } - env := "HOME" - if runtime.GOOS == "windows" { - env = "USERPROFILE" - } else if runtime.GOOS == "plan9" { - env = "home" - } - if home := os.Getenv(env); home != "" { - def := filepath.Join(home, "go") - if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) { - GoPathError = "cannot set GOROOT as GOPATH" - } - return "" - } - GoPathError = fmt.Sprintf("%s is not set", env) - return "" -} - // WithBuildXWriter returns a Context in which BuildX output is written // to given io.Writer. func WithBuildXWriter(ctx context.Context, xLog io.Writer) context.Context { diff --git a/src/go/build/build.go b/src/go/build/build.go index 9ce3700dc4bcbe..bfbbda782b06dd 100644 --- a/src/go/build/build.go +++ b/src/go/build/build.go @@ -13,6 +13,7 @@ import ( "go/doc" "go/token" "internal/buildcfg" + "internal/cfg" "internal/godebug" "internal/goroot" "internal/goversion" @@ -286,25 +287,6 @@ func (ctxt *Context) SrcDirs() []string { // if set, or else the compiled code's GOARCH, GOOS, and GOROOT. var Default Context = defaultContext() -func defaultGOPATH() string { - env := "HOME" - if runtime.GOOS == "windows" { - env = "USERPROFILE" - } else if runtime.GOOS == "plan9" { - env = "home" - } - if home := os.Getenv(env); home != "" { - def := filepath.Join(home, "go") - if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) { - // Don't set the default GOPATH to GOROOT, - // as that will trigger warnings from the go tool. - return "" - } - return def - } - return "" -} - var defaultToolTags, defaultReleaseTags []string func defaultContext() Context { @@ -315,7 +297,7 @@ func defaultContext() Context { if goroot := runtime.GOROOT(); goroot != "" { c.GOROOT = filepath.Clean(goroot) } - c.GOPATH = envOr("GOPATH", defaultGOPATH()) + c.GOPATH = envOr("GOPATH", cfg.DefaultGOPATH()) c.Compiler = runtime.Compiler c.ToolTags = append(c.ToolTags, buildcfg.ToolTags...) diff --git a/src/go/build/deps_test.go b/src/go/build/deps_test.go index 14880d9ef12021..1f184473dad95b 100644 --- a/src/go/build/deps_test.go +++ b/src/go/build/deps_test.go @@ -40,7 +40,7 @@ var depsRules = ` # No dependencies allowed for any of these packages. NONE < cmp, container/list, container/ring, - internal/cfg, internal/coverage, internal/coverage/rtcov, + internal/coverage, internal/coverage/rtcov, internal/coverage/uleb128, internal/coverage/calloc, internal/cpu, internal/goarch, internal/godebugs, internal/goexperiment, internal/goos, @@ -323,7 +323,9 @@ var depsRules = ` go/doc/comment, go/parser, internal/lazyregexp, text/template < go/doc; - go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion, internal/platform + os, path/filepath, runtime < internal/cfg; + + go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion, internal/platform, internal/cfg < go/build; # databases diff --git a/src/internal/cfg/cfg.go b/src/internal/cfg/cfg.go index 08d210b797385b..2ea4ac9c76449a 100644 --- a/src/internal/cfg/cfg.go +++ b/src/internal/cfg/cfg.go @@ -7,6 +7,12 @@ // cmd/go/internal/cfg instead of this package. package cfg +import ( + "os" + "path/filepath" + "runtime" +) + // KnownEnv is a list of environment variables that affect the operation // of the Go command. const KnownEnv = ` @@ -70,3 +76,22 @@ const KnownEnv = ` GO_EXTLINK_ENABLED PKG_CONFIG ` + +func DefaultGOPATH() string { + env := "HOME" + if runtime.GOOS == "windows" { + env = "USERPROFILE" + } else if runtime.GOOS == "plan9" { + env = "home" + } + if home := os.Getenv(env); home != "" { + def := filepath.Join(home, "go") + if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) { + // Don't set the default GOPATH to GOROOT, + // as that will trigger warnings from the go tool. + return "" + } + return def + } + return "" +} From 1be48aba20db374b69be09a93979d54abb0cc723 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Sat, 2 Mar 2024 14:24:26 +0800 Subject: [PATCH 11/28] n Change-Id: I266e8d98e9db85ff44ef566e239498e385502065 --- src/cmd/go/internal/envcmd/env.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index 3141451f2fd2f3..552b716a268643 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -54,7 +54,8 @@ form NAME=VALUE and changes the default settings of the named environment variables to the given values. The -changed flag output rusult of -query for non-defaults in the env +query for non-defaults in the env, +do not print GOOS and GOARCH. For more about environment variables, see 'go help environment'. `, @@ -108,7 +109,7 @@ func MkEnv() []cfg.EnvVar { {Name: "GOTOOLDIR", Value: build.ToolDir}, {Name: "GOVCS", Value: cfg.GOVCS}, {Name: "GOVERSION", Value: runtime.Version()}, - {Name: "GODEBUG", Value: os.Getenv("GODEBUG")}, + {Name: "GODEBUG"}, } // See go.dev/issue/34208 @@ -129,6 +130,8 @@ func MkEnv() []cfg.EnvVar { if env[i].Value != "auto" { env[i].Changed = true } + case "GODEBUG": + env[i].Value, env[i].Changed = cfg.EnvOrAndChanged("GODEBUG", "") } } From fef020e314177f3ea0e5cc0bab83920e8bce3cd5 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Sat, 2 Mar 2024 14:48:10 +0800 Subject: [PATCH 12/28] n Change-Id: I7499ea25314fa0b0f981682bfbbfbe69b8defeb6 --- src/cmd/go/alldocs.go | 3 ++- src/cmd/go/internal/cfg/cfg.go | 2 -- src/internal/cfg/cfg.go | 1 + 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index 4897d4c66117f4..4aca3d1566d1d6 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -477,7 +477,8 @@ // of the named environment variables to the given values. // // The -changed flag output rusult of -// query for non-defaults in the env +// query for non-defaults in the env, +// do not print GOOS and GOARCH. // // For more about environment variables, see 'go help environment'. // diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index 1a493e19c77ee1..d1fe69cbaca471 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -399,8 +399,6 @@ var ( GOROOTpkg string GOROOTsrc string - GOROOT_FINAL string - GOBIN = Getenv("GOBIN") GOMODCACHE, GOMODCACHEChanged = EnvOrAndChanged("GOMODCACHE", gopathDir("pkg/mod")) diff --git a/src/internal/cfg/cfg.go b/src/internal/cfg/cfg.go index 2ea4ac9c76449a..c3264b66681df1 100644 --- a/src/internal/cfg/cfg.go +++ b/src/internal/cfg/cfg.go @@ -75,6 +75,7 @@ const KnownEnv = ` GOWORK GO_EXTLINK_ENABLED PKG_CONFIG + GODEBUG ` func DefaultGOPATH() string { From c127e3f1adcbb7105532d8dbb3253cdbd1fdf9ed Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Wed, 17 Apr 2024 21:02:45 +0800 Subject: [PATCH 13/28] n Change-Id: I6d0a4c4bd5fcd0809f7306af929764ecc4cccf70 --- doc/next/3-tools.md | 3 +++ src/cmd/go/internal/cache/default.go | 1 + src/cmd/go/internal/cfg/cfg.go | 3 +++ src/cmd/go/internal/envcmd/env.go | 3 +-- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/doc/next/3-tools.md b/doc/next/3-tools.md index c052f3b084009a..2f5f9ea1c118ce 100644 --- a/doc/next/3-tools.md +++ b/doc/next/3-tools.md @@ -8,6 +8,9 @@ Distributions that install the `go` command to a location other than `$GOROOT/bin/go` should install a symlink instead of relocating or copying the `go` binary. +New Flag -changed only prints all non-default settings in go env, +do not print GOOS and GOARCH. + ### Vet {#vet} The `go vet` subcommand now includes the diff --git a/src/cmd/go/internal/cache/default.go b/src/cmd/go/internal/cache/default.go index cd8c9cf3a57593..ee30e2cda95f06 100644 --- a/src/cmd/go/internal/cache/default.go +++ b/src/cmd/go/internal/cache/default.go @@ -75,6 +75,7 @@ var ( // DefaultDir returns the effective GOCACHE setting. // It returns "off" if the cache is disabled. +// Bool result report dir whether it is a modified non-default value. func DefaultDir() (string, bool) { // Save the result of the first call to DefaultDir for later use in // initDefaultCache. cmd/go/main.go explicitly sets GOCACHE so that diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index d1fe69cbaca471..855539323f7ecc 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -282,6 +282,7 @@ var envCache struct { } // EnvFile returns the name of the Go environment configuration file. +// Bool result report env file whether it is a modified non-default value. func EnvFile() (string, bool, error) { if file := os.Getenv("GOENV"); file != "" { if file == "off" { @@ -422,6 +423,8 @@ var ( GOVCS = Getenv("GOVCS") ) +// EnvOrAndChanged get the environment variable value +// and report if it is different from the default value func EnvOrAndChanged(name, def string) (string, bool) { val := Getenv(name) if val != "" { diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index 552b716a268643..7194d701590aa1 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -53,8 +53,7 @@ The -w flag requires one or more arguments of the form NAME=VALUE and changes the default settings of the named environment variables to the given values. -The -changed flag output rusult of -query for non-defaults in the env, +The -changed flag only prints all non-default settings, do not print GOOS and GOARCH. For more about environment variables, see 'go help environment'. From 5f87f1834bdbcae91d5b353b7d99251516ec9b84 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Wed, 17 Apr 2024 21:13:13 +0800 Subject: [PATCH 14/28] n Change-Id: I0ea0f35c23f3efea097193ea310c23df0e07150a --- src/cmd/go/internal/cfg/cfg.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index 855539323f7ecc..60a2c0a9ef4f38 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -424,7 +424,7 @@ var ( ) // EnvOrAndChanged get the environment variable value -// and report if it is different from the default value +// and report if it is different from the default value. func EnvOrAndChanged(name, def string) (string, bool) { val := Getenv(name) if val != "" { From cd5c6183b5913a71f5e4b65d38eabaae16fff9aa Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Wed, 17 Apr 2024 21:25:51 +0800 Subject: [PATCH 15/28] n Change-Id: I063b1f608bd90c8171143b7313e71a8214948eda --- src/cmd/go/alldocs.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index 4aca3d1566d1d6..328126a855a5bb 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -476,8 +476,7 @@ // form NAME=VALUE and changes the default settings // of the named environment variables to the given values. // -// The -changed flag output rusult of -// query for non-defaults in the env, +// The -changed flag only prints all non-default settings, // do not print GOOS and GOARCH. // // For more about environment variables, see 'go help environment'. From 6a641a9531eb32d47d433c58401146a213cffc11 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Thu, 18 Apr 2024 18:11:20 +0800 Subject: [PATCH 16/28] n Change-Id: I40ef2eb9df844005b0b8dacf1d0a6242c43221ec --- doc/next/3-tools.md | 5 +++-- src/cmd/go/internal/cache/default.go | 16 ++++++++-------- src/cmd/go/internal/cfg/cfg.go | 12 ++++++------ src/cmd/go/internal/envcmd/env.go | 26 ++++++++++---------------- 4 files changed, 27 insertions(+), 32 deletions(-) diff --git a/doc/next/3-tools.md b/doc/next/3-tools.md index 2f5f9ea1c118ce..6154df6d57786f 100644 --- a/doc/next/3-tools.md +++ b/doc/next/3-tools.md @@ -8,8 +8,9 @@ Distributions that install the `go` command to a location other than `$GOROOT/bin/go` should install a symlink instead of relocating or copying the `go` binary. -New Flag -changed only prints all non-default settings in go env, -do not print GOOS and GOARCH. +The new go env -changed flag causes the command to print only +those settings whose effective value differs from the default value +that would be obtained in an empty environment with no prior uses of the -w flag. ### Vet {#vet} diff --git a/src/cmd/go/internal/cache/default.go b/src/cmd/go/internal/cache/default.go index ee30e2cda95f06..d8439a7489d5a3 100644 --- a/src/cmd/go/internal/cache/default.go +++ b/src/cmd/go/internal/cache/default.go @@ -69,13 +69,13 @@ func initDefaultCache() { var ( defaultDirOnce sync.Once defaultDir string - defaultDirChanged bool + defaultDirChanged bool // effective value differs from $GOCACHE defaultDirErr error ) // DefaultDir returns the effective GOCACHE setting. -// It returns "off" if the cache is disabled. -// Bool result report dir whether it is a modified non-default value. +// It returns "off" if the cache is disabled, +// and reports whether the effective value differs from GOCACHE. func DefaultDir() (string, bool) { // Save the result of the first call to DefaultDir for later use in // initDefaultCache. cmd/go/main.go explicitly sets GOCACHE so that @@ -83,12 +83,11 @@ func DefaultDir() (string, bool) { // otherwise distinguish between an explicit "off" and a UserCacheDir error. defaultDirOnce.Do(func() { - defaultDir = cfg.Getenv("GOCACHE") - if filepath.IsAbs(defaultDir) || defaultDir == "off" { - defaultDirChanged = true - return - } if defaultDir != "" { + if filepath.IsAbs(defaultDir) || defaultDir == "off" { + defaultDirChanged = true + return + } defaultDir = "off" defaultDirChanged = true defaultDirErr = fmt.Errorf("GOCACHE is not an absolute path") @@ -99,6 +98,7 @@ func DefaultDir() (string, bool) { dir, err := os.UserCacheDir() if err != nil { defaultDir = "off" + defaultDirChanged = true defaultDirErr = fmt.Errorf("GOCACHE is not defined and %v", err) return } diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index 60a2c0a9ef4f38..4e733162933741 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -265,7 +265,7 @@ func init() { type EnvVar struct { Name string Value string - Changed bool + Changed bool // effective Value differs from default } // OrigEnv is the original environment of the program at startup. @@ -281,12 +281,12 @@ var envCache struct { m map[string]string } -// EnvFile returns the name of the Go environment configuration file. -// Bool result report env file whether it is a modified non-default value. +// EnvFile returns the name of the Go environment configuration file, +// and reports whether the effective value differs from the default. func EnvFile() (string, bool, error) { if file := os.Getenv("GOENV"); file != "" { if file == "off" { - return "", true, fmt.Errorf("GOENV=off") + return "", false, fmt.Errorf("GOENV=off") } return file, true, nil } @@ -423,8 +423,8 @@ var ( GOVCS = Getenv("GOVCS") ) -// EnvOrAndChanged get the environment variable value -// and report if it is different from the default value. +// EnvOrAndChanged returns the environment variable value +// and reports whether it differs from the default value. func EnvOrAndChanged(name, def string) (string, bool) { val := Getenv(name) if val != "" { diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index 7194d701590aa1..a5e373083ececd 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -140,9 +140,9 @@ func MkEnv() []cfg.EnvVar { env = append(env, cfg.EnvVar{Name: "GCCGO", Value: work.GccgoName}) } - key, val, changde := cfg.GetArchEnv() - if key != "" { - env = append(env, cfg.EnvVar{Name: key, Value: val, Changed: changde}) + goarch, val, changed := cfg.GetArchEnv() + if goarch != "" { + env = append(env, cfg.EnvVar{Name: goarch, Value: val, Changed: changed}) } cc := cfg.Getenv("CC") @@ -251,21 +251,15 @@ func ExtraEnvVarsCostly() []cfg.EnvVar { } for i := range ret { - switch ret[i].Name { - // GOGCCFLAGS cannot be modified - case "GOGCCFLAGS": + ev := &ret[i] + switch ev.Name { + case "GOGCCFLAGS": // GOGCCFLAGS cannot be modified case "CGO_CPPFLAGS": - if ret[i].Value != "" { - ret[i].Changed = true - } + ev.Changed = ev.Value != "" case "PKG_CONFIG": - if ret[i].Value != cfg.DefaultPkgConfig { - ret[i].Changed = true - } - default: - if ret[i].Value != work.DefaultCFlags { - ret[i].Changed = true - } + ev.Changed = ev.Value != cfg.DefaultPkgConfig + case "CXXFLAGS", "CFLAGS", "FFLAGS", "LDFLAGS": + ev.Changed = ev.Value != work.DefaultCFlags } } From 7663878e0fb239734b36cd8979e412c9f0572f10 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Thu, 18 Apr 2024 18:29:39 +0800 Subject: [PATCH 17/28] n Change-Id: I8dd2a229a4778602a1f741f181173720e6765dee --- src/cmd/go/internal/cache/default.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cmd/go/internal/cache/default.go b/src/cmd/go/internal/cache/default.go index d8439a7489d5a3..5ec942a598dd48 100644 --- a/src/cmd/go/internal/cache/default.go +++ b/src/cmd/go/internal/cache/default.go @@ -83,6 +83,7 @@ func DefaultDir() (string, bool) { // otherwise distinguish between an explicit "off" and a UserCacheDir error. defaultDirOnce.Do(func() { + defaultDir = cfg.Getenv("GOCACHE") if defaultDir != "" { if filepath.IsAbs(defaultDir) || defaultDir == "off" { defaultDirChanged = true From 63b2294863c71f24e47839f12b4177fa9da6d28b Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Thu, 18 Apr 2024 18:34:13 +0800 Subject: [PATCH 18/28] n Change-Id: Id2b07c35998bd8940a5431df5ad649c94a3ed0ac --- src/cmd/go/internal/envcmd/env.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index a5e373083ececd..6341f8c25db8b8 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -258,7 +258,7 @@ func ExtraEnvVarsCostly() []cfg.EnvVar { ev.Changed = ev.Value != "" case "PKG_CONFIG": ev.Changed = ev.Value != cfg.DefaultPkgConfig - case "CXXFLAGS", "CFLAGS", "FFLAGS", "LDFLAGS": + case "CGO_CXXFLAGS", "CGO_CFLAGS", "CGO_FFLAGS", "GGO_LDFLAGS": ev.Changed = ev.Value != work.DefaultCFlags } } From 6fab88d98388e0da714fc9e1eb0413ea1cb88695 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Fri, 19 Apr 2024 21:11:47 +0800 Subject: [PATCH 19/28] n Change-Id: I1139d3f728b9c69c67e6020590afc2effafef9e9 --- src/cmd/go/internal/envcmd/env.go | 9 ++++----- src/cmd/go/testdata/script/env_changed.txt | 12 ++++++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index 6341f8c25db8b8..5eea478d40f642 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -54,7 +54,8 @@ form NAME=VALUE and changes the default settings of the named environment variables to the given values. The -changed flag only prints all non-default settings, -do not print GOOS and GOARCH. +when command line arguments exist, +print only if the specified environment variable is not the default value. For more about environment variables, see 'go help environment'. `, @@ -77,7 +78,7 @@ func MkEnv() []cfg.EnvVar { envFile, envFileChanged, _ := cfg.EnvFile() env := []cfg.EnvVar{ {Name: "GO111MODULE", Value: cfg.Getenv("GO111MODULE")}, - {Name: "GOARCH", Value: cfg.Goarch}, + {Name: "GOARCH", Value: cfg.Goarch, Changed: cfg.Goarch != runtime.GOARCH}, {Name: "GOBIN", Value: cfg.GOBIN}, {Name: "GOCACHE"}, {Name: "GOENV", Value: envFile, Changed: envFileChanged}, @@ -97,7 +98,7 @@ func MkEnv() []cfg.EnvVar { {Name: "GOMODCACHE", Value: cfg.GOMODCACHE, Changed: cfg.GOMODCACHEChanged}, {Name: "GONOPROXY", Value: cfg.GONOPROXY, Changed: cfg.GONOPROXYChanged}, {Name: "GONOSUMDB", Value: cfg.GONOSUMDB, Changed: cfg.GONOSUMDBChanged}, - {Name: "GOOS", Value: cfg.Goos}, + {Name: "GOOS", Value: cfg.Goos, Changed: cfg.Goos != runtime.GOOS}, {Name: "GOPATH", Value: cfg.BuildContext.GOPATH, Changed: cfg.GOPATHChanged}, {Name: "GOPRIVATE", Value: cfg.GOPRIVATE}, {Name: "GOPROXY", Value: cfg.GOPROXY, Changed: cfg.GOPROXYChanged}, @@ -111,8 +112,6 @@ func MkEnv() []cfg.EnvVar { {Name: "GODEBUG"}, } - // See go.dev/issue/34208 - // GOOS and GOARCH are deliberately not set Changed for i := range env { switch env[i].Name { case "GO111MODULE": diff --git a/src/cmd/go/testdata/script/env_changed.txt b/src/cmd/go/testdata/script/env_changed.txt index bbcecd666de7b6..cade1b19d87393 100644 --- a/src/cmd/go/testdata/script/env_changed.txt +++ b/src/cmd/go/testdata/script/env_changed.txt @@ -31,11 +31,15 @@ stdout '"CGO_CPPFLAGS": "nodefault"' [!GOARCH:amd64] env GOARCH=amd64 go env -changed GOOS -! stdout 'GOOS' +[GOOS:windows] stdout 'set GOOS=linux' +[!GOOS:windows] stdout 'GOOS=''windows''' go env -changed GOARCH -! stdout 'GOARCH' +[GOARCH:amd64] stdout 'set GOARCH=arm64' +[!GOARCH:amd64] stdout 'GOARCH=''amd64''' go env -changed -json GOOS -stdout '^{}$' +[GOOS:windows] stdout '"GOOS": "linux"' +[!GOOS:windows] stdout '"GOOS": "windows"' go env -changed -json GOARCH -stdout '^{}$' +[GOARCH:amd64] stdout '"GOARCH": "arm64"' +[!GOARCH:amd64] stdout '"GOARCH": "amd64"' From 0a01dd50cf067d971b314c032928ab4689e255cc Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Fri, 19 Apr 2024 21:15:31 +0800 Subject: [PATCH 20/28] n Change-Id: Idcbc320798752dfa5721b0903a3f9facf516d61f --- src/cmd/go/alldocs.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index 328126a855a5bb..f3d6a4e4ebe9f0 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -477,7 +477,8 @@ // of the named environment variables to the given values. // // The -changed flag only prints all non-default settings, -// do not print GOOS and GOARCH. +// when command line arguments exist, +// print only if the specified environment variable is not the default value. // // For more about environment variables, see 'go help environment'. // From f2f1095aec2c43434dcf57c70c917773fa203e4e Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Fri, 19 Apr 2024 21:57:46 +0800 Subject: [PATCH 21/28] n Change-Id: I65a527b06855b431eb8bfd24b522523eb135626e --- src/cmd/go/testdata/script/env_changed.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cmd/go/testdata/script/env_changed.txt b/src/cmd/go/testdata/script/env_changed.txt index cade1b19d87393..f1818103bfc280 100644 --- a/src/cmd/go/testdata/script/env_changed.txt +++ b/src/cmd/go/testdata/script/env_changed.txt @@ -34,8 +34,8 @@ go env -changed GOOS [GOOS:windows] stdout 'set GOOS=linux' [!GOOS:windows] stdout 'GOOS=''windows''' go env -changed GOARCH -[GOARCH:amd64] stdout 'set GOARCH=arm64' -[!GOARCH:amd64] stdout 'GOARCH=''amd64''' +[GOARCH:amd64] stdout 'set GOARCH=arm64|GOARCH=''arm64''' +[!GOARCH:amd64] stdout 'set GOARCH=amd64|GOARCH=''amd64''' go env -changed -json GOOS [GOOS:windows] stdout '"GOOS": "linux"' From 9e71a5410e8011aea58948564c4207651f338ff3 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Sat, 20 Apr 2024 09:52:59 +0800 Subject: [PATCH 22/28] n Change-Id: I2d9f70319b1cc94cc84c25270a0416104061729e --- doc/next/3-tools.md | 4 ++-- src/cmd/go/alldocs.go | 6 +++--- src/cmd/go/internal/cache/default.go | 3 +-- src/cmd/go/internal/cfg/cfg.go | 24 ++++++++++++++++++++- src/cmd/go/internal/envcmd/env.go | 32 ++++++++++------------------ src/go/build/build.go | 23 ++++++++++++++++++-- src/internal/cfg/cfg.go | 26 ---------------------- 7 files changed, 61 insertions(+), 57 deletions(-) diff --git a/doc/next/3-tools.md b/doc/next/3-tools.md index 6154df6d57786f..4112fb61ac8316 100644 --- a/doc/next/3-tools.md +++ b/doc/next/3-tools.md @@ -8,9 +8,9 @@ Distributions that install the `go` command to a location other than `$GOROOT/bin/go` should install a symlink instead of relocating or copying the `go` binary. -The new go env -changed flag causes the command to print only +The new go env `-changed` flag causes the command to print only those settings whose effective value differs from the default value -that would be obtained in an empty environment with no prior uses of the -w flag. +that would be obtained in an empty environment with no prior uses of the `-w` flag. ### Vet {#vet} diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index f3d6a4e4ebe9f0..7c1fcc5066f961 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -476,9 +476,9 @@ // form NAME=VALUE and changes the default settings // of the named environment variables to the given values. // -// The -changed flag only prints all non-default settings, -// when command line arguments exist, -// print only if the specified environment variable is not the default value. +// The -changed flag prints only those settings whose effective +// value differs from the default value that would be obtained in +// an empty environment with no prior uses of the -w flag. // // For more about environment variables, see 'go help environment'. // diff --git a/src/cmd/go/internal/cache/default.go b/src/cmd/go/internal/cache/default.go index 5ec942a598dd48..5430d9651ec1d7 100644 --- a/src/cmd/go/internal/cache/default.go +++ b/src/cmd/go/internal/cache/default.go @@ -85,12 +85,11 @@ func DefaultDir() (string, bool) { defaultDirOnce.Do(func() { defaultDir = cfg.Getenv("GOCACHE") if defaultDir != "" { + defaultDirChanged = true if filepath.IsAbs(defaultDir) || defaultDir == "off" { - defaultDirChanged = true return } defaultDir = "off" - defaultDirChanged = true defaultDirErr = fmt.Errorf("GOCACHE is not an absolute path") return } diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index 4e733162933741..1a5ef4d31a919e 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -112,7 +112,7 @@ func defaultContext() build.Context { // Override defaults computed in go/build with defaults // from go environment configuration file, if known. - ctxt.GOPATH, GOPATHChanged = EnvOrAndChanged("GOPATH", cfg.DefaultGOPATH()) + ctxt.GOPATH, GOPATHChanged = EnvOrAndChanged("GOPATH", gopath(ctxt)) ctxt.GOOS = Goos ctxt.GOARCH = Goarch @@ -578,6 +578,28 @@ func gopathDir(rel string) string { return filepath.Join(list[0], rel) } +// gopath now semantics are actually the same in go/build.defaultGOPATH. +func gopath(ctxt build.Context) string { + if len(ctxt.GOPATH) > 0 { + return ctxt.GOPATH + } + env := "HOME" + if runtime.GOOS == "windows" { + env = "USERPROFILE" + } else if runtime.GOOS == "plan9" { + env = "home" + } + if home := os.Getenv(env); home != "" { + def := filepath.Join(home, "go") + if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) { + GoPathError = "cannot set GOROOT as GOPATH" + } + return "" + } + GoPathError = fmt.Sprintf("%s is not set", env) + return "" +} + // WithBuildXWriter returns a Context in which BuildX output is written // to given io.Writer. func WithBuildXWriter(ctx context.Context, xLog io.Writer) context.Context { diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index 5eea478d40f642..cb64675ab0d8dc 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -53,9 +53,9 @@ The -w flag requires one or more arguments of the form NAME=VALUE and changes the default settings of the named environment variables to the given values. -The -changed flag only prints all non-default settings, -when command line arguments exist, -print only if the specified environment variable is not the default value. +The -changed flag prints only those settings whose effective +value differs from the default value that would be obtained in +an empty environment with no prior uses of the -w flag. For more about environment variables, see 'go help environment'. `, @@ -343,25 +343,15 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) { env = append(env, ExtraEnvVarsCostly()...) } - if *envChanged { - if len(args) > 0 { - var es []cfg.EnvVar - for _, name := range args { - es = append(es, findCfgEnv(env, name)) - } - env = es - } - - if *envJson { - printEnvAsJSON(env, true) - return + if len(args) > 0 && *envChanged { + var es []cfg.EnvVar + for _, name := range args { + es = append(es, findCfgEnv(env, name)) } - - PrintEnv(os.Stdout, env, true) - return + env = es } - if len(args) > 0 { + if len(args) > 0 && !*envChanged { if *envJson { var es []cfg.EnvVar for _, name := range args { @@ -378,11 +368,11 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) { } if *envJson { - printEnvAsJSON(env, false) + printEnvAsJSON(env, *envChanged) return } - PrintEnv(os.Stdout, env, false) + PrintEnv(os.Stdout, env, *envChanged) } func runEnvW(args []string) { diff --git a/src/go/build/build.go b/src/go/build/build.go index bfbbda782b06dd..1f003ae1fd2609 100644 --- a/src/go/build/build.go +++ b/src/go/build/build.go @@ -13,7 +13,6 @@ import ( "go/doc" "go/token" "internal/buildcfg" - "internal/cfg" "internal/godebug" "internal/goroot" "internal/goversion" @@ -287,6 +286,26 @@ func (ctxt *Context) SrcDirs() []string { // if set, or else the compiled code's GOARCH, GOOS, and GOROOT. var Default Context = defaultContext() +// defaultGOPATH now semantics are actually the same in cmd/go/internal/cfg.gopath. +func defaultGOPATH() string { + env := "HOME" + if runtime.GOOS == "windows" { + env = "USERPROFILE" + } else if runtime.GOOS == "plan9" { + env = "home" + } + if home := os.Getenv(env); home != "" { + def := filepath.Join(home, "go") + if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) { + // Don't set the default GOPATH to GOROOT, + // as that will trigger warnings from the go tool. + return "" + } + return def + } + return "" +} + var defaultToolTags, defaultReleaseTags []string func defaultContext() Context { @@ -297,7 +316,7 @@ func defaultContext() Context { if goroot := runtime.GOROOT(); goroot != "" { c.GOROOT = filepath.Clean(goroot) } - c.GOPATH = envOr("GOPATH", cfg.DefaultGOPATH()) + c.GOPATH = envOr("GOPATH", defaultGOPATH()) c.Compiler = runtime.Compiler c.ToolTags = append(c.ToolTags, buildcfg.ToolTags...) diff --git a/src/internal/cfg/cfg.go b/src/internal/cfg/cfg.go index c3264b66681df1..08d210b797385b 100644 --- a/src/internal/cfg/cfg.go +++ b/src/internal/cfg/cfg.go @@ -7,12 +7,6 @@ // cmd/go/internal/cfg instead of this package. package cfg -import ( - "os" - "path/filepath" - "runtime" -) - // KnownEnv is a list of environment variables that affect the operation // of the Go command. const KnownEnv = ` @@ -75,24 +69,4 @@ const KnownEnv = ` GOWORK GO_EXTLINK_ENABLED PKG_CONFIG - GODEBUG ` - -func DefaultGOPATH() string { - env := "HOME" - if runtime.GOOS == "windows" { - env = "USERPROFILE" - } else if runtime.GOOS == "plan9" { - env = "home" - } - if home := os.Getenv(env); home != "" { - def := filepath.Join(home, "go") - if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) { - // Don't set the default GOPATH to GOROOT, - // as that will trigger warnings from the go tool. - return "" - } - return def - } - return "" -} From f3c2fe90133bd0b693012e0e2d7f9b65a866e68f Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Sat, 20 Apr 2024 10:06:44 +0800 Subject: [PATCH 23/28] n Change-Id: Id6ded781cbfd3247cef7e600861d94758d347cc3 --- src/internal/cfg/cfg.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/internal/cfg/cfg.go b/src/internal/cfg/cfg.go index 08d210b797385b..bde29d0f7c29fc 100644 --- a/src/internal/cfg/cfg.go +++ b/src/internal/cfg/cfg.go @@ -69,4 +69,5 @@ const KnownEnv = ` GOWORK GO_EXTLINK_ENABLED PKG_CONFIG + GODEBUG ` From 887b753f9d0a04b83e9b0b894c940b26a5e41aa0 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Mon, 22 Apr 2024 22:09:12 +0800 Subject: [PATCH 24/28] n Change-Id: Ie3a324527dbee8c6cf67042d28edd81a47530918 --- src/cmd/go/internal/cfg/cfg.go | 2 +- src/cmd/go/internal/envcmd/env.go | 57 +++++++++++++++---------------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index 1a5ef4d31a919e..46e6d9fd978313 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -578,7 +578,7 @@ func gopathDir(rel string) string { return filepath.Join(list[0], rel) } -// gopath now semantics are actually the same in go/build.defaultGOPATH. +// Keep consistent with go/build.defaultGOPATH. func gopath(ctxt build.Context) string { if len(ctxt.GOPATH) > 0 { return ctxt.GOPATH diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index cb64675ab0d8dc..0fdacae557ef55 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -170,15 +170,6 @@ func MkEnv() []cfg.EnvVar { return env } -func findCfgEnv(env []cfg.EnvVar, name string) cfg.EnvVar { - for _, e := range env { - if e.Name == name { - return e - } - } - return cfg.EnvVar{} -} - func findEnv(env []cfg.EnvVar, name string) string { for _, e := range env { if e.Name == name { @@ -343,36 +334,44 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) { env = append(env, ExtraEnvVarsCostly()...) } - if len(args) > 0 && *envChanged { - var es []cfg.EnvVar - for _, name := range args { - es = append(es, findCfgEnv(env, name)) - } - env = es - } - - if len(args) > 0 && !*envChanged { - if *envJson { - var es []cfg.EnvVar - for _, name := range args { - e := cfg.EnvVar{Name: name, Value: findEnv(env, name)} - es = append(es, e) + if len(args) > 0 { + // Show only the named vars. + if !*envChanged { + if *envJson { + var es []cfg.EnvVar + for _, name := range args { + e := cfg.EnvVar{Name: name, Value: findEnv(env, name)} + es = append(es, e) + } + env = es + } else { + // Print just the values, without names. + for _, name := range args { + fmt.Printf("%s\n", findEnv(env, name)) + } + return } - printEnvAsJSON(es, false) } else { + // Show only the changed, named vars. + var es []cfg.EnvVar for _, name := range args { - fmt.Printf("%s\n", findEnv(env, name)) + for _, e := range env { + if e.Name == name { + es = append(es, e) + break + } + } } + env = es } - return } + // print if *envJson { printEnvAsJSON(env, *envChanged) - return + } else { + PrintEnv(os.Stdout, env, *envChanged) } - - PrintEnv(os.Stdout, env, *envChanged) } func runEnvW(args []string) { From a7a7804a42f749b6852884cc71ebd89e4b270dd5 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Sat, 4 May 2024 09:21:32 +0800 Subject: [PATCH 25/28] n Change-Id: Ib6c3a7d62b808de2d2e18f60059c346bfe2f80a5 --- src/cmd/go/testdata/counters.txt | 690 ------------------------------- 1 file changed, 690 deletions(-) delete mode 100644 src/cmd/go/testdata/counters.txt diff --git a/src/cmd/go/testdata/counters.txt b/src/cmd/go/testdata/counters.txt deleted file mode 100644 index 6ac1bccff979e1..00000000000000 --- a/src/cmd/go/testdata/counters.txt +++ /dev/null @@ -1,690 +0,0 @@ -go/flag:C -go/subcommand:unknown -go/flag:fixdocs -go/flag:fixreadme -go/flag:flaky -go/flag:proxy -go/flag:test.bench -go/flag:test.benchmem -go/flag:test.benchtime -go/flag:test.blockprofile -go/flag:test.blockprofilerate -go/flag:test.count -go/flag:test.coverprofile -go/flag:test.cpu -go/flag:test.cpuprofile -go/flag:test.failfast -go/flag:test.fullpath -go/flag:test.fuzz -go/flag:test.fuzzcachedir -go/flag:test.fuzzminimizetime -go/flag:test.fuzztime -go/flag:test.fuzzworker -go/flag:test.gocoverdir -go/flag:test.list -go/flag:test.memprofile -go/flag:test.memprofilerate -go/flag:test.mutexprofile -go/flag:test.mutexprofilefraction -go/flag:test.outputdir -go/flag:test.paniconexit0 -go/flag:test.parallel -go/flag:test.run -go/flag:test.short -go/flag:test.shuffle -go/flag:test.skip -go/flag:test.testlogfile -go/flag:test.timeout -go/flag:test.trace -go/flag:test.v -go/flag:testsum -go/flag:testwork -go/flag:update -go/subcommand:help -go/subcommand:bug -go/flag:bug-C -go/flag:bug-v -go/subcommand:help-bug -go/subcommand:build -go/flag:build-C -go/flag:build-a -go/flag:build-asan -go/flag:build-asmflags -go/flag:build-buildmode -go/flag:build-buildvcs -go/flag:build-compiler -go/flag:build-cover -go/flag:build-covermode -go/flag:build-coverpkg -go/flag:build-debug-actiongraph -go/flag:build-debug-runtime-trace -go/flag:build-debug-trace -go/flag:build-gccgoflags -go/flag:build-gcflags -go/flag:build-installsuffix -go/flag:build-ldflags -go/flag:build-linkshared -go/flag:build-mod -go/flag:build-modcacherw -go/flag:build-modfile -go/flag:build-msan -go/flag:build-n -go/flag:build-o -go/flag:build-overlay -go/flag:build-p -go/flag:build-pgo -go/flag:build-pkgdir -go/flag:build-race -go/flag:build-tags -go/flag:build-toolexec -go/flag:build-trimpath -go/flag:build-v -go/flag:build-work -go/flag:build-x -go/subcommand:help-build -go/subcommand:clean -go/flag:clean-C -go/flag:clean-a -go/flag:clean-asan -go/flag:clean-asmflags -go/flag:clean-buildmode -go/flag:clean-buildvcs -go/flag:clean-cache -go/flag:clean-compiler -go/flag:clean-debug-actiongraph -go/flag:clean-debug-runtime-trace -go/flag:clean-debug-trace -go/flag:clean-fuzzcache -go/flag:clean-gccgoflags -go/flag:clean-gcflags -go/flag:clean-i -go/flag:clean-installsuffix -go/flag:clean-ldflags -go/flag:clean-linkshared -go/flag:clean-mod -go/flag:clean-modcache -go/flag:clean-modcacherw -go/flag:clean-modfile -go/flag:clean-msan -go/flag:clean-n -go/flag:clean-overlay -go/flag:clean-p -go/flag:clean-pgo -go/flag:clean-pkgdir -go/flag:clean-r -go/flag:clean-race -go/flag:clean-tags -go/flag:clean-testcache -go/flag:clean-toolexec -go/flag:clean-trimpath -go/flag:clean-v -go/flag:clean-work -go/flag:clean-x -go/subcommand:help-clean -go/subcommand:doc -go/subcommand:help-doc -go/subcommand:env -go/flag:env-C -go/flag:env-changed -go/flag:env-json -go/flag:env-n -go/flag:env-u -go/flag:env-w -go/flag:env-x -go/subcommand:help-env -go/subcommand:fix -go/flag:fix-C -go/flag:fix-a -go/flag:fix-asan -go/flag:fix-asmflags -go/flag:fix-buildmode -go/flag:fix-buildvcs -go/flag:fix-compiler -go/flag:fix-debug-actiongraph -go/flag:fix-debug-runtime-trace -go/flag:fix-debug-trace -go/flag:fix-fix -go/flag:fix-gccgoflags -go/flag:fix-gcflags -go/flag:fix-installsuffix -go/flag:fix-ldflags -go/flag:fix-linkshared -go/flag:fix-mod -go/flag:fix-modcacherw -go/flag:fix-modfile -go/flag:fix-msan -go/flag:fix-n -go/flag:fix-overlay -go/flag:fix-p -go/flag:fix-pgo -go/flag:fix-pkgdir -go/flag:fix-race -go/flag:fix-tags -go/flag:fix-toolexec -go/flag:fix-trimpath -go/flag:fix-v -go/flag:fix-work -go/flag:fix-x -go/subcommand:help-fix -go/subcommand:fmt -go/flag:fmt-C -go/flag:fmt-mod -go/flag:fmt-modcacherw -go/flag:fmt-modfile -go/flag:fmt-n -go/flag:fmt-overlay -go/flag:fmt-x -go/subcommand:help-fmt -go/subcommand:generate -go/flag:generate-C -go/flag:generate-a -go/flag:generate-asan -go/flag:generate-asmflags -go/flag:generate-buildmode -go/flag:generate-buildvcs -go/flag:generate-compiler -go/flag:generate-debug-actiongraph -go/flag:generate-debug-runtime-trace -go/flag:generate-debug-trace -go/flag:generate-gccgoflags -go/flag:generate-gcflags -go/flag:generate-installsuffix -go/flag:generate-ldflags -go/flag:generate-linkshared -go/flag:generate-mod -go/flag:generate-modcacherw -go/flag:generate-modfile -go/flag:generate-msan -go/flag:generate-n -go/flag:generate-overlay -go/flag:generate-p -go/flag:generate-pgo -go/flag:generate-pkgdir -go/flag:generate-race -go/flag:generate-run -go/flag:generate-skip -go/flag:generate-tags -go/flag:generate-toolexec -go/flag:generate-trimpath -go/flag:generate-v -go/flag:generate-work -go/flag:generate-x -go/subcommand:help-generate -go/subcommand:get -go/flag:get-C -go/flag:get-a -go/flag:get-asan -go/flag:get-asmflags -go/flag:get-buildmode -go/flag:get-buildvcs -go/flag:get-compiler -go/flag:get-d -go/flag:get-debug-actiongraph -go/flag:get-debug-runtime-trace -go/flag:get-debug-trace -go/flag:get-f -go/flag:get-fix -go/flag:get-gccgoflags -go/flag:get-gcflags -go/flag:get-insecure -go/flag:get-installsuffix -go/flag:get-ldflags -go/flag:get-linkshared -go/flag:get-m -go/flag:get-modcacherw -go/flag:get-modfile -go/flag:get-msan -go/flag:get-n -go/flag:get-overlay -go/flag:get-p -go/flag:get-pgo -go/flag:get-pkgdir -go/flag:get-race -go/flag:get-t -go/flag:get-tags -go/flag:get-toolexec -go/flag:get-trimpath -go/flag:get-u -go/flag:get-v -go/flag:get-work -go/flag:get-x -go/subcommand:help-get -go/subcommand:install -go/flag:install-C -go/flag:install-a -go/flag:install-asan -go/flag:install-asmflags -go/flag:install-buildmode -go/flag:install-buildvcs -go/flag:install-compiler -go/flag:install-cover -go/flag:install-covermode -go/flag:install-coverpkg -go/flag:install-debug-actiongraph -go/flag:install-debug-runtime-trace -go/flag:install-debug-trace -go/flag:install-gccgoflags -go/flag:install-gcflags -go/flag:install-installsuffix -go/flag:install-ldflags -go/flag:install-linkshared -go/flag:install-mod -go/flag:install-modcacherw -go/flag:install-modfile -go/flag:install-msan -go/flag:install-n -go/flag:install-overlay -go/flag:install-p -go/flag:install-pgo -go/flag:install-pkgdir -go/flag:install-race -go/flag:install-tags -go/flag:install-toolexec -go/flag:install-trimpath -go/flag:install-v -go/flag:install-work -go/flag:install-x -go/subcommand:help-install -go/subcommand:list -go/flag:list-C -go/flag:list-a -go/flag:list-asan -go/flag:list-asmflags -go/flag:list-buildmode -go/flag:list-buildvcs -go/flag:list-compiled -go/flag:list-compiler -go/flag:list-cover -go/flag:list-covermode -go/flag:list-coverpkg -go/flag:list-debug-actiongraph -go/flag:list-debug-runtime-trace -go/flag:list-debug-trace -go/flag:list-deps -go/flag:list-e -go/flag:list-export -go/flag:list-f -go/flag:list-find -go/flag:list-gccgoflags -go/flag:list-gcflags -go/flag:list-installsuffix -go/flag:list-json -go/flag:list-ldflags -go/flag:list-linkshared -go/flag:list-m -go/flag:list-mod -go/flag:list-modcacherw -go/flag:list-modfile -go/flag:list-msan -go/flag:list-n -go/flag:list-overlay -go/flag:list-p -go/flag:list-pgo -go/flag:list-pkgdir -go/flag:list-race -go/flag:list-retracted -go/flag:list-reuse -go/flag:list-tags -go/flag:list-test -go/flag:list-toolexec -go/flag:list-trimpath -go/flag:list-u -go/flag:list-v -go/flag:list-versions -go/flag:list-work -go/flag:list-x -go/subcommand:help-list -go/subcommand:help-mod -go/subcommand:mod-download -go/flag:mod-download-C -go/flag:mod-download-json -go/flag:mod-download-modcacherw -go/flag:mod-download-modfile -go/flag:mod-download-overlay -go/flag:mod-download-reuse -go/flag:mod-download-x -go/subcommand:mod-help-download -go/subcommand:help-mod-download -go/subcommand:mod-edit -go/flag:mod-edit-C -go/flag:mod-edit-dropexclude -go/flag:mod-edit-dropreplace -go/flag:mod-edit-droprequire -go/flag:mod-edit-dropretract -go/flag:mod-edit-exclude -go/flag:mod-edit-fmt -go/flag:mod-edit-go -go/flag:mod-edit-json -go/flag:mod-edit-modcacherw -go/flag:mod-edit-modfile -go/flag:mod-edit-module -go/flag:mod-edit-n -go/flag:mod-edit-overlay -go/flag:mod-edit-print -go/flag:mod-edit-replace -go/flag:mod-edit-require -go/flag:mod-edit-retract -go/flag:mod-edit-toolchain -go/flag:mod-edit-x -go/subcommand:mod-help-edit -go/subcommand:help-mod-edit -go/subcommand:mod-graph -go/flag:mod-graph-C -go/flag:mod-graph-go -go/flag:mod-graph-modcacherw -go/flag:mod-graph-modfile -go/flag:mod-graph-overlay -go/flag:mod-graph-x -go/subcommand:mod-help-graph -go/subcommand:help-mod-graph -go/subcommand:mod-init -go/flag:mod-init-C -go/flag:mod-init-modcacherw -go/flag:mod-init-modfile -go/flag:mod-init-overlay -go/subcommand:mod-help-init -go/subcommand:help-mod-init -go/subcommand:mod-tidy -go/flag:mod-tidy-C -go/flag:mod-tidy-compat -go/flag:mod-tidy-e -go/flag:mod-tidy-go -go/flag:mod-tidy-modcacherw -go/flag:mod-tidy-modfile -go/flag:mod-tidy-overlay -go/flag:mod-tidy-v -go/flag:mod-tidy-x -go/subcommand:mod-help-tidy -go/subcommand:help-mod-tidy -go/subcommand:mod-vendor -go/flag:mod-vendor-C -go/flag:mod-vendor-e -go/flag:mod-vendor-modcacherw -go/flag:mod-vendor-modfile -go/flag:mod-vendor-o -go/flag:mod-vendor-overlay -go/flag:mod-vendor-v -go/subcommand:mod-help-vendor -go/subcommand:help-mod-vendor -go/subcommand:mod-verify -go/flag:mod-verify-C -go/flag:mod-verify-modcacherw -go/flag:mod-verify-modfile -go/flag:mod-verify-overlay -go/subcommand:mod-help-verify -go/subcommand:help-mod-verify -go/subcommand:mod-why -go/flag:mod-why-C -go/flag:mod-why-m -go/flag:mod-why-modcacherw -go/flag:mod-why-modfile -go/flag:mod-why-overlay -go/flag:mod-why-vendor -go/subcommand:mod-help-why -go/subcommand:help-mod-why -go/subcommand:help-work -go/subcommand:work-edit -go/flag:work-edit-C -go/flag:work-edit-dropreplace -go/flag:work-edit-dropuse -go/flag:work-edit-fmt -go/flag:work-edit-go -go/flag:work-edit-json -go/flag:work-edit-print -go/flag:work-edit-replace -go/flag:work-edit-toolchain -go/flag:work-edit-use -go/subcommand:work-help-edit -go/subcommand:help-work-edit -go/subcommand:work-init -go/flag:work-init-C -go/flag:work-init-modcacherw -go/flag:work-init-modfile -go/flag:work-init-overlay -go/subcommand:work-help-init -go/subcommand:help-work-init -go/subcommand:work-sync -go/flag:work-sync-C -go/flag:work-sync-modcacherw -go/flag:work-sync-modfile -go/flag:work-sync-overlay -go/subcommand:work-help-sync -go/subcommand:help-work-sync -go/subcommand:work-use -go/flag:work-use-C -go/flag:work-use-modcacherw -go/flag:work-use-modfile -go/flag:work-use-overlay -go/flag:work-use-r -go/subcommand:work-help-use -go/subcommand:help-work-use -go/subcommand:work-vendor -go/flag:work-vendor-C -go/flag:work-vendor-e -go/flag:work-vendor-modcacherw -go/flag:work-vendor-modfile -go/flag:work-vendor-o -go/flag:work-vendor-overlay -go/flag:work-vendor-v -go/subcommand:work-help-vendor -go/subcommand:help-work-vendor -go/subcommand:run -go/flag:run-C -go/flag:run-a -go/flag:run-asan -go/flag:run-asmflags -go/flag:run-buildmode -go/flag:run-buildvcs -go/flag:run-compiler -go/flag:run-cover -go/flag:run-covermode -go/flag:run-coverpkg -go/flag:run-debug-actiongraph -go/flag:run-debug-runtime-trace -go/flag:run-debug-trace -go/flag:run-exec -go/flag:run-gccgoflags -go/flag:run-gcflags -go/flag:run-installsuffix -go/flag:run-ldflags -go/flag:run-linkshared -go/flag:run-mod -go/flag:run-modcacherw -go/flag:run-modfile -go/flag:run-msan -go/flag:run-n -go/flag:run-overlay -go/flag:run-p -go/flag:run-pgo -go/flag:run-pkgdir -go/flag:run-race -go/flag:run-tags -go/flag:run-toolexec -go/flag:run-trimpath -go/flag:run-v -go/flag:run-work -go/flag:run-x -go/subcommand:help-run -go/subcommand:test -go/flag:test-C -go/flag:test-a -go/flag:test-asan -go/flag:test-asmflags -go/flag:test-bench -go/flag:test-benchmem -go/flag:test-benchtime -go/flag:test-blockprofile -go/flag:test-blockprofilerate -go/flag:test-buildmode -go/flag:test-buildvcs -go/flag:test-c -go/flag:test-compiler -go/flag:test-count -go/flag:test-cover -go/flag:test-covermode -go/flag:test-coverpkg -go/flag:test-coverprofile -go/flag:test-cpu -go/flag:test-cpuprofile -go/flag:test-debug-actiongraph -go/flag:test-debug-runtime-trace -go/flag:test-debug-trace -go/flag:test-exec -go/flag:test-failfast -go/flag:test-fullpath -go/flag:test-fuzz -go/flag:test-fuzzminimizetime -go/flag:test-fuzztime -go/flag:test-gccgoflags -go/flag:test-gcflags -go/flag:test-installsuffix -go/flag:test-json -go/flag:test-ldflags -go/flag:test-linkshared -go/flag:test-list -go/flag:test-memprofile -go/flag:test-memprofilerate -go/flag:test-mod -go/flag:test-modcacherw -go/flag:test-modfile -go/flag:test-msan -go/flag:test-mutexprofile -go/flag:test-mutexprofilefraction -go/flag:test-n -go/flag:test-o -go/flag:test-outputdir -go/flag:test-overlay -go/flag:test-p -go/flag:test-parallel -go/flag:test-pgo -go/flag:test-pkgdir -go/flag:test-race -go/flag:test-run -go/flag:test-short -go/flag:test-shuffle -go/flag:test-skip -go/flag:test-tags -go/flag:test-test.bench -go/flag:test-test.benchmem -go/flag:test-test.benchtime -go/flag:test-test.blockprofile -go/flag:test-test.blockprofilerate -go/flag:test-test.count -go/flag:test-test.coverprofile -go/flag:test-test.cpu -go/flag:test-test.cpuprofile -go/flag:test-test.failfast -go/flag:test-test.fullpath -go/flag:test-test.fuzz -go/flag:test-test.fuzzminimizetime -go/flag:test-test.fuzztime -go/flag:test-test.list -go/flag:test-test.memprofile -go/flag:test-test.memprofilerate -go/flag:test-test.mutexprofile -go/flag:test-test.mutexprofilefraction -go/flag:test-test.outputdir -go/flag:test-test.parallel -go/flag:test-test.run -go/flag:test-test.short -go/flag:test-test.shuffle -go/flag:test-test.skip -go/flag:test-test.timeout -go/flag:test-test.trace -go/flag:test-test.v -go/flag:test-timeout -go/flag:test-toolexec -go/flag:test-trace -go/flag:test-trimpath -go/flag:test-v -go/flag:test-vet -go/flag:test-work -go/flag:test-x -go/subcommand:help-test -go/subcommand:tool-addr2line -go/subcommand:tool-asm -go/subcommand:tool-buildid -go/subcommand:tool-cgo -go/subcommand:tool-compile -go/subcommand:tool-covdata -go/subcommand:tool-cover -go/subcommand:tool-dist -go/subcommand:tool-distpack -go/subcommand:tool-doc -go/subcommand:tool-fix -go/subcommand:tool-link -go/subcommand:tool-nm -go/subcommand:tool-objdump -go/subcommand:tool-pack -go/subcommand:tool-pprof -go/subcommand:tool-preprofile -go/subcommand:tool-test2json -go/subcommand:tool-trace -go/subcommand:tool-vet -go/subcommand:tool-unknown -go/subcommand:tool -go/flag:tool-C -go/flag:tool-n -go/subcommand:help-tool -go/subcommand:version -go/flag:version-C -go/flag:version-m -go/flag:version-v -go/subcommand:help-version -go/subcommand:vet -go/flag:vet-C -go/flag:vet-a -go/flag:vet-asan -go/flag:vet-asmflags -go/flag:vet-buildmode -go/flag:vet-buildvcs -go/flag:vet-compiler -go/flag:vet-debug-actiongraph -go/flag:vet-debug-runtime-trace -go/flag:vet-debug-trace -go/flag:vet-gccgoflags -go/flag:vet-gcflags -go/flag:vet-installsuffix -go/flag:vet-ldflags -go/flag:vet-linkshared -go/flag:vet-mod -go/flag:vet-modcacherw -go/flag:vet-modfile -go/flag:vet-msan -go/flag:vet-n -go/flag:vet-overlay -go/flag:vet-p -go/flag:vet-pgo -go/flag:vet-pkgdir -go/flag:vet-race -go/flag:vet-tags -go/flag:vet-toolexec -go/flag:vet-trimpath -go/flag:vet-v -go/flag:vet-vettool -go/flag:vet-work -go/flag:vet-x -go/subcommand:help-vet -go/subcommand:help-buildconstraint -go/subcommand:help-buildmode -go/subcommand:help-c -go/subcommand:help-cache -go/subcommand:help-environment -go/subcommand:help-filetype -go/subcommand:help-go.mod -go/subcommand:help-gopath -go/subcommand:help-goproxy -go/subcommand:help-importpath -go/subcommand:help-modules -go/subcommand:help-module-auth -go/subcommand:help-packages -go/subcommand:help-private -go/subcommand:help-testflag -go/subcommand:help-testfunc -go/subcommand:help-vcs -go/errors:gomodcache-entry-relative -go/errors:gopath-entry-relative -go/errors:help-unknown-topic -go/errors:invalid-toolchain-in-file -go/toolchain/select-exec -go/toolchain/switch-exec From c8744c56dc755d02aab690cf588f5d0ec4fecf90 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Wed, 8 May 2024 06:22:31 +0800 Subject: [PATCH 26/28] n Change-Id: Iabbb564b16abf1be55280d39152155f6748ec5fb --- src/cmd/go/internal/cfg/cfg.go | 7 ++++--- src/cmd/go/internal/envcmd/env.go | 4 ++-- src/cmd/go/testdata/script/env_changed.txt | 12 ++++++------ src/go/build/build.go | 2 +- src/go/build/deps_test.go | 6 ++---- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index 46e6d9fd978313..a037e21be51624 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -103,6 +103,7 @@ var ( // explanation why GOPATH is unset. GoPathError string GOPATHChanged bool + CGOChanged bool ) func defaultContext() build.Context { @@ -131,7 +132,7 @@ func defaultContext() build.Context { // 3. Otherwise, use built-in default for GOOS/GOARCH. // Recreate that logic here with the new GOOS/GOARCH setting. if v := Getenv("CGO_ENABLED"); v == "0" || v == "1" { - ctxt.CgoEnabled = v[0] == '1' + ctxt.CgoEnabled, CGOChanged = v[0] == '1', v[0] != '0' } else if ctxt.GOOS != runtime.GOOS || ctxt.GOARCH != runtime.GOARCH { ctxt.CgoEnabled = false } else { @@ -411,7 +412,7 @@ var ( GOMIPS, goMIPSChanged = EnvOrAndChanged("GOMIPS", buildcfg.GOMIPS) GOMIPS64, goMIPS64Changed = EnvOrAndChanged("GOMIPS64", buildcfg.GOMIPS64) GOPPC64, goPPC64Changed = EnvOrAndChanged("GOPPC64", fmt.Sprintf("%s%d", "power", buildcfg.GOPPC64)) - GORISCV64, gORISCV64Changed = EnvOrAndChanged("GORISCV64", fmt.Sprintf("rva%du64", buildcfg.GORISCV64)) + GORISCV64, goRISCV64Changed = EnvOrAndChanged("GORISCV64", fmt.Sprintf("rva%du64", buildcfg.GORISCV64)) GOWASM, goWASMChanged = EnvOrAndChanged("GOWASM", fmt.Sprint(buildcfg.GOWASM)) GOPROXY, GOPROXYChanged = EnvOrAndChanged("GOPROXY", "") @@ -456,7 +457,7 @@ func GetArchEnv() (key, val string, changed bool) { case "ppc64", "ppc64le": return "GOPPC64", GOPPC64, goPPC64Changed case "riscv64": - return "GORISCV64", GORISCV64, gORISCV64Changed + return "GORISCV64", GORISCV64, goRISCV64Changed case "wasm": return "GOWASM", GOWASM, goWASMChanged } diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index 0fdacae557ef55..0fff28dba495b9 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -162,9 +162,9 @@ func MkEnv() []cfg.EnvVar { env = append(env, cfg.EnvVar{Name: "CXX", Value: cxx, Changed: cxxChanged}) if cfg.BuildContext.CgoEnabled { - env = append(env, cfg.EnvVar{Name: "CGO_ENABLED", Value: "1", Changed: true}) + env = append(env, cfg.EnvVar{Name: "CGO_ENABLED", Value: "1", Changed: cfg.CGOChanged}) } else { - env = append(env, cfg.EnvVar{Name: "CGO_ENABLED", Value: "0"}) + env = append(env, cfg.EnvVar{Name: "CGO_ENABLED", Value: "0", Changed: cfg.CGOChanged}) } return env diff --git a/src/cmd/go/testdata/script/env_changed.txt b/src/cmd/go/testdata/script/env_changed.txt index f1818103bfc280..7b7b154dae6bf9 100644 --- a/src/cmd/go/testdata/script/env_changed.txt +++ b/src/cmd/go/testdata/script/env_changed.txt @@ -10,12 +10,12 @@ env CGO_CPPFLAGS=nodefault go env -changed # linux output like GOTOOLCHAIN='local' # windows output like GOTOOLCHAIN=local -stdout 'GOTOOLCHAIN=local|GOTOOLCHAIN=''local''' -stdout 'GOSUMDB=nodefault|GOSUMDB=''nodefault''' -stdout 'GOPROXY=nodefault|GOPROXY=''nodefault''' -stdout 'GO111MODULE=auto|GO111MODULE=''auto''' -stdout 'CGO_CFLAGS=nodefault|CGO_CFLAGS=''nodefault''' -stdout 'CGO_CPPFLAGS=nodefault|CGO_CPPFLAGS=''nodefault''' +stdout 'GOTOOLCHAIN=''?local''?' +stdout 'GOSUMDB=''?nodefault''?' +stdout 'GOPROXY=''?nodefault''?' +stdout 'GO111MODULE=''?auto''?' +stdout 'CGO_CFLAGS=''?nodefault''?' +stdout 'CGO_CPPFLAGS=''?nodefault''?' go env -changed -json stdout '"GOTOOLCHAIN": "local"' diff --git a/src/go/build/build.go b/src/go/build/build.go index 1f003ae1fd2609..43c74cb99adf87 100644 --- a/src/go/build/build.go +++ b/src/go/build/build.go @@ -286,7 +286,7 @@ func (ctxt *Context) SrcDirs() []string { // if set, or else the compiled code's GOARCH, GOOS, and GOROOT. var Default Context = defaultContext() -// defaultGOPATH now semantics are actually the same in cmd/go/internal/cfg.gopath. +// Keep consistent with cmd/go/internal/cfg.defaultGOPATH. func defaultGOPATH() string { env := "HOME" if runtime.GOOS == "windows" { diff --git a/src/go/build/deps_test.go b/src/go/build/deps_test.go index 1f184473dad95b..14880d9ef12021 100644 --- a/src/go/build/deps_test.go +++ b/src/go/build/deps_test.go @@ -40,7 +40,7 @@ var depsRules = ` # No dependencies allowed for any of these packages. NONE < cmp, container/list, container/ring, - internal/coverage, internal/coverage/rtcov, + internal/cfg, internal/coverage, internal/coverage/rtcov, internal/coverage/uleb128, internal/coverage/calloc, internal/cpu, internal/goarch, internal/godebugs, internal/goexperiment, internal/goos, @@ -323,9 +323,7 @@ var depsRules = ` go/doc/comment, go/parser, internal/lazyregexp, text/template < go/doc; - os, path/filepath, runtime < internal/cfg; - - go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion, internal/platform, internal/cfg + go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion, internal/platform < go/build; # databases From 4ece9ceb75277e17e0cb65136d86989bbbaa3430 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Thu, 9 May 2024 06:18:09 +0800 Subject: [PATCH 27/28] n Change-Id: Ib6afec3f7309f5190ac5e21d8fa9711aff1c29a3 --- src/cmd/go/internal/cfg/cfg.go | 9 ++++++++- src/cmd/go/internal/envcmd/env.go | 3 ++- src/internal/cfg/cfg.go | 1 - 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index a037e21be51624..f78e8cede4ce6f 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -13,6 +13,7 @@ import ( "go/build" "internal/buildcfg" "internal/cfg" + "internal/platform" "io" "os" "path/filepath" @@ -132,7 +133,7 @@ func defaultContext() build.Context { // 3. Otherwise, use built-in default for GOOS/GOARCH. // Recreate that logic here with the new GOOS/GOARCH setting. if v := Getenv("CGO_ENABLED"); v == "0" || v == "1" { - ctxt.CgoEnabled, CGOChanged = v[0] == '1', v[0] != '0' + ctxt.CgoEnabled = v[0] == '1' } else if ctxt.GOOS != runtime.GOOS || ctxt.GOARCH != runtime.GOARCH { ctxt.CgoEnabled = false } else { @@ -166,6 +167,12 @@ func defaultContext() build.Context { } } } + CGOChanged = ctxt.CgoEnabled != func() bool { + if runtime.GOARCH == ctxt.GOARCH && runtime.GOOS == ctxt.GOOS { + return platform.CgoSupported(ctxt.GOOS, ctxt.GOARCH) + } + return false + }() ctxt.OpenFile = func(path string) (io.ReadCloser, error) { return fsys.Open(path) diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index 0fff28dba495b9..b3838a75e253b1 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -129,7 +129,8 @@ func MkEnv() []cfg.EnvVar { env[i].Changed = true } case "GODEBUG": - env[i].Value, env[i].Changed = cfg.EnvOrAndChanged("GODEBUG", "") + env[i].Value = os.Getenv("GODEBUG") + env[i].Changed = env[i].Value != "" } } diff --git a/src/internal/cfg/cfg.go b/src/internal/cfg/cfg.go index bde29d0f7c29fc..08d210b797385b 100644 --- a/src/internal/cfg/cfg.go +++ b/src/internal/cfg/cfg.go @@ -69,5 +69,4 @@ const KnownEnv = ` GOWORK GO_EXTLINK_ENABLED PKG_CONFIG - GODEBUG ` From 6543df4784cff1ba5751dc9885ef502e69679118 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Fri, 10 May 2024 06:07:31 +0800 Subject: [PATCH 28/28] n Change-Id: I23851c3d47c5727fe9ccf6b318693ca30c872409 --- src/cmd/go/internal/cfg/cfg.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index f78e8cede4ce6f..002d0006ed41fc 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -13,7 +13,6 @@ import ( "go/build" "internal/buildcfg" "internal/cfg" - "internal/platform" "io" "os" "path/filepath" @@ -128,14 +127,16 @@ func defaultContext() build.Context { ctxt.ToolTags = save // The go/build rule for whether cgo is enabled is: - // 1. If $CGO_ENABLED is set, respect it. - // 2. Otherwise, if this is a cross-compile, disable cgo. - // 3. Otherwise, use built-in default for GOOS/GOARCH. + // 1. If $CGO_ENABLED is set, respect it. + // 2. Otherwise, if this is a cross-compile, disable cgo. + // 3. Otherwise, use built-in default for GOOS/GOARCH. + // // Recreate that logic here with the new GOOS/GOARCH setting. - if v := Getenv("CGO_ENABLED"); v == "0" || v == "1" { - ctxt.CgoEnabled = v[0] == '1' - } else if ctxt.GOOS != runtime.GOOS || ctxt.GOARCH != runtime.GOARCH { - ctxt.CgoEnabled = false + // We need to run steps 2 and 3 to determine what the default value + // of CgoEnabled would be for computing CGOChanged. + defaultCgoEnabled := ctxt.CgoEnabled + if ctxt.GOOS != runtime.GOOS || ctxt.GOARCH != runtime.GOARCH { + defaultCgoEnabled = false } else { // Use built-in default cgo setting for GOOS/GOARCH. // Note that ctxt.GOOS/GOARCH are derived from the preference list @@ -162,17 +163,16 @@ func defaultContext() build.Context { if os.Getenv("CC") == "" { cc := DefaultCC(ctxt.GOOS, ctxt.GOARCH) if _, err := LookPath(cc); err != nil { - ctxt.CgoEnabled = false + defaultCgoEnabled = false } } } } - CGOChanged = ctxt.CgoEnabled != func() bool { - if runtime.GOARCH == ctxt.GOARCH && runtime.GOOS == ctxt.GOOS { - return platform.CgoSupported(ctxt.GOOS, ctxt.GOARCH) - } - return false - }() + ctxt.CgoEnabled = defaultCgoEnabled + if v := Getenv("CGO_ENABLED"); v == "0" || v == "1" { + ctxt.CgoEnabled = v[0] == '1' + } + CGOChanged = ctxt.CgoEnabled != defaultCgoEnabled ctxt.OpenFile = func(path string) (io.ReadCloser, error) { return fsys.Open(path)