Skip to content

Commit f5925b0

Browse files
authored
Merge pull request #14928 from fjl/build-goroot-explain
internal/build: add GoTool and document why it uses GOROOT
2 parents 67439c1 + f59a49d commit f5925b0

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

‎build/ci.go‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,7 @@ func goTool(subcmd string, args ...string) *exec.Cmd {
250250
}
251251

252252
func goToolArch(arch string, subcmd string, args ...string) *exec.Cmd {
253-
gocmd := filepath.Join(runtime.GOROOT(), "bin", "go")
254-
cmd := exec.Command(gocmd, subcmd)
255-
cmd.Args = append(cmd.Args, args...)
256-
253+
cmd := build.GoTool(subcmd, args...)
257254
if subcmd == "build" || subcmd == "install" || subcmd == "test" {
258255
// Go CGO has a Windows linker error prior to 1.8 (https://github.com/golang/go/issues/8756).
259256
// Work around issue by allowing multiple definitions for <1.8 builds.

‎internal/build/util.go‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@ func CopyFile(dst, src string, mode os.FileMode) {
138138
}
139139
}
140140

141+
// GoTool returns the command that runs a go tool. This uses go from GOROOT instead of PATH
142+
// so that go commands executed by build use the same version of Go as the 'host' that runs
143+
// build code. e.g.
144+
//
145+
// /usr/lib/go-1.8/bin/go run build/ci.go ...
146+
//
147+
// runs using go 1.8 and invokes go 1.8 tools from the same GOROOT. This is also important
148+
// because runtime.Version checks on the host should match the tools that are run.
149+
func GoTool(tool string, args ...string) *exec.Cmd {
150+
args = append([]string{tool}, args...)
151+
return exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), args...)
152+
}
153+
141154
// ExpandPackagesNoVendor expands a cmd/go import path pattern, skipping
142155
// vendored packages.
143156
func ExpandPackagesNoVendor(patterns []string) []string {
@@ -148,8 +161,7 @@ func ExpandPackagesNoVendor(patterns []string) []string {
148161
}
149162
}
150163
if expand {
151-
args := append([]string{"list"}, patterns...)
152-
cmd := exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), args...)
164+
cmd := GoTool("list", patterns...)
153165
out, err := cmd.CombinedOutput()
154166
if err != nil {
155167
log.Fatalf("package listing failed: %v\n%s", err, string(out))

0 commit comments

Comments
 (0)