From 9bc544a1580a67166060aabc5af91227092f6a39 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Thu, 19 May 2022 10:26:53 -0400 Subject: [PATCH] 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 Reviewed-by: Heschi Kreinick Auto-Submit: Bryan Mills TryBot-Result: Gopher Robot --- misc/cgo/testplugin/plugin_test.go | 9 ++++++++- misc/cgo/testshared/shared_test.go | 14 ++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/misc/cgo/testplugin/plugin_test.go b/misc/cgo/testplugin/plugin_test.go index 74fb866f6f..d373642e45 100644 --- a/misc/cgo/testplugin/plugin_test.go +++ b/misc/cgo/testplugin/plugin_test.go @@ -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. diff --git a/misc/cgo/testshared/shared_test.go b/misc/cgo/testshared/shared_test.go index 616630979c..024f084da5 100644 --- a/misc/cgo/testshared/shared_test.go +++ b/misc/cgo/testshared/shared_test.go @@ -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", -- 2.50.0