Skip to content

Commit 8ef1273

Browse files
authored
Revert "gotooltest: proxy the new GOMODCACHE env var too" (#136)
We started making gotooltest share the host's module download cache with test scripts, since we did it with GOCACHE and it initially made sense. However, the upside is significantly smaller for GOMODCACHE compared to GOCACHE. The build cache can save a significant amount of time, since many tools have to load or build Go packages as part of their tests. In contrast, few tests download modules, and those which do tend to download those modules from a local proxy like goproxytest, which is very fast already. The downsides of sharing the module download cache are a few: * We don't share GOPATH, and since the default GOMODCACHE is GOPATH/pkg/mod, sharing one and not the other is unexpected and inconsistent. * Upstream testscript shares GOCACHE, but does not share GOMODCACHE. See golang/go#42017. * Losing a degree of isolation in the tests is a downside in itself, especially given that sharing GOMODCACHE isn't crucial. This reverts commit c5fd45a. Note that we keep the env.txt test in place, just flipping the expectation that GOMODCACHE does not contain ${WORK}.
1 parent 50426be commit 8ef1273

File tree

4 files changed

+9
-24
lines changed

4 files changed

+9
-24
lines changed

cmd/testscript/testdata/simple.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# With .gomodproxy supporting files, any GOPROXY from the
22
# environment should be overridden by the test proxy.
3-
# Note that we want an empty GOMODCACHE, to ensure we have to download modules
4-
# from our proxy.
53
env GOPROXY=0.1.2.3
64

75
unquote file.txt
@@ -10,7 +8,6 @@ stdout 'example.com/mod'
108
! stderr .+
119

1210
-- file.txt --
13-
>env GOMODCACHE=$WORK
1411
>go get -d fruit.com
1512
>go list -m
1613
>stdout 'example.com/mod'

cmd/txtar-addmod/addmod.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package main
2020

2121
import (
2222
"bytes"
23-
"encoding/json"
2423
"flag"
2524
"fmt"
2625
"io/ioutil"
@@ -101,17 +100,9 @@ func main1() int {
101100
return string(out)
102101
}
103102

104-
var goEnv struct {
105-
GOPATH string
106-
GOMODCACHE string
107-
}
108-
if err := json.Unmarshal([]byte(run("go", "env", "-json", "GOPATH", "GOMODCACHE")), &goEnv); err != nil {
109-
fatalf("cannot unmarshal 'go env -json': %v", err)
110-
}
111-
modCache := goEnv.GOMODCACHE
112-
if modCache == "" {
113-
// For Go 1.14 and older.
114-
modCache = filepath.Join(goEnv.GOPATH, "pkg", "mod")
103+
gopath := strings.TrimSpace(run("go", "env", "GOPATH"))
104+
if gopath == "" {
105+
fatalf("cannot find GOPATH")
115106
}
116107

117108
exitCode := 0
@@ -140,13 +131,13 @@ func main1() int {
140131
}
141132
path = encpath
142133

143-
mod, err := ioutil.ReadFile(filepath.Join(modCache, "cache", "download", path, "@v", vers+".mod"))
134+
mod, err := ioutil.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".mod"))
144135
if err != nil {
145136
log.Printf("%s: %v", arg, err)
146137
exitCode = 1
147138
continue
148139
}
149-
info, err := ioutil.ReadFile(filepath.Join(modCache, "cache", "download", path, "@v", vers+".info"))
140+
info, err := ioutil.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".info"))
150141
if err != nil {
151142
log.Printf("%s: %v", arg, err)
152143
exitCode = 1

gotooltest/setup.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ var (
2727
goEnv struct {
2828
GOROOT string
2929
GOCACHE string
30-
GOMODCACHE string
3130
GOPROXY string
3231
goversion string
3332
releaseTags []string
@@ -60,14 +59,13 @@ func initGoEnv() error {
6059
eout, stderr, err := run("go", "env", "-json",
6160
"GOROOT",
6261
"GOCACHE",
63-
"GOMODCACHE",
6462
"GOPROXY",
6563
)
6664
if err != nil {
6765
return fmt.Errorf("failed to determine environment from go command: %v\n%v", err, stderr)
6866
}
6967
if err := json.Unmarshal(eout.Bytes(), &goEnv); err != nil {
70-
return fmt.Errorf("failed to unmarshal 'go env -json' output: %v\n%v", err, eout)
68+
return fmt.Errorf("failed to unmarshal GOROOT and GOCACHE tags from go command out: %v\n%v", err, eout)
7169
}
7270

7371
version := goEnv.releaseTags[len(goEnv.releaseTags)-1]
@@ -140,7 +138,6 @@ func goEnviron(env0 []string) []string {
140138
"GOOS=" + runtime.GOOS,
141139
"GOROOT=" + goEnv.GOROOT,
142140
"GOCACHE=" + goEnv.GOCACHE,
143-
"GOMODCACHE=" + goEnv.GOMODCACHE,
144141
"GOPROXY=" + goEnv.GOPROXY,
145142
"goversion=" + goEnv.goversion,
146143
}...)

gotooltest/testdata/env.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# GOPATH should point inside the temporary work directory, but GOCACHE and
2-
# GOMODCACHE should not, as they should reuse the host's.
1+
# GOPATH and GOMODCACHE are not shared with the host,
2+
# but GOCACHE is.
33
go env
44
stdout GOPATH=.*${WORK@R}
5+
stdout GOMODCACHE=.*${WORK@R}
56
! stdout GOCACHE=.*${WORK@R}
6-
! stdout GOMODCACHE=.*${WORK@R}

0 commit comments

Comments
 (0)