misc/cgo: invoke "go" from $GOROOT/bin instead of $PATH

If PATH doesn't contain GOROOT/bin as the first element, this could
otherwise end up running entirely the wrong command (and from the
wrong GOROOT, even).

I pre-tested this change on release-branch.go1.17 using a gomote.
I believe that it will fix the test failure on that branch,
but will need to be backported.

For #52995.

Change-Id: Ib0c43289a1e0ccf9409f0f0ef8046501a955ce65
Reviewed-on: https://go-review.googlesource.com/c/go/+/407294
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Bryan C. Mills 2022-05-19 10:26:53 -04:00 committed by Gopher Robot
parent b93ceefa7b
commit 9bc544a158
2 changed files with 16 additions and 7 deletions

View File

@ -19,6 +19,7 @@ import (
)
var gcflags string = os.Getenv("GO_GCFLAGS")
var goroot string
func TestMain(m *testing.M) {
flag.Parse()
@ -43,6 +44,12 @@ func prettyPrintf(format string, args ...interface{}) {
}
func testMain(m *testing.M) int {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
goroot = filepath.Join(cwd, "../../..")
// Copy testdata into GOPATH/src/testplugin, along with a go.mod file
// declaring the same path.
@ -113,7 +120,7 @@ func goCmd(t *testing.T, op string, args ...string) {
if t != nil {
t.Helper()
}
run(t, "go", append([]string{op, "-gcflags", gcflags}, args...)...)
run(t, filepath.Join(goroot, "bin", "go"), append([]string{op, "-gcflags", gcflags}, args...)...)
}
// escape converts a string to something suitable for a shell command line.

View File

@ -27,6 +27,7 @@ import (
)
var gopathInstallDir, gorootInstallDir string
var oldGOROOT string
// This is the smallest set of packages we can link into a shared
// library (runtime/cgo is built implicitly).
@ -60,7 +61,7 @@ func goCmd(t *testing.T, args ...string) string {
newargs = append(newargs, "-x", "-ldflags=-v")
}
newargs = append(newargs, args[1:]...)
c := exec.Command("go", newargs...)
c := exec.Command(filepath.Join(oldGOROOT, "bin", "go"), newargs...)
stderr := new(strings.Builder)
c.Stderr = stderr
@ -90,6 +91,12 @@ func goCmd(t *testing.T, args ...string) string {
// TestMain calls testMain so that the latter can use defer (TestMain exits with os.Exit).
func testMain(m *testing.M) (int, error) {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
oldGOROOT = filepath.Join(cwd, "../../..")
workDir, err := os.MkdirTemp("", "shared_test")
if err != nil {
return 0, err
@ -187,11 +194,6 @@ func cloneTestdataModule(gopath string) (string, error) {
// GOROOT/pkg relevant to this test into the given directory.
// It must be run from within the testdata module.
func cloneGOROOTDeps(goroot string) error {
oldGOROOT := strings.TrimSpace(goCmd(nil, "env", "GOROOT"))
if oldGOROOT == "" {
return fmt.Errorf("go env GOROOT returned an empty string")
}
// Before we clone GOROOT, figure out which packages we need to copy over.
listArgs := []string{
"list",