From 009359f0a81056fd155119a185c78dc37800f828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Fri, 22 Apr 2022 14:38:43 +0100 Subject: [PATCH] gotooltest: move default GOPATH out of the main module Otherwise, commands like `go mod tidy` will try to walk into the default GOPATH directory. At best, that's a waste of resources, since directories like GOMODPATH may contain lots of files. At worst, it results in errors, as directories in GOMODPATH are not valid import paths: --- FAIL: TestScripts/list (0.01s) testscript.go:413: [...] > go mod tidy [stderr] mod/gopath/pkg/mod/fruit.com@v1.1.0/fruit: import path "mod/gopath/pkg/mod/fruit.com@v1.1.0/fruit" should not have @version We add the test in goproxytest, as goproxytest already uses gotooltest, and we need an external module to reproduce the error. --- cmd/testscript/testdata/env_var_with_go.txt | 10 +++++----- goproxytest/testdata/list.txt | 7 +++++++ gotooltest/setup.go | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/cmd/testscript/testdata/env_var_with_go.txt b/cmd/testscript/testdata/env_var_with_go.txt index fe596e08..cf445cac 100644 --- a/cmd/testscript/testdata/env_var_with_go.txt +++ b/cmd/testscript/testdata/env_var_with_go.txt @@ -12,7 +12,7 @@ unquote withproxy.txt # Baseline testscript -v noproxy.txt stdout ^BANANA=$ -stdout '^GOPATH=\$WORK[/\\]gopath'$ +stdout '^GOPATH=\$WORK[/\\]\.gopath'$ [!go1.13] stdout ^GOPROXY=$ [go1.13] stdout ^GOPROXY=https://proxy.golang.org,direct$ ! stderr .+ @@ -25,7 +25,7 @@ env GOPROXY= # no GOPROXY, no pass-through, no proxy testscript -v noproxy.txt stdout ^BANANA=$ -stdout '^GOPATH=\$WORK[/\\]gopath'$ +stdout '^GOPATH=\$WORK[/\\]\.gopath'$ [!go1.13] stdout ^GOPROXY=$ [go1.13] stdout ^GOPROXY=https://proxy.golang.org,direct$ ! stderr .+ @@ -33,7 +33,7 @@ stdout '^GOPATH=\$WORK[/\\]gopath'$ # no GOPROXY, no pass-through, with proxy testscript -v withproxy.txt stdout ^BANANA=$ -stdout '^GOPATH=\$WORK[/\\]gopath'$ +stdout '^GOPATH=\$WORK[/\\]\.gopath'$ stdout ^GOPROXY=http://.*/mod$ ! stderr .+ @@ -56,14 +56,14 @@ setfilegoproxy $WORK/proxy # with GOPROXY, no pass-through, no proxy testscript -v noproxy.txt stdout ^BANANA=$ -stdout '^GOPATH=\$WORK[/\\]gopath'$ +stdout '^GOPATH=\$WORK[/\\]\.gopath'$ stdout ^GOPROXY=$GOPROXY$ ! stderr .+ # with GOPROXY, no pass-through, with proxy testscript -v withproxy.txt stdout ^BANANA=$ -stdout '^GOPATH=\$WORK[/\\]gopath'$ +stdout '^GOPATH=\$WORK[/\\]\.gopath'$ stdout ^GOPROXY=http://.*/mod$ ! stderr .+ diff --git a/goproxytest/testdata/list.txt b/goproxytest/testdata/list.txt index c293604b..8b05a178 100644 --- a/goproxytest/testdata/list.txt +++ b/goproxytest/testdata/list.txt @@ -7,6 +7,13 @@ stdout 'v1.0.0 v1.1.0' go get -d fruit.com@v1.0.0 go get -d fruit.com@v1.1.0 +# On Go 1.18 and later, this will error out if GOPATH is ${WORK}/gopath, +# as that places GOMODCACHE inside the Go module and "mod tidy" walks it. +# "mod tidy" then complains about invalid import paths such as +# "mod/gopath/pkg/mod/fruit.com@v1.1.0/fruit". +# It's for that reason that we moved the default GOPATH to ${WORK}/.gopath. +go mod tidy + -- go.mod -- module mod diff --git a/gotooltest/setup.go b/gotooltest/setup.go index b1092c92..7e56df6e 100644 --- a/gotooltest/setup.go +++ b/gotooltest/setup.go @@ -110,7 +110,7 @@ func goEnviron(env0 []string) []string { env := environ(env0) workdir := env.get("WORK") return append(env, []string{ - "GOPATH=" + filepath.Join(workdir, "gopath"), + "GOPATH=" + filepath.Join(workdir, ".gopath"), "CCACHE_DISABLE=1", // ccache breaks with non-existent HOME "GOARCH=" + runtime.GOARCH, "GOOS=" + runtime.GOOS,