Skip to content

Commit 841e63e

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
cmd/go: don't warn about GOROOT equal to GOPATH when both are the empty string
As of Go 1.19, runtime.GOROOT() reports the empty string if the binary was built with -trimpath. cmd/go/internal/cfg uses the path of the go command executable to reverse-engineer the correct GOROOT setting, but that means that cmd/go's "GOPATH set to GOROOT" warning needs to use cfg.GOROOT instead of runtime.GOROOT(). In addition, if we fail to find the GOROOT then there is no point in complaining about GOPATH also being empty: the missing GOROOT will stop everything right away anyway, so there is no point confusing the user with an additional warning about GOPATH. Updates #51461. Updates #18678. Updates #3207. Change-Id: Id7d0f4dc2f229c202dfda4e6e8af5dea909bb16f Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest,gotip-windows-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/go/+/543955 Reviewed-by: Michael Matloob <[email protected]> Auto-Submit: Bryan Mills <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent af12429 commit 841e63e

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/cmd/go/main.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"log"
1717
"os"
1818
"path/filepath"
19-
"runtime"
2019
rtrace "runtime/trace"
2120
"slices"
2221
"strings"
@@ -107,10 +106,19 @@ func main() {
107106
return
108107
}
109108

109+
if cfg.GOROOT == "" {
110+
fmt.Fprintf(os.Stderr, "go: cannot find GOROOT directory: 'go' binary is trimmed and GOROOT is not set\n")
111+
os.Exit(2)
112+
}
113+
if fi, err := os.Stat(cfg.GOROOT); err != nil || !fi.IsDir() {
114+
fmt.Fprintf(os.Stderr, "go: cannot find GOROOT directory: %v\n", cfg.GOROOT)
115+
os.Exit(2)
116+
}
117+
110118
// Diagnose common mistake: GOPATH==GOROOT.
111119
// This setting is equivalent to not setting GOPATH at all,
112120
// which is not what most people want when they do it.
113-
if gopath := cfg.BuildContext.GOPATH; filepath.Clean(gopath) == filepath.Clean(runtime.GOROOT()) {
121+
if gopath := cfg.BuildContext.GOPATH; filepath.Clean(gopath) == filepath.Clean(cfg.GOROOT) {
114122
fmt.Fprintf(os.Stderr, "warning: GOPATH set to GOROOT (%s) has no effect\n", gopath)
115123
} else {
116124
for _, p := range filepath.SplitList(gopath) {
@@ -139,15 +147,6 @@ func main() {
139147
}
140148
}
141149

142-
if cfg.GOROOT == "" {
143-
fmt.Fprintf(os.Stderr, "go: cannot find GOROOT directory: 'go' binary is trimmed and GOROOT is not set\n")
144-
os.Exit(2)
145-
}
146-
if fi, err := os.Stat(cfg.GOROOT); err != nil || !fi.IsDir() {
147-
fmt.Fprintf(os.Stderr, "go: cannot find GOROOT directory: %v\n", cfg.GOROOT)
148-
os.Exit(2)
149-
}
150-
151150
cmd, used := lookupCmd(args)
152151
cfg.CmdName = strings.Join(args[:used], " ")
153152
if len(cmd.Commands) > 0 {

src/cmd/go/testdata/script/goroot_executable_trimpath.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,20 @@ wait
2929
env TESTGOROOT=$GOROOT
3030
env GOROOT=
3131

32+
# Unset GOPATH and any variables that its default may be derived from,
33+
# so that we can check for a spurious warning.
34+
env GOPATH=
35+
env HOME=''
36+
env USERPROFILE=''
37+
env home=''
38+
3239
# Relocated Executable
3340
# Since we built with -trimpath and the binary isn't installed in a
3441
# normal-looking GOROOT, this command should fail.
3542

3643
! exec $WORK/new/bin/go$GOEXE env GOROOT
3744
stderr '^go: cannot find GOROOT directory: ''go'' binary is trimmed and GOROOT is not set$'
45+
! stderr 'GOPATH set to GOROOT'
3846

3947
# Cross-compiled binaries in cmd are installed to a ${GOOS}_${GOARCH} subdirectory,
4048
# so we also want to try a copy there.
@@ -44,13 +52,15 @@ stderr '^go: cannot find GOROOT directory: ''go'' binary is trimmed and GOROOT i
4452
cp $WORK/new/bin/go$GOEXE $WORK/new/bin/${GOOS}_${GOARCH}/go$GOEXE
4553
! exec $WORK/new/bin/${GOOS}_${GOARCH}/go$GOEXE env GOROOT
4654
stderr '^go: cannot find GOROOT directory: ''go'' binary is trimmed and GOROOT is not set$'
55+
! stderr 'GOPATH set to GOROOT'
4756

4857
# Relocated Tree:
4958
# If the binary is sitting in a bin dir next to ../pkg/tool, that counts as a GOROOT,
5059
# so it should find the new tree.
5160
mkdir $WORK/new/pkg/tool
5261
exec $WORK/bin/check$GOEXE $WORK/new/bin/go$GOEXE $WORK/new
5362
exec $WORK/bin/check$GOEXE $WORK/new/bin/${GOOS}_${GOARCH}/go$GOEXE $WORK/new
63+
! stderr 'GOPATH set to GOROOT'
5464

5565
-- check.go --
5666
package main

0 commit comments

Comments
 (0)