From 58631ba54f45506f2f178bb01d22273e7dfba674 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Wed, 9 Mar 2022 17:19:23 -0500 Subject: internal/testenv: add GOROOT and use it to fix tests broken with -trimpath This fixes many (but not all) of the tests that currently fail (due to a bogus path reported by runtime.GOROOT) when run with 'go test -trimpath std cmd'. Updates #51461 Change-Id: Ia2cc05705529c4859e7928f32eeceed647f2e986 Reviewed-on: https://go-review.googlesource.com/c/go/+/391806 Trust: Bryan Mills Run-TryBot: Bryan Mills Reviewed-by: Russ Cox TryBot-Result: Gopher Robot --- src/cmd/api/goapi.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/cmd/api/goapi.go') diff --git a/src/cmd/api/goapi.go b/src/cmd/api/goapi.go index 2a0e109575..b2a023a9b7 100644 --- a/src/cmd/api/goapi.go +++ b/src/cmd/api/goapi.go @@ -34,9 +34,11 @@ func goCmd() string { if runtime.GOOS == "windows" { exeSuffix = ".exe" } - path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix) - if _, err := os.Stat(path); err == nil { - return path + if goroot := build.Default.GOROOT; goroot != "" { + path := filepath.Join(goroot, "bin", "go"+exeSuffix) + if _, err := os.Stat(path); err == nil { + return path + } } return "go" } @@ -127,6 +129,10 @@ var internalPkg = regexp.MustCompile(`(^|/)internal($|/)`) func main() { flag.Parse() + if build.Default.GOROOT == "" { + log.Fatalf("GOROOT not found. (If binary was built with -trimpath, $GOROOT must be set.)") + } + if !strings.Contains(runtime.Version(), "weekly") && !strings.Contains(runtime.Version(), "devel") { if *nextFiles != "" { fmt.Printf("Go version is %q, ignoring -next %s\n", runtime.Version(), *nextFiles) -- cgit v1.3-5-g9baa