]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: diagnose unset GOROOT when built with -trimpath
authorBryan C. Mills <bcmills@google.com>
Fri, 11 Mar 2022 04:40:10 +0000 (23:40 -0500)
committerBryan Mills <bcmills@google.com>
Fri, 18 Mar 2022 22:42:43 +0000 (22:42 +0000)
For #51483

Change-Id: I4546c20cf968b595020a1eba888fe1d9a1c6cfc5
Reviewed-on: https://go-review.googlesource.com/c/go/+/391811
Reviewed-by: Russ Cox <rsc@golang.org>
Trust: Bryan Mills <bcmills@google.com>

src/cmd/go/internal/cfg/cfg.go
src/cmd/go/main.go
src/cmd/go/script_test.go
src/cmd/go/testdata/script/build_trimpath_goroot.txt

index 61fd3ce4ef1afe5fd31dff376ad0359c53b2c95e..a11a1a7655537fe59632236a4fff5fe6a94aedb7 100644 (file)
@@ -370,7 +370,10 @@ func findGOROOT() string {
        if env := Getenv("GOROOT"); env != "" {
                return filepath.Clean(env)
        }
-       def := filepath.Clean(runtime.GOROOT())
+       def := ""
+       if r := runtime.GOROOT(); r != "" {
+               def = filepath.Clean(r)
+       }
        if runtime.Compiler == "gccgo" {
                // gccgo has no real GOROOT, and it certainly doesn't
                // depend on the executable's location.
index ed46ed822a601e0a335aea6c9fcff801142252fc..ee705e87e0513328ce7d3af2ce125357333ddcc0 100644 (file)
@@ -142,6 +142,10 @@ func main() {
                }
        }
 
+       if cfg.GOROOT == "" {
+               fmt.Fprintf(os.Stderr, "go: cannot find GOROOT directory: 'go' binary is trimmed and GOROOT is not set\n")
+               os.Exit(2)
+       }
        if fi, err := os.Stat(cfg.GOROOT); err != nil || !fi.IsDir() {
                fmt.Fprintf(os.Stderr, "go: cannot find GOROOT directory: %v\n", cfg.GOROOT)
                os.Exit(2)
index 90ab3a6501b248f7e790b4cae15f65ecdd1d2159..bffbe32220a9a062513bf40516b4c4dfefb10ba6 100644 (file)
@@ -22,6 +22,7 @@ import (
        "path/filepath"
        "regexp"
        "runtime"
+       "runtime/debug"
        "strconv"
        "strings"
        "sync"
@@ -373,6 +374,17 @@ Script:
                                ok = testenv.HasSymlink()
                        case "case-sensitive":
                                ok = isCaseSensitive(ts.t)
+                       case "trimpath":
+                               if info, _ := debug.ReadBuildInfo(); info == nil {
+                                       ts.fatalf("missing build info")
+                               } else {
+                                       for _, s := range info.Settings {
+                                               if s.Key == "-trimpath" && s.Value == "true" {
+                                                       ok = true
+                                                       break
+                                               }
+                                       }
+                               }
                        default:
                                if strings.HasPrefix(cond.tag, "exec:") {
                                        prog := cond.tag[len("exec:"):]
index c0ce1f0457f891aa8005ea2c807e689da5dc10e6..7b870ab739d38d124bd7c5f86866f73f6179198b 100644 (file)
@@ -8,11 +8,16 @@
 # TODO(#51483): when runtime.GOROOT() returns the empty string,
 # go/build should default to 'go env GOROOT' instead.
 
-[short] skip
-
 env GOROOT=
 env GOROOT_FINAL=
 
+[trimpath] ! go env GOROOT
+[trimpath] stderr '^go: cannot find GOROOT directory: ''go'' binary is trimmed and GOROOT is not set$'
+[trimpath] stop
+
+
+[short] skip
+
 go run .
 stdout '^GOROOT '$TESTGO_GOROOT'$'
 stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'