Skip to content

Commit d68615f

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/test: ensure that build.ToolDir is accurate in tests
This fixes a build failure due to inability to locate the "vet" tool when the test binary is built with -trimpath. Updates #51461 Change-Id: I81838cc8842e4ff7900cab81af60501ebba86ff1 Reviewed-on: https://go-review.googlesource.com/c/go/+/391808 Trust: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 6378c0e commit d68615f

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

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

+33-17
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,6 @@ func defaultContext() build.Context {
8787

8888
ctxt.JoinPath = filepath.Join // back door to say "do not use go command"
8989

90-
ctxt.GOROOT = findGOROOT()
91-
if runtime.Compiler != "gccgo" {
92-
// Note that we must use runtime.GOOS and runtime.GOARCH here,
93-
// as the tool directory does not move based on environment
94-
// variables. This matches the initialization of ToolDir in
95-
// go/build, except for using ctxt.GOROOT rather than
96-
// runtime.GOROOT.
97-
build.ToolDir = filepath.Join(ctxt.GOROOT, "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)
98-
}
99-
10090
// Override defaults computed in go/build with defaults
10191
// from go environment configuration file, if known.
10292
ctxt.GOPATH = envOr("GOPATH", gopath(ctxt))
@@ -146,10 +136,36 @@ func defaultContext() build.Context {
146136
}
147137

148138
func init() {
139+
SetGOROOT(findGOROOT())
149140
BuildToolchainCompiler = func() string { return "missing-compiler" }
150141
BuildToolchainLinker = func() string { return "missing-linker" }
151142
}
152143

144+
func SetGOROOT(goroot string) {
145+
BuildContext.GOROOT = goroot
146+
147+
GOROOT = goroot
148+
if goroot == "" {
149+
GOROOTbin = ""
150+
GOROOTpkg = ""
151+
GOROOTsrc = ""
152+
} else {
153+
GOROOTbin = filepath.Join(goroot, "bin")
154+
GOROOTpkg = filepath.Join(goroot, "pkg")
155+
GOROOTsrc = filepath.Join(goroot, "src")
156+
}
157+
GOROOT_FINAL = findGOROOT_FINAL(goroot)
158+
159+
if runtime.Compiler != "gccgo" && goroot != "" {
160+
// Note that we must use runtime.GOOS and runtime.GOARCH here,
161+
// as the tool directory does not move based on environment
162+
// variables. This matches the initialization of ToolDir in
163+
// go/build, except for using BuildContext.GOROOT rather than
164+
// runtime.GOROOT.
165+
build.ToolDir = filepath.Join(goroot, "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)
166+
}
167+
}
168+
153169
// Experiment configuration.
154170
var (
155171
// RawGOEXPERIMENT is the GOEXPERIMENT value set by the user.
@@ -279,12 +295,12 @@ func CanGetenv(key string) bool {
279295
}
280296

281297
var (
282-
GOROOT = BuildContext.GOROOT
298+
GOROOT string
299+
GOROOTbin string
300+
GOROOTpkg string
301+
GOROOTsrc string
302+
GOROOT_FINAL string
283303
GOBIN = Getenv("GOBIN")
284-
GOROOTbin = filepath.Join(GOROOT, "bin")
285-
GOROOTpkg = filepath.Join(GOROOT, "pkg")
286-
GOROOTsrc = filepath.Join(GOROOT, "src")
287-
GOROOT_FINAL = findGOROOT_FINAL()
288304
GOMODCACHE = envOr("GOMODCACHE", gopathDir("pkg/mod"))
289305

290306
// Used in envcmd.MkEnv and build ID computations.
@@ -386,10 +402,10 @@ func findGOROOT() string {
386402
return def
387403
}
388404

389-
func findGOROOT_FINAL() string {
405+
func findGOROOT_FINAL(goroot string) string {
390406
// $GOROOT_FINAL is only for use during make.bash
391407
// so it is not settable using go/env, so we use os.Getenv here.
392-
def := GOROOT
408+
def := goroot
393409
if env := os.Getenv("GOROOT_FINAL"); env != "" {
394410
def = filepath.Clean(env)
395411
}

src/cmd/go/internal/test/flagdefs_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
package test
66

77
import (
8+
"cmd/go/internal/cfg"
89
"cmd/go/internal/test/internal/genflags"
910
"flag"
11+
"internal/testenv"
1012
"reflect"
1113
"strings"
1214
"testing"
1315
)
1416

17+
func TestMain(m *testing.M) {
18+
cfg.SetGOROOT(testenv.GOROOT(nil))
19+
}
20+
1521
func TestPassFlagToTestIncludesAllTestFlags(t *testing.T) {
1622
flag.VisitAll(func(f *flag.Flag) {
1723
if !strings.HasPrefix(f.Name, "test.") {

0 commit comments

Comments
 (0)