}
func init() {
- SetGOROOT(findGOROOT(), false)
+ SetGOROOT(Getenv("GOROOT"), false)
BuildToolchainCompiler = func() string { return "missing-compiler" }
BuildToolchainLinker = func() string { return "missing-linker" }
}
func initEnvCache() {
envCache.m = make(map[string]string)
- file, _ := EnvFile()
+ if file, _ := EnvFile(); file != "" {
+ readEnvFile(file, "user")
+ }
+ goroot := findGOROOT(envCache.m["GOROOT"])
+ if goroot != "" {
+ readEnvFile(filepath.Join(goroot, "go.env"), "GOROOT")
+ }
+
+ // Save the goroot for func init calling SetGOROOT,
+ // and also overwrite anything that might have been in go.env.
+ // It makes no sense for GOROOT/go.env to specify
+ // a different GOROOT.
+ envCache.m["GOROOT"] = goroot
+}
+
+func readEnvFile(file string, source string) {
if file == "" {
return
}
i = bytes.IndexByte(line, '=')
if i < 0 || line[0] < 'A' || 'Z' < line[0] {
// Line is missing = (or empty) or a comment or not a valid env name. Ignore.
- // (This should not happen, since the file should be maintained almost
+ // This should not happen in the user file, since the file should be maintained almost
// exclusively by "go env -w", but better to silently ignore than to make
// the go command unusable just because somehow the env file has
- // gotten corrupted.)
+ // gotten corrupted.
+ // In the GOROOT/go.env file, we expect comments.
continue
}
key, val := line[:i], line[i+1:]
+
+ if source == "GOROOT" {
+ // In the GOROOT/go.env file, do not overwrite fields loaded from the user's go/env file.
+ if _, ok := envCache.m[string(key)]; ok {
+ continue
+ }
+ }
envCache.m[string(key)] = string(val)
}
}
GOPPC64 = envOr("GOPPC64", fmt.Sprintf("%s%d", "power", buildcfg.GOPPC64))
GOWASM = envOr("GOWASM", fmt.Sprint(buildcfg.GOWASM))
- GOPROXY = envOr("GOPROXY", "https://proxy.golang.org,direct")
- GOSUMDB = envOr("GOSUMDB", "sum.golang.org")
+ GOPROXY = envOr("GOPROXY", "")
+ GOSUMDB = envOr("GOSUMDB", "")
GOPRIVATE = Getenv("GOPRIVATE")
GONOPROXY = envOr("GONOPROXY", GOPRIVATE)
GONOSUMDB = envOr("GONOSUMDB", GOPRIVATE)
// with from runtime.GOROOT().
//
// There is a copy of this code in x/tools/cmd/godoc/goroot.go.
-func findGOROOT() string {
- if env := Getenv("GOROOT"); env != "" {
+func findGOROOT(env string) string {
+ if env == "" {
+ // Not using Getenv because findGOROOT is called
+ // to find the GOROOT/go.env file. initEnvCache
+ // has passed in the setting from the user go/env file.
+ env = os.Getenv("GOROOT")
+ }
+ if env != "" {
return filepath.Clean(env)
}
def := ""