Skip to content

Commit 69756b3

Browse files
author
Bryan C. Mills
committed
cmd/dist: move more environment logic into cmd/dist from make and run scripts
'go tool dist env' outputs different (and fewer) environment variables than 'go env'. The 'go tool dist env' variables should be authoritative, whereas many printed by 'go env' are merely informational (and not intended to be overridden in the actual environment). Fixes #52009 Change-Id: Ic0590153875183135cebf7ca55ead7c2b4038569 Reviewed-on: https://go-review.googlesource.com/c/go/+/398061 Trust: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 5a6a830 commit 69756b3

File tree

8 files changed

+41
-66
lines changed

8 files changed

+41
-66
lines changed

src/cmd/dist/build.go

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
// The usual variables.
2727
var (
2828
goarch string
29-
gobin string
29+
gorootBin string
3030
gohostarch string
3131
gohostos string
3232
goos string
@@ -112,19 +112,14 @@ func xinit() {
112112
fatalf("$GOROOT must be set")
113113
}
114114
goroot = filepath.Clean(b)
115+
gorootBin = pathf("%s/bin", goroot)
115116

116117
b = os.Getenv("GOROOT_FINAL")
117118
if b == "" {
118119
b = goroot
119120
}
120121
goroot_final = b
121122

122-
b = os.Getenv("GOBIN")
123-
if b == "" {
124-
b = pathf("%s/bin", goroot)
125-
}
126-
gobin = b
127-
128123
b = os.Getenv("GOOS")
129124
if b == "" {
130125
b = gohostos
@@ -241,9 +236,19 @@ func xinit() {
241236
// make.bash really does start from a clean slate.
242237
os.Setenv("GOCACHE", pathf("%s/pkg/obj/go-build", goroot))
243238

239+
// Set GOBIN to GOROOT/bin. The meaning of GOBIN has drifted over time
240+
// (see https://go.dev/issue/3269, https://go.dev/cl/183058,
241+
// https://go.dev/issue/31576). Since we want binaries installed by 'dist' to
242+
// always go to GOROOT/bin anyway.
243+
os.Setenv("GOBIN", gorootBin)
244+
244245
// Make the environment more predictable.
245246
os.Setenv("LANG", "C")
246247
os.Setenv("LANGUAGE", "en_US.UTF8")
248+
os.Unsetenv("GO111MODULE")
249+
os.Setenv("GOENV", "off")
250+
os.Unsetenv("GOFLAGS")
251+
os.Setenv("GOWORK", "off")
247252

248253
workdir = xworkdir()
249254
if err := ioutil.WriteFile(pathf("%s/go.mod", workdir), []byte("module bootstrap"), 0666); err != nil {
@@ -490,16 +495,6 @@ func setup() {
490495
xremove(pathf("%s/bin/%s", goroot, old))
491496
}
492497

493-
// If $GOBIN is set and has a Go compiler, it must be cleaned.
494-
for _, char := range "56789" {
495-
if isfile(pathf("%s/%c%s", gobin, char, "g")) {
496-
for _, old := range oldtool {
497-
xremove(pathf("%s/%s", gobin, old))
498-
}
499-
break
500-
}
501-
}
502-
503498
// For release, make sure excluded things are excluded.
504499
goversion := findgoversion()
505500
if strings.HasPrefix(goversion, "release.") || (strings.HasPrefix(goversion, "go") && !strings.Contains(goversion, "beta")) {
@@ -1126,8 +1121,8 @@ func clean() {
11261121
// The env command prints the default environment.
11271122
func cmdenv() {
11281123
path := flag.Bool("p", false, "emit updated PATH")
1129-
plan9 := flag.Bool("9", false, "emit plan 9 syntax")
1130-
windows := flag.Bool("w", false, "emit windows syntax")
1124+
plan9 := flag.Bool("9", gohostos == "plan9", "emit plan 9 syntax")
1125+
windows := flag.Bool("w", gohostos == "windows", "emit windows syntax")
11311126
xflagparse(0)
11321127

11331128
format := "%s=\"%s\"\n"
@@ -1138,10 +1133,13 @@ func cmdenv() {
11381133
format = "set %s=%s\r\n"
11391134
}
11401135

1136+
xprintf(format, "GO111MODULE", "")
11411137
xprintf(format, "GOARCH", goarch)
1142-
xprintf(format, "GOBIN", gobin)
1138+
xprintf(format, "GOBIN", gorootBin)
11431139
xprintf(format, "GOCACHE", os.Getenv("GOCACHE"))
11441140
xprintf(format, "GODEBUG", os.Getenv("GODEBUG"))
1141+
xprintf(format, "GOENV", "off")
1142+
xprintf(format, "GOFLAGS", "")
11451143
xprintf(format, "GOHOSTARCH", gohostarch)
11461144
xprintf(format, "GOHOSTOS", gohostos)
11471145
xprintf(format, "GOOS", goos)
@@ -1167,13 +1165,14 @@ func cmdenv() {
11671165
if goarch == "ppc64" || goarch == "ppc64le" {
11681166
xprintf(format, "GOPPC64", goppc64)
11691167
}
1168+
xprintf(format, "GOWORK", "off")
11701169

11711170
if *path {
11721171
sep := ":"
11731172
if gohostos == "windows" {
11741173
sep = ";"
11751174
}
1176-
xprintf(format, "PATH", fmt.Sprintf("%s%s%s", gobin, sep, os.Getenv("PATH")))
1175+
xprintf(format, "PATH", fmt.Sprintf("%s%s%s", gorootBin, sep, os.Getenv("PATH")))
11771176
}
11781177
}
11791178

@@ -1318,7 +1317,7 @@ func cmdbootstrap() {
13181317
gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now
13191318
goldflags = os.Getenv("GO_LDFLAGS") // we were using $BOOT_GO_LDFLAGS until now
13201319
goBootstrap := pathf("%s/go_bootstrap", tooldir)
1321-
cmdGo := pathf("%s/go", gobin)
1320+
cmdGo := pathf("%s/go", gorootBin)
13221321
if debug {
13231322
run("", ShowOutput|CheckExit, pathf("%s/compile", tooldir), "-V=full")
13241323
copyfile(pathf("%s/compile1", tooldir), pathf("%s/compile", tooldir), writeExec)
@@ -1457,7 +1456,7 @@ func cmdbootstrap() {
14571456
os.Setenv("GOOS", gohostos)
14581457
os.Setenv("GOARCH", gohostarch)
14591458
os.Setenv("CC", compilerEnvLookup(defaultcc, gohostos, gohostarch))
1460-
goCmd(cmdGo, "build", "-o", pathf("%s/go_%s_%s_exec%s", gobin, goos, goarch, exe), wrapperPath)
1459+
goCmd(cmdGo, "build", "-o", pathf("%s/go_%s_%s_exec%s", gorootBin, goos, goarch, exe), wrapperPath)
14611460
// Restore environment.
14621461
// TODO(elias.naur): support environment variables in goCmd?
14631462
os.Setenv("GOOS", goos)
@@ -1681,26 +1680,26 @@ func banner() {
16811680
}
16821681
xprintf("---\n")
16831682
xprintf("Installed Go for %s/%s in %s\n", goos, goarch, goroot)
1684-
xprintf("Installed commands in %s\n", gobin)
1683+
xprintf("Installed commands in %s\n", gorootBin)
16851684

16861685
if !xsamefile(goroot_final, goroot) {
16871686
// If the files are to be moved, don't check that gobin
16881687
// is on PATH; assume they know what they are doing.
16891688
} else if gohostos == "plan9" {
1690-
// Check that gobin is bound before /bin.
1689+
// Check that GOROOT/bin is bound before /bin.
16911690
pid := strings.Replace(readfile("#c/pid"), " ", "", -1)
16921691
ns := fmt.Sprintf("/proc/%s/ns", pid)
1693-
if !strings.Contains(readfile(ns), fmt.Sprintf("bind -b %s /bin", gobin)) {
1694-
xprintf("*** You need to bind %s before /bin.\n", gobin)
1692+
if !strings.Contains(readfile(ns), fmt.Sprintf("bind -b %s /bin", gorootBin)) {
1693+
xprintf("*** You need to bind %s before /bin.\n", gorootBin)
16951694
}
16961695
} else {
1697-
// Check that gobin appears in $PATH.
1696+
// Check that GOROOT/bin appears in $PATH.
16981697
pathsep := ":"
16991698
if gohostos == "windows" {
17001699
pathsep = ";"
17011700
}
1702-
if !strings.Contains(pathsep+os.Getenv("PATH")+pathsep, pathsep+gobin+pathsep) {
1703-
xprintf("*** You need to add %s to your PATH.\n", gobin)
1701+
if !strings.Contains(pathsep+os.Getenv("PATH")+pathsep, pathsep+gorootBin+pathsep) {
1702+
xprintf("*** You need to add %s to your PATH.\n", gorootBin)
17041703
}
17051704
}
17061705

src/cmd/dist/test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ func (t *tester) run() {
100100
if goos == "windows" {
101101
exeSuffix = ".exe"
102102
}
103-
if _, err := os.Stat(filepath.Join(gobin, "go"+exeSuffix)); err == nil {
104-
os.Setenv("PATH", fmt.Sprintf("%s%c%s", gobin, os.PathListSeparator, os.Getenv("PATH")))
103+
if _, err := os.Stat(filepath.Join(gorootBin, "go"+exeSuffix)); err == nil {
104+
os.Setenv("PATH", fmt.Sprintf("%s%c%s", gorootBin, os.PathListSeparator, os.Getenv("PATH")))
105105
}
106106

107107
cmd := exec.Command("go", "env", "CGO_ENABLED")

src/make.bash

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,6 @@
7373

7474
set -e
7575

76-
export GOENV=off
77-
export GOWORK=off # Issue 51558
78-
unset GOBIN # Issue 14340
79-
unset GOFLAGS
80-
unset GO111MODULE
81-
8276
if [ ! -f run.bash ]; then
8377
echo 'make.bash must be run from $GOROOT/src' 1>&2
8478
exit 1
@@ -204,7 +198,7 @@ if [ "$GOROOT_BOOTSTRAP" = "$GOROOT" ]; then
204198
exit 1
205199
fi
206200
rm -f cmd/dist/dist
207-
GOROOT="$GOROOT_BOOTSTRAP" GOOS="" GOARCH="" GO111MODULE=off GOEXPERIMENT="" "$GOROOT_BOOTSTRAP/bin/go" build -o cmd/dist/dist ./cmd/dist
201+
GOROOT="$GOROOT_BOOTSTRAP" GOOS="" GOARCH="" GO111MODULE=off GOEXPERIMENT="" GOENV=off GOFLAGS="" "$GOROOT_BOOTSTRAP/bin/go" build -o cmd/dist/dist ./cmd/dist
208202

209203
# -e doesn't propagate out of eval, so check success by hand.
210204
eval $(./cmd/dist/dist env -p || echo FAIL=true)

src/make.bat

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ if x%4==x--no-local goto nolocal
4646
setlocal
4747
:nolocal
4848

49-
set GOENV=off
50-
set GOWORK=off
5149
set GOBUILDFAIL=0
52-
set GOFLAGS=
53-
set GO111MODULE=
5450

5551
if exist make.bat goto ok
5652
echo Must run make.bat from Go src directory.
@@ -102,6 +98,8 @@ set GOARCH=
10298
set GOBIN=
10399
set GOEXPERIMENT=
104100
set GO111MODULE=off
101+
set GOENV=off
102+
set GOFLAGS=
105103
"%GOROOT_BOOTSTRAP%\bin\go.exe" build -o cmd\dist\dist.exe .\cmd\dist
106104
endlocal
107105
if errorlevel 1 goto fail

src/make.rc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ if(~ $1 -v) {
4747
shift
4848
}
4949
50-
GOENV=off
51-
GOWORK=off
52-
GOFLAGS=()
53-
GO111MODULE=()
5450
GOROOT = `{cd .. && pwd}
5551
goroot_bootstrap_set = 'true'
5652
if(! ~ $#GOROOT_BOOTSTRAP 1){
@@ -88,7 +84,7 @@ if(~ $GOROOT_BOOTSTRAP $GOROOT){
8884
echo 'Building Go cmd/dist using '^$GOROOT_BOOTSTRAP
8985
if(~ $#vflag 1)
9086
echo cmd/dist
91-
GOROOT=$GOROOT_BOOTSTRAP GOOS='' GOARCH='' GOEXPERIMENT='' GO111MODULE=off $GOROOT_BOOTSTRAP/bin/go build -o cmd/dist/dist ./cmd/dist
87+
GOROOT=$GOROOT_BOOTSTRAP GOOS='' GOARCH='' GOEXPERIMENT='' GO111MODULE=off GOENV=off GOFLAGS='' $GOROOT_BOOTSTRAP/bin/go build -o cmd/dist/dist ./cmd/dist
9288

9389
eval `{./cmd/dist/dist env -9}
9490
if(~ $#vflag 1)

src/run.bash

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,10 @@ if [ ! -f ../bin/go ]; then
2121
exit 1
2222
fi
2323

24-
eval $(../bin/go env)
24+
eval $(../bin/go tool dist env)
2525
export GOROOT # The api test requires GOROOT to be set, so set it to match ../bin/go.
26-
export GOPATH=/nonexist-gopath
2726

2827
unset CDPATH # in case user has it set
29-
export GOBIN=$GOROOT/bin # Issue 14340
30-
unset GOFLAGS
31-
unset GO111MODULE
3228

3329
export GOHOSTOS
3430
export CC
@@ -53,4 +49,5 @@ if ulimit -T &> /dev/null; then
5349
[ "$(ulimit -H -T)" = "unlimited" ] || ulimit -S -T $(ulimit -H -T)
5450
fi
5551

52+
export GOPATH=/nonexist-gopath
5653
exec ../bin/go tool dist test -rebuild "$@"

src/run.bat

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,13 @@ setlocal
1818

1919
set GOBUILDFAIL=0
2020

21-
set GOPATH=c:\nonexist-gopath
22-
:: Issue 14340: ignore GOBIN during all.bat.
23-
set GOBIN=
24-
set GOFLAGS=
25-
set GO111MODULE=
26-
27-
:: get CGO_ENABLED
28-
..\bin\go env > env.bat
21+
..\bin\go tool dist env > env.bat
2922
if errorlevel 1 goto fail
3023
call env.bat
3124
del env.bat
3225

26+
set GOPATH=c:\nonexist-gopath
27+
3328
if x%1==x--no-rebuild goto norebuild
3429
..\bin\go tool dist test --rebuild
3530
if errorlevel 1 goto fail

src/run.rc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ if(! test -f ../bin/go){
1010
exit wrongdir
1111
}
1212

13-
eval `{../bin/go env}
13+
eval `{../bin/go tool dist env}
1414
1515
GOPATH=/nonexist-gopath
16-
GOBIN=() # Issue 14340
17-
GOFLAGS=()
18-
GO111MODULE=()
19-
2016
exec ../bin/go tool dist test -rebuild $*

0 commit comments

Comments
 (0)