Skip to content

Commit 4b36588

Browse files
cuonglmJay Conrod
authored and
Jay Conrod
committed
cmd/go: add go command known variables to test cache hash
The go test result must not be cached when each of known variables to go command change. To do this, add all known variables to test metadata. Fixes #32285 Change-Id: I90be6a72f46c42d965aec4fed534c0623244cd3d Reviewed-on: https://go-review.googlesource.com/c/go/+/179040 Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 89d300b commit 4b36588

File tree

5 files changed

+88
-54
lines changed

5 files changed

+88
-54
lines changed

src/cmd/go/internal/cfg/cfg.go

+2-53
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"bytes"
1111
"fmt"
1212
"go/build"
13+
"internal/cfg"
1314
"io/ioutil"
1415
"os"
1516
"path/filepath"
@@ -221,61 +222,9 @@ func Getenv(key string) string {
221222

222223
// CanGetenv reports whether key is a valid go/env configuration key.
223224
func CanGetenv(key string) bool {
224-
return strings.Contains(knownEnv, "\t"+key+"\n")
225+
return strings.Contains(cfg.KnownEnv, "\t"+key+"\n")
225226
}
226227

227-
var knownEnv = `
228-
AR
229-
CC
230-
CGO_CFLAGS
231-
CGO_CFLAGS_ALLOW
232-
CGO_CFLAGS_DISALLOW
233-
CGO_CPPFLAGS
234-
CGO_CPPFLAGS_ALLOW
235-
CGO_CPPFLAGS_DISALLOW
236-
CGO_CXXFLAGS
237-
CGO_CXXFLAGS_ALLOW
238-
CGO_CXXFLAGS_DISALLOW
239-
CGO_ENABLED
240-
CGO_FFLAGS
241-
CGO_FFLAGS_ALLOW
242-
CGO_FFLAGS_DISALLOW
243-
CGO_LDFLAGS
244-
CGO_LDFLAGS_ALLOW
245-
CGO_LDFLAGS_DISALLOW
246-
CXX
247-
FC
248-
GCCGO
249-
GO111MODULE
250-
GO386
251-
GOARCH
252-
GOARM
253-
GOBIN
254-
GOCACHE
255-
GOENV
256-
GOEXE
257-
GOFLAGS
258-
GOGCCFLAGS
259-
GOHOSTARCH
260-
GOHOSTOS
261-
GOMIPS
262-
GOMIPS64
263-
GONOPROXY
264-
GONOSUMDB
265-
GOOS
266-
GOPATH
267-
GOPPC64
268-
GOPRIVATE
269-
GOPROXY
270-
GOROOT
271-
GOSUMDB
272-
GOTMPDIR
273-
GOTOOLDIR
274-
GOWASM
275-
GO_EXTLINK_ENABLED
276-
PKG_CONFIG
277-
`
278-
279228
var (
280229
GOROOT = BuildContext.GOROOT
281230
GOBIN = Getenv("GOBIN")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
env GO111MODULE=on
2+
go mod init foo
3+
go test
4+
stdout ^ok\s+foo
5+
env GO111MODULE=off
6+
go test
7+
stdout ^ok\s+
8+
! stdout ^ok\s+(cache)$
9+
10+
-- main_test.go --
11+
package main
12+
13+
import "testing"
14+
15+
func TestF(t *testing.T) {}

src/go/build/deps_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ var pkgDeps = map[string][]string{
166166
"syscall/js",
167167
},
168168

169+
"internal/cfg": {"L0"},
169170
"internal/poll": {"L0", "internal/oserror", "internal/race", "syscall", "time", "unicode/utf16", "unicode/utf8", "internal/syscall/windows"},
170171
"internal/testlog": {"L0"},
171172
"os": {"L1", "os", "syscall", "time", "internal/oserror", "internal/poll", "internal/syscall/windows", "internal/syscall/unix", "internal/testlog"},
@@ -199,7 +200,7 @@ var pkgDeps = map[string][]string{
199200
"testing": {"L2", "flag", "fmt", "internal/race", "os", "runtime/debug", "runtime/pprof", "runtime/trace", "time"},
200201
"testing/iotest": {"L2", "log"},
201202
"testing/quick": {"L2", "flag", "fmt", "reflect", "time"},
202-
"internal/testenv": {"L2", "OS", "flag", "testing", "syscall"},
203+
"internal/testenv": {"L2", "OS", "flag", "testing", "syscall", "internal/cfg"},
203204
"internal/lazyregexp": {"L2", "OS", "regexp"},
204205
"internal/lazytemplate": {"L2", "OS", "text/template"},
205206

src/internal/cfg/cfg.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package cfg holds configuration shared by the Go command and internal/testenv.
6+
// Definitions that don't need to be exposed outside of cmd/go should be in
7+
// cmd/go/internal/cfg instead of this package.
8+
package cfg
9+
10+
// KnownEnv is a list of environment variables that affect the operation
11+
// of the Go command.
12+
const KnownEnv = `
13+
AR
14+
CC
15+
CGO_CFLAGS
16+
CGO_CFLAGS_ALLOW
17+
CGO_CFLAGS_DISALLOW
18+
CGO_CPPFLAGS
19+
CGO_CPPFLAGS_ALLOW
20+
CGO_CPPFLAGS_DISALLOW
21+
CGO_CXXFLAGS
22+
CGO_CXXFLAGS_ALLOW
23+
CGO_CXXFLAGS_DISALLOW
24+
CGO_ENABLED
25+
CGO_FFLAGS
26+
CGO_FFLAGS_ALLOW
27+
CGO_FFLAGS_DISALLOW
28+
CGO_LDFLAGS
29+
CGO_LDFLAGS_ALLOW
30+
CGO_LDFLAGS_DISALLOW
31+
CXX
32+
FC
33+
GCCGO
34+
GO111MODULE
35+
GO386
36+
GOARCH
37+
GOARM
38+
GOBIN
39+
GOCACHE
40+
GOENV
41+
GOEXE
42+
GOFLAGS
43+
GOGCCFLAGS
44+
GOHOSTARCH
45+
GOHOSTOS
46+
GOMIPS
47+
GOMIPS64
48+
GONOPROXY
49+
GONOSUMDB
50+
GOOS
51+
GOPATH
52+
GOPPC64
53+
GOPRIVATE
54+
GOPROXY
55+
GOROOT
56+
GOSUMDB
57+
GOTMPDIR
58+
GOTOOLDIR
59+
GOWASM
60+
GO_EXTLINK_ENABLED
61+
PKG_CONFIG
62+
`

src/internal/testenv/testenv.go

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package testenv
1313
import (
1414
"errors"
1515
"flag"
16+
"internal/cfg"
1617
"os"
1718
"os/exec"
1819
"path/filepath"
@@ -88,6 +89,12 @@ func GoToolPath(t testing.TB) string {
8889
if err != nil {
8990
t.Fatal(err)
9091
}
92+
// Add all environment variables that affect the Go command to test metadata.
93+
// Cached test results will be invalidate when these variables change.
94+
// See golang.org/issue/32285.
95+
for _, envVar := range strings.Fields(cfg.KnownEnv) {
96+
os.Getenv(envVar)
97+
}
9198
return path
9299
}
93100

0 commit comments

Comments
 (0)