Skip to content

Commit

Permalink
misc/cgo: invoke "go" from $GOROOT/bin instead of $PATH
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Heschi Kreinick <[email protected]>
Auto-Submit: Bryan Mills <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
Bryan C. Mills authored and gopherbot committed May 19, 2022
1 parent b93ceef commit 9bc544a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
9 changes: 8 additions & 1 deletion misc/cgo/testplugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
)

var gcflags string = os.Getenv("GO_GCFLAGS")
var goroot string

func TestMain(m *testing.M) {
flag.Parse()
Expand All @@ -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.

Expand Down Expand Up @@ -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.
Expand Down
14 changes: 8 additions & 6 deletions misc/cgo/testshared/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 9bc544a

Please sign in to comment.