Skip to content

Commit e39f2da

Browse files
rscpull[bot]
authored andcommitted
cmd/go: introduce GOROOT/go.env and move proxy/sumdb config there
Various Linux distributions edit cmd/go/internal/cfg/cfg.go to change the default settings of GOPROXY and GOSUMDB. Make it possible for them to do this without editing the go command source code by introducing GOROOT/go.env and moving those defaults there. With the upcoming changes for reproducible builds (#24904), this should mean that Linux distributions distribute binaries that are bit-for-bit identical to the Go distribution binaries, even when rebuilding the distribution themselves. Fixes #57179. Change-Id: Ib2ecc61e6d036f97db6fd47dca757c94fdea5629 Reviewed-on: https://go-review.googlesource.com/c/go/+/462198 Auto-Submit: Russ Cox <[email protected]> Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Russ Cox <[email protected]>
1 parent 0c9bdb9 commit e39f2da

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

go.env

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This file contains the initial defaults for go command configuration.
2+
# Values set by 'go env -w' and written to the user's go/env file override these.
3+
# The environment overrides everything else.
4+
5+
# Use the Go module mirror and checksum database by default.
6+
# See https://proxy.golang.org for details.
7+
GOPROXY=https://proxy.golang.org,direct
8+
GOSUMDB=sum.golang.org

src/cmd/go/internal/cfg/cfg.go

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func defaultContext() build.Context {
179179
}
180180

181181
func init() {
182-
SetGOROOT(findGOROOT(), false)
182+
SetGOROOT(Getenv("GOROOT"), false)
183183
BuildToolchainCompiler = func() string { return "missing-compiler" }
184184
BuildToolchainLinker = func() string { return "missing-linker" }
185185
}
@@ -303,7 +303,22 @@ func EnvFile() (string, error) {
303303

304304
func initEnvCache() {
305305
envCache.m = make(map[string]string)
306-
file, _ := EnvFile()
306+
if file, _ := EnvFile(); file != "" {
307+
readEnvFile(file, "user")
308+
}
309+
goroot := findGOROOT(envCache.m["GOROOT"])
310+
if goroot != "" {
311+
readEnvFile(filepath.Join(goroot, "go.env"), "GOROOT")
312+
}
313+
314+
// Save the goroot for func init calling SetGOROOT,
315+
// and also overwrite anything that might have been in go.env.
316+
// It makes no sense for GOROOT/go.env to specify
317+
// a different GOROOT.
318+
envCache.m["GOROOT"] = goroot
319+
}
320+
321+
func readEnvFile(file string, source string) {
307322
if file == "" {
308323
return
309324
}
@@ -325,13 +340,21 @@ func initEnvCache() {
325340
i = bytes.IndexByte(line, '=')
326341
if i < 0 || line[0] < 'A' || 'Z' < line[0] {
327342
// Line is missing = (or empty) or a comment or not a valid env name. Ignore.
328-
// (This should not happen, since the file should be maintained almost
343+
// This should not happen in the user file, since the file should be maintained almost
329344
// exclusively by "go env -w", but better to silently ignore than to make
330345
// the go command unusable just because somehow the env file has
331-
// gotten corrupted.)
346+
// gotten corrupted.
347+
// In the GOROOT/go.env file, we expect comments.
332348
continue
333349
}
334350
key, val := line[:i], line[i+1:]
351+
352+
if source == "GOROOT" {
353+
// In the GOROOT/go.env file, do not overwrite fields loaded from the user's go/env file.
354+
if _, ok := envCache.m[string(key)]; ok {
355+
continue
356+
}
357+
}
335358
envCache.m[string(key)] = string(val)
336359
}
337360
}
@@ -383,8 +406,8 @@ var (
383406
GOPPC64 = envOr("GOPPC64", fmt.Sprintf("%s%d", "power", buildcfg.GOPPC64))
384407
GOWASM = envOr("GOWASM", fmt.Sprint(buildcfg.GOWASM))
385408

386-
GOPROXY = envOr("GOPROXY", "https://proxy.golang.org,direct")
387-
GOSUMDB = envOr("GOSUMDB", "sum.golang.org")
409+
GOPROXY = envOr("GOPROXY", "")
410+
GOSUMDB = envOr("GOSUMDB", "")
388411
GOPRIVATE = Getenv("GOPRIVATE")
389412
GONOPROXY = envOr("GONOPROXY", GOPRIVATE)
390413
GONOSUMDB = envOr("GONOSUMDB", GOPRIVATE)
@@ -437,8 +460,14 @@ func envOr(key, def string) string {
437460
// with from runtime.GOROOT().
438461
//
439462
// There is a copy of this code in x/tools/cmd/godoc/goroot.go.
440-
func findGOROOT() string {
441-
if env := Getenv("GOROOT"); env != "" {
463+
func findGOROOT(env string) string {
464+
if env == "" {
465+
// Not using Getenv because findGOROOT is called
466+
// to find the GOROOT/go.env file. initEnvCache
467+
// has passed in the setting from the user go/env file.
468+
env = os.Getenv("GOROOT")
469+
}
470+
if env != "" {
442471
return filepath.Clean(env)
443472
}
444473
def := ""

0 commit comments

Comments
 (0)