Skip to content

Commit df38614

Browse files
committed
test: use go tool from tree, not path
Some of our tests do exec.Command("go", "tool", "compile", ...) or similar. That "go" is selected from PATH. When run.go is started from the command line (but not from all.bash), the first "go" is whatever happens to be first in the user's path (some random older version than tip). We really want all these tests to use the "go" tool from the source tree under test. Add GOROOT/bin to the front of the path to ensure that the tools we use come from the source tree under test. Change-Id: I609261a4add8cd5cb228316752d52b5499aec963 Reviewed-on: https://go-review.googlesource.com/c/go/+/418474 Run-TryBot: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent bb1749b commit df38614

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

test/run.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type envVars struct {
5858
}
5959

6060
var env = func() (res envVars) {
61-
cmd := exec.Command("go", "env", "-json")
61+
cmd := exec.Command(goTool(), "env", "-json")
6262
stdout, err := cmd.StdoutPipe()
6363
if err != nil {
6464
log.Fatal("StdoutPipe:", err)
@@ -710,6 +710,22 @@ func (t *test) run() {
710710
if tempDirIsGOPATH {
711711
cmd.Env = append(cmd.Env, "GOPATH="+t.tempDir)
712712
}
713+
// Put the bin directory of the GOROOT that built this program
714+
// first in the path. This ensures that tests that use the "go"
715+
// tool use the same one that built this program. This ensures
716+
// that if you do "../bin/go run run.go" in this directory, all
717+
// the tests that start subprocesses that "go tool compile" or
718+
// whatever, use ../bin/go as their go tool, not whatever happens
719+
// to be first in the user's path.
720+
path := os.Getenv("PATH")
721+
newdir := filepath.Join(runtime.GOROOT(), "bin")
722+
if path != "" {
723+
path = newdir + string(filepath.ListSeparator) + path
724+
} else {
725+
path = newdir
726+
}
727+
cmd.Env = append(cmd.Env, "PATH="+path)
728+
713729
cmd.Env = append(cmd.Env, runenv...)
714730

715731
var err error

0 commit comments

Comments
 (0)