From feada5366134772c4c6462e4a12150410741a00f Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Tue, 12 Jul 2022 13:43:02 -0400 Subject: [PATCH] misc/cgo/testcshared: don't rely on an erroneous install target in tests Non-main packages in module mode should not be installed to GOPATH/pkg, but due to #37015 they were installed there anyway. This change switches the 'go install' command in createHeaders to instead use 'go build' (with an extension determined by the install target for 'runtime/cgo', which is well-defined at least for the moment), and switches TestCachedInstall (which appears to be explicitly testing 'go install') to explicitly request GOPATH mode (which provides a well-defined install target for the library). This change follows a similar structure to CL 416954. For #37015. Change-Id: I22ae4af0f0d4c50adc9e0f0dc279859d1f258cc8 Reviewed-on: https://go-review.googlesource.com/c/go/+/417096 Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot Auto-Submit: Bryan Mills Reviewed-by: Ian Lance Taylor --- misc/cgo/testcshared/cshared_test.go | 35 ++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/misc/cgo/testcshared/cshared_test.go b/misc/cgo/testcshared/cshared_test.go index e4898778be..7fbcff24dd 100644 --- a/misc/cgo/testcshared/cshared_test.go +++ b/misc/cgo/testcshared/cshared_test.go @@ -151,16 +151,22 @@ func testMain(m *testing.M) int { // The installation directory format varies depending on the platform. output, err := exec.Command("go", "list", "-buildmode=c-shared", - "-installsuffix", "testcshared", "-f", "{{.Target}}", - "./libgo").CombinedOutput() + "runtime/cgo").CombinedOutput() if err != nil { log.Panicf("go list failed: %v\n%s", err, output) } - target := string(bytes.TrimSpace(output)) - libgoname = filepath.Base(target) - installdir = filepath.Dir(target) - libSuffix = strings.TrimPrefix(filepath.Ext(target), ".") + runtimeCgoTarget := string(bytes.TrimSpace(output)) + libSuffix = strings.TrimPrefix(filepath.Ext(runtimeCgoTarget), ".") + + defer func() { + if installdir != "" { + err := os.RemoveAll(installdir) + if err != nil { + log.Panic(err) + } + } + }() return m.Run() } @@ -284,8 +290,13 @@ func createHeaders() error { } // Generate a C header file for libgo itself. - args = []string{"go", "install", "-buildmode=c-shared", - "-installsuffix", "testcshared", "./libgo"} + installdir, err = os.MkdirTemp("", "testcshared") + if err != nil { + return err + } + libgoname = "libgo." + libSuffix + + args = []string{"go", "build", "-buildmode=c-shared", "-o", filepath.Join(installdir, libgoname), "./libgo"} cmd = exec.Command(args[0], args[1:]...) out, err = cmd.CombinedOutput() if err != nil { @@ -373,6 +384,7 @@ func createHeadersOnce(t *testing.T) { headersErr = createHeaders() }) if headersErr != nil { + t.Helper() t.Fatal(headersErr) } } @@ -705,12 +717,15 @@ func TestCachedInstall(t *testing.T) { copyFile(t, filepath.Join(tmpdir, "src", "testcshared", "libgo", "libgo.go"), filepath.Join("libgo", "libgo.go")) copyFile(t, filepath.Join(tmpdir, "src", "testcshared", "p", "p.go"), filepath.Join("p", "p.go")) - env := append(os.Environ(), "GOPATH="+tmpdir, "GOBIN="+filepath.Join(tmpdir, "bin")) - buildcmd := []string{"go", "install", "-x", "-buildmode=c-shared", "-installsuffix", "testcshared", "./libgo"} cmd := exec.Command(buildcmd[0], buildcmd[1:]...) cmd.Dir = filepath.Join(tmpdir, "src", "testcshared") + env := append(cmd.Environ(), + "GOPATH="+tmpdir, + "GOBIN="+filepath.Join(tmpdir, "bin"), + "GO111MODULE=off", // 'go install' only works in GOPATH mode + ) cmd.Env = env t.Log(buildcmd) out, err := cmd.CombinedOutput() -- 2.48.1