]> Cypherpunks.ru repositories - gostls13.git/commitdiff
misc/cgo: invoke "go" from $GOROOT/bin instead of $PATH
authorBryan C. Mills <bcmills@google.com>
Thu, 19 May 2022 14:26:53 +0000 (10:26 -0400)
committerGopher Robot <gobot@golang.org>
Thu, 19 May 2022 15:04:11 +0000 (15:04 +0000)
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>

misc/cgo/testplugin/plugin_test.go
misc/cgo/testshared/shared_test.go

index 74fb866f6f9f7c0041c22ed4974afc13ad99b699..d373642e45977c3026712e414fcb46e1e9c0fdbe 100644 (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.
index 616630979c1bd2af0574078dfb3160328152e133..024f084da55195945414c84d4e933976fdcd666f 100644 (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",