Skip to content

Commit cc2e7f3

Browse files
author
Bryan C. Mills
committed
cmd/go: diagnose unset GOROOT when built with -trimpath
For #51483 Change-Id: I4546c20cf968b595020a1eba888fe1d9a1c6cfc5 Reviewed-on: https://go-review.googlesource.com/c/go/+/391811 Reviewed-by: Russ Cox <[email protected]> Trust: Bryan Mills <[email protected]>
1 parent e1fbf13 commit cc2e7f3

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,10 @@ func findGOROOT() string {
370370
if env := Getenv("GOROOT"); env != "" {
371371
return filepath.Clean(env)
372372
}
373-
def := filepath.Clean(runtime.GOROOT())
373+
def := ""
374+
if r := runtime.GOROOT(); r != "" {
375+
def = filepath.Clean(r)
376+
}
374377
if runtime.Compiler == "gccgo" {
375378
// gccgo has no real GOROOT, and it certainly doesn't
376379
// depend on the executable's location.

src/cmd/go/main.go

+4
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ func main() {
142142
}
143143
}
144144

145+
if cfg.GOROOT == "" {
146+
fmt.Fprintf(os.Stderr, "go: cannot find GOROOT directory: 'go' binary is trimmed and GOROOT is not set\n")
147+
os.Exit(2)
148+
}
145149
if fi, err := os.Stat(cfg.GOROOT); err != nil || !fi.IsDir() {
146150
fmt.Fprintf(os.Stderr, "go: cannot find GOROOT directory: %v\n", cfg.GOROOT)
147151
os.Exit(2)

src/cmd/go/script_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"path/filepath"
2323
"regexp"
2424
"runtime"
25+
"runtime/debug"
2526
"strconv"
2627
"strings"
2728
"sync"
@@ -373,6 +374,17 @@ Script:
373374
ok = testenv.HasSymlink()
374375
case "case-sensitive":
375376
ok = isCaseSensitive(ts.t)
377+
case "trimpath":
378+
if info, _ := debug.ReadBuildInfo(); info == nil {
379+
ts.fatalf("missing build info")
380+
} else {
381+
for _, s := range info.Settings {
382+
if s.Key == "-trimpath" && s.Value == "true" {
383+
ok = true
384+
break
385+
}
386+
}
387+
}
376388
default:
377389
if strings.HasPrefix(cond.tag, "exec:") {
378390
prog := cond.tag[len("exec:"):]

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
# TODO(#51483): when runtime.GOROOT() returns the empty string,
99
# go/build should default to 'go env GOROOT' instead.
1010

11-
[short] skip
12-
1311
env GOROOT=
1412
env GOROOT_FINAL=
1513

14+
[trimpath] ! go env GOROOT
15+
[trimpath] stderr '^go: cannot find GOROOT directory: ''go'' binary is trimmed and GOROOT is not set$'
16+
[trimpath] stop
17+
18+
19+
[short] skip
20+
1621
go run .
1722
stdout '^GOROOT '$TESTGO_GOROOT'$'
1823
stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'

0 commit comments

Comments
 (0)