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 <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Bryan C. Mills 2022-04-05 14:25:34 -04:00 committed by Bryan Mills
parent 5a6a830c1c
commit 69756b38f2
8 changed files with 41 additions and 66 deletions

59
src/cmd/dist/build.go vendored
View File

@ -26,7 +26,7 @@ import (
// The usual variables. // The usual variables.
var ( var (
goarch string goarch string
gobin string gorootBin string
gohostarch string gohostarch string
gohostos string gohostos string
goos string goos string
@ -112,6 +112,7 @@ func xinit() {
fatalf("$GOROOT must be set") fatalf("$GOROOT must be set")
} }
goroot = filepath.Clean(b) goroot = filepath.Clean(b)
gorootBin = pathf("%s/bin", goroot)
b = os.Getenv("GOROOT_FINAL") b = os.Getenv("GOROOT_FINAL")
if b == "" { if b == "" {
@ -119,12 +120,6 @@ func xinit() {
} }
goroot_final = b goroot_final = b
b = os.Getenv("GOBIN")
if b == "" {
b = pathf("%s/bin", goroot)
}
gobin = b
b = os.Getenv("GOOS") b = os.Getenv("GOOS")
if b == "" { if b == "" {
b = gohostos b = gohostos
@ -241,9 +236,19 @@ func xinit() {
// make.bash really does start from a clean slate. // make.bash really does start from a clean slate.
os.Setenv("GOCACHE", pathf("%s/pkg/obj/go-build", goroot)) os.Setenv("GOCACHE", pathf("%s/pkg/obj/go-build", goroot))
// Set GOBIN to GOROOT/bin. The meaning of GOBIN has drifted over time
// (see https://go.dev/issue/3269, https://go.dev/cl/183058,
// https://go.dev/issue/31576). Since we want binaries installed by 'dist' to
// always go to GOROOT/bin anyway.
os.Setenv("GOBIN", gorootBin)
// Make the environment more predictable. // Make the environment more predictable.
os.Setenv("LANG", "C") os.Setenv("LANG", "C")
os.Setenv("LANGUAGE", "en_US.UTF8") os.Setenv("LANGUAGE", "en_US.UTF8")
os.Unsetenv("GO111MODULE")
os.Setenv("GOENV", "off")
os.Unsetenv("GOFLAGS")
os.Setenv("GOWORK", "off")
workdir = xworkdir() workdir = xworkdir()
if err := ioutil.WriteFile(pathf("%s/go.mod", workdir), []byte("module bootstrap"), 0666); err != nil { if err := ioutil.WriteFile(pathf("%s/go.mod", workdir), []byte("module bootstrap"), 0666); err != nil {
@ -490,16 +495,6 @@ func setup() {
xremove(pathf("%s/bin/%s", goroot, old)) xremove(pathf("%s/bin/%s", goroot, old))
} }
// If $GOBIN is set and has a Go compiler, it must be cleaned.
for _, char := range "56789" {
if isfile(pathf("%s/%c%s", gobin, char, "g")) {
for _, old := range oldtool {
xremove(pathf("%s/%s", gobin, old))
}
break
}
}
// For release, make sure excluded things are excluded. // For release, make sure excluded things are excluded.
goversion := findgoversion() goversion := findgoversion()
if strings.HasPrefix(goversion, "release.") || (strings.HasPrefix(goversion, "go") && !strings.Contains(goversion, "beta")) { if strings.HasPrefix(goversion, "release.") || (strings.HasPrefix(goversion, "go") && !strings.Contains(goversion, "beta")) {
@ -1126,8 +1121,8 @@ func clean() {
// The env command prints the default environment. // The env command prints the default environment.
func cmdenv() { func cmdenv() {
path := flag.Bool("p", false, "emit updated PATH") path := flag.Bool("p", false, "emit updated PATH")
plan9 := flag.Bool("9", false, "emit plan 9 syntax") plan9 := flag.Bool("9", gohostos == "plan9", "emit plan 9 syntax")
windows := flag.Bool("w", false, "emit windows syntax") windows := flag.Bool("w", gohostos == "windows", "emit windows syntax")
xflagparse(0) xflagparse(0)
format := "%s=\"%s\"\n" format := "%s=\"%s\"\n"
@ -1138,10 +1133,13 @@ func cmdenv() {
format = "set %s=%s\r\n" format = "set %s=%s\r\n"
} }
xprintf(format, "GO111MODULE", "")
xprintf(format, "GOARCH", goarch) xprintf(format, "GOARCH", goarch)
xprintf(format, "GOBIN", gobin) xprintf(format, "GOBIN", gorootBin)
xprintf(format, "GOCACHE", os.Getenv("GOCACHE")) xprintf(format, "GOCACHE", os.Getenv("GOCACHE"))
xprintf(format, "GODEBUG", os.Getenv("GODEBUG")) xprintf(format, "GODEBUG", os.Getenv("GODEBUG"))
xprintf(format, "GOENV", "off")
xprintf(format, "GOFLAGS", "")
xprintf(format, "GOHOSTARCH", gohostarch) xprintf(format, "GOHOSTARCH", gohostarch)
xprintf(format, "GOHOSTOS", gohostos) xprintf(format, "GOHOSTOS", gohostos)
xprintf(format, "GOOS", goos) xprintf(format, "GOOS", goos)
@ -1167,13 +1165,14 @@ func cmdenv() {
if goarch == "ppc64" || goarch == "ppc64le" { if goarch == "ppc64" || goarch == "ppc64le" {
xprintf(format, "GOPPC64", goppc64) xprintf(format, "GOPPC64", goppc64)
} }
xprintf(format, "GOWORK", "off")
if *path { if *path {
sep := ":" sep := ":"
if gohostos == "windows" { if gohostos == "windows" {
sep = ";" sep = ";"
} }
xprintf(format, "PATH", fmt.Sprintf("%s%s%s", gobin, sep, os.Getenv("PATH"))) xprintf(format, "PATH", fmt.Sprintf("%s%s%s", gorootBin, sep, os.Getenv("PATH")))
} }
} }
@ -1318,7 +1317,7 @@ func cmdbootstrap() {
gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now
goldflags = os.Getenv("GO_LDFLAGS") // we were using $BOOT_GO_LDFLAGS until now goldflags = os.Getenv("GO_LDFLAGS") // we were using $BOOT_GO_LDFLAGS until now
goBootstrap := pathf("%s/go_bootstrap", tooldir) goBootstrap := pathf("%s/go_bootstrap", tooldir)
cmdGo := pathf("%s/go", gobin) cmdGo := pathf("%s/go", gorootBin)
if debug { if debug {
run("", ShowOutput|CheckExit, pathf("%s/compile", tooldir), "-V=full") run("", ShowOutput|CheckExit, pathf("%s/compile", tooldir), "-V=full")
copyfile(pathf("%s/compile1", tooldir), pathf("%s/compile", tooldir), writeExec) copyfile(pathf("%s/compile1", tooldir), pathf("%s/compile", tooldir), writeExec)
@ -1457,7 +1456,7 @@ func cmdbootstrap() {
os.Setenv("GOOS", gohostos) os.Setenv("GOOS", gohostos)
os.Setenv("GOARCH", gohostarch) os.Setenv("GOARCH", gohostarch)
os.Setenv("CC", compilerEnvLookup(defaultcc, gohostos, gohostarch)) os.Setenv("CC", compilerEnvLookup(defaultcc, gohostos, gohostarch))
goCmd(cmdGo, "build", "-o", pathf("%s/go_%s_%s_exec%s", gobin, goos, goarch, exe), wrapperPath) goCmd(cmdGo, "build", "-o", pathf("%s/go_%s_%s_exec%s", gorootBin, goos, goarch, exe), wrapperPath)
// Restore environment. // Restore environment.
// TODO(elias.naur): support environment variables in goCmd? // TODO(elias.naur): support environment variables in goCmd?
os.Setenv("GOOS", goos) os.Setenv("GOOS", goos)
@ -1681,26 +1680,26 @@ func banner() {
} }
xprintf("---\n") xprintf("---\n")
xprintf("Installed Go for %s/%s in %s\n", goos, goarch, goroot) xprintf("Installed Go for %s/%s in %s\n", goos, goarch, goroot)
xprintf("Installed commands in %s\n", gobin) xprintf("Installed commands in %s\n", gorootBin)
if !xsamefile(goroot_final, goroot) { if !xsamefile(goroot_final, goroot) {
// If the files are to be moved, don't check that gobin // If the files are to be moved, don't check that gobin
// is on PATH; assume they know what they are doing. // is on PATH; assume they know what they are doing.
} else if gohostos == "plan9" { } else if gohostos == "plan9" {
// Check that gobin is bound before /bin. // Check that GOROOT/bin is bound before /bin.
pid := strings.Replace(readfile("#c/pid"), " ", "", -1) pid := strings.Replace(readfile("#c/pid"), " ", "", -1)
ns := fmt.Sprintf("/proc/%s/ns", pid) ns := fmt.Sprintf("/proc/%s/ns", pid)
if !strings.Contains(readfile(ns), fmt.Sprintf("bind -b %s /bin", gobin)) { if !strings.Contains(readfile(ns), fmt.Sprintf("bind -b %s /bin", gorootBin)) {
xprintf("*** You need to bind %s before /bin.\n", gobin) xprintf("*** You need to bind %s before /bin.\n", gorootBin)
} }
} else { } else {
// Check that gobin appears in $PATH. // Check that GOROOT/bin appears in $PATH.
pathsep := ":" pathsep := ":"
if gohostos == "windows" { if gohostos == "windows" {
pathsep = ";" pathsep = ";"
} }
if !strings.Contains(pathsep+os.Getenv("PATH")+pathsep, pathsep+gobin+pathsep) { if !strings.Contains(pathsep+os.Getenv("PATH")+pathsep, pathsep+gorootBin+pathsep) {
xprintf("*** You need to add %s to your PATH.\n", gobin) xprintf("*** You need to add %s to your PATH.\n", gorootBin)
} }
} }

View File

@ -100,8 +100,8 @@ func (t *tester) run() {
if goos == "windows" { if goos == "windows" {
exeSuffix = ".exe" exeSuffix = ".exe"
} }
if _, err := os.Stat(filepath.Join(gobin, "go"+exeSuffix)); err == nil { if _, err := os.Stat(filepath.Join(gorootBin, "go"+exeSuffix)); err == nil {
os.Setenv("PATH", fmt.Sprintf("%s%c%s", gobin, os.PathListSeparator, os.Getenv("PATH"))) os.Setenv("PATH", fmt.Sprintf("%s%c%s", gorootBin, os.PathListSeparator, os.Getenv("PATH")))
} }
cmd := exec.Command("go", "env", "CGO_ENABLED") cmd := exec.Command("go", "env", "CGO_ENABLED")

View File

@ -73,12 +73,6 @@
set -e set -e
export GOENV=off
export GOWORK=off # Issue 51558
unset GOBIN # Issue 14340
unset GOFLAGS
unset GO111MODULE
if [ ! -f run.bash ]; then if [ ! -f run.bash ]; then
echo 'make.bash must be run from $GOROOT/src' 1>&2 echo 'make.bash must be run from $GOROOT/src' 1>&2
exit 1 exit 1
@ -204,7 +198,7 @@ if [ "$GOROOT_BOOTSTRAP" = "$GOROOT" ]; then
exit 1 exit 1
fi fi
rm -f cmd/dist/dist rm -f cmd/dist/dist
GOROOT="$GOROOT_BOOTSTRAP" GOOS="" GOARCH="" GO111MODULE=off GOEXPERIMENT="" "$GOROOT_BOOTSTRAP/bin/go" build -o cmd/dist/dist ./cmd/dist GOROOT="$GOROOT_BOOTSTRAP" GOOS="" GOARCH="" GO111MODULE=off GOEXPERIMENT="" GOENV=off GOFLAGS="" "$GOROOT_BOOTSTRAP/bin/go" build -o cmd/dist/dist ./cmd/dist
# -e doesn't propagate out of eval, so check success by hand. # -e doesn't propagate out of eval, so check success by hand.
eval $(./cmd/dist/dist env -p || echo FAIL=true) eval $(./cmd/dist/dist env -p || echo FAIL=true)

View File

@ -46,11 +46,7 @@ if x%4==x--no-local goto nolocal
setlocal setlocal
:nolocal :nolocal
set GOENV=off
set GOWORK=off
set GOBUILDFAIL=0 set GOBUILDFAIL=0
set GOFLAGS=
set GO111MODULE=
if exist make.bat goto ok if exist make.bat goto ok
echo Must run make.bat from Go src directory. echo Must run make.bat from Go src directory.
@ -102,6 +98,8 @@ set GOARCH=
set GOBIN= set GOBIN=
set GOEXPERIMENT= set GOEXPERIMENT=
set GO111MODULE=off set GO111MODULE=off
set GOENV=off
set GOFLAGS=
"%GOROOT_BOOTSTRAP%\bin\go.exe" build -o cmd\dist\dist.exe .\cmd\dist "%GOROOT_BOOTSTRAP%\bin\go.exe" build -o cmd\dist\dist.exe .\cmd\dist
endlocal endlocal
if errorlevel 1 goto fail if errorlevel 1 goto fail

View File

@ -47,10 +47,6 @@ if(~ $1 -v) {
shift shift
} }
GOENV=off
GOWORK=off
GOFLAGS=()
GO111MODULE=()
GOROOT = `{cd .. && pwd} GOROOT = `{cd .. && pwd}
goroot_bootstrap_set = 'true' goroot_bootstrap_set = 'true'
if(! ~ $#GOROOT_BOOTSTRAP 1){ if(! ~ $#GOROOT_BOOTSTRAP 1){
@ -88,7 +84,7 @@ if(~ $GOROOT_BOOTSTRAP $GOROOT){
echo 'Building Go cmd/dist using '^$GOROOT_BOOTSTRAP echo 'Building Go cmd/dist using '^$GOROOT_BOOTSTRAP
if(~ $#vflag 1) if(~ $#vflag 1)
echo cmd/dist echo cmd/dist
GOROOT=$GOROOT_BOOTSTRAP GOOS='' GOARCH='' GOEXPERIMENT='' GO111MODULE=off $GOROOT_BOOTSTRAP/bin/go build -o cmd/dist/dist ./cmd/dist GOROOT=$GOROOT_BOOTSTRAP GOOS='' GOARCH='' GOEXPERIMENT='' GO111MODULE=off GOENV=off GOFLAGS='' $GOROOT_BOOTSTRAP/bin/go build -o cmd/dist/dist ./cmd/dist
eval `{./cmd/dist/dist env -9} eval `{./cmd/dist/dist env -9}
if(~ $#vflag 1) if(~ $#vflag 1)

View File

@ -21,14 +21,10 @@ if [ ! -f ../bin/go ]; then
exit 1 exit 1
fi fi
eval $(../bin/go env) eval $(../bin/go tool dist env)
export GOROOT # The api test requires GOROOT to be set, so set it to match ../bin/go. export GOROOT # The api test requires GOROOT to be set, so set it to match ../bin/go.
export GOPATH=/nonexist-gopath
unset CDPATH # in case user has it set unset CDPATH # in case user has it set
export GOBIN=$GOROOT/bin # Issue 14340
unset GOFLAGS
unset GO111MODULE
export GOHOSTOS export GOHOSTOS
export CC export CC
@ -53,4 +49,5 @@ if ulimit -T &> /dev/null; then
[ "$(ulimit -H -T)" = "unlimited" ] || ulimit -S -T $(ulimit -H -T) [ "$(ulimit -H -T)" = "unlimited" ] || ulimit -S -T $(ulimit -H -T)
fi fi
export GOPATH=/nonexist-gopath
exec ../bin/go tool dist test -rebuild "$@" exec ../bin/go tool dist test -rebuild "$@"

View File

@ -18,18 +18,13 @@ setlocal
set GOBUILDFAIL=0 set GOBUILDFAIL=0
set GOPATH=c:\nonexist-gopath ..\bin\go tool dist env > env.bat
:: Issue 14340: ignore GOBIN during all.bat.
set GOBIN=
set GOFLAGS=
set GO111MODULE=
:: get CGO_ENABLED
..\bin\go env > env.bat
if errorlevel 1 goto fail if errorlevel 1 goto fail
call env.bat call env.bat
del env.bat del env.bat
set GOPATH=c:\nonexist-gopath
if x%1==x--no-rebuild goto norebuild if x%1==x--no-rebuild goto norebuild
..\bin\go tool dist test --rebuild ..\bin\go tool dist test --rebuild
if errorlevel 1 goto fail if errorlevel 1 goto fail

View File

@ -10,11 +10,7 @@ if(! test -f ../bin/go){
exit wrongdir exit wrongdir
} }
eval `{../bin/go env} eval `{../bin/go tool dist env}
GOPATH=/nonexist-gopath GOPATH=/nonexist-gopath
GOBIN=() # Issue 14340
GOFLAGS=()
GO111MODULE=()
exec ../bin/go tool dist test -rebuild $* exec ../bin/go tool dist test -rebuild $*