Skip to content

Commit 779d878

Browse files
committed
cmd/go: add 'go test -cachelink' boolean flag
This permits (opt-in via a new flag) running package tests faster in subsequent runs with different -test.run=... flags, at the expense of writing more to the build cache. Updates golang#77349 Updates #149 Cherry-picked from https://go-review.googlesource.com/c/go/+/739161 v3. Change-Id: I1c84920b57b2c20f32f5ec6a1f7b540b1eb6779d
1 parent 799b253 commit 779d878

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/cmd/go/alldocs.go

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/test/test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ In addition to the build flags, the flags handled by 'go test' itself are:
153153
(where pkg is the last element of the package's import path).
154154
The file name or target directory can be changed with the -o flag.
155155
156+
-cachelink
157+
Cache the emphemeral test binary (the linker output) in the build
158+
cache. This avoids re-linking the test binary if running the same
159+
test binary repeatedly but with different flags, environment,
160+
or other test inputs such that the test caching rules above
161+
would not apply. On such case is Go test wrappers that shard tests
162+
within a package across multiple machines, sharing a common build
163+
cache with GOCACHEPROG.
164+
156165
-exec xprog
157166
Run the test binary using xprog. The behavior is the same as
158167
in 'go run'. See 'go help run' for details.
@@ -543,6 +552,7 @@ See the documentation of the testing package for more information.
543552
var (
544553
testBench string // -bench flag
545554
testC bool // -c flag
555+
testCacheLink bool // -cachelink flag
546556
testCoverPkgs []*load.Package // -coverpkg flag
547557
testCoverProfile string // -coverprofile flag
548558
testFailFast bool // -failfast flag
@@ -1190,6 +1200,7 @@ func builderTest(b *work.Builder, ctx context.Context, pkgOpts load.PackageOpts,
11901200
b.CompileAction(work.ModeBuild, work.ModeBuild, pmain).Objdir = testDir
11911201

11921202
a := b.LinkAction(work.ModeBuild, work.ModeBuild, pmain)
1203+
a.CacheExecutable = testCacheLink
11931204
a.Target = testDir + testBinary + cfg.ExeSuffix
11941205
if cfg.Goos == "windows" {
11951206
// There are many reserved words on Windows that,

src/cmd/go/internal/test/testflag.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func init() {
3434

3535
cf := CmdTest.Flag
3636
cf.BoolVar(&testC, "c", false, "")
37+
cf.BoolVar(&testCacheLink, "cachelink", false, "")
3738
cf.StringVar(&testO, "o", "", "")
3839
work.AddCoverFlags(CmdTest, &testCoverProfile)
3940
cf.Var((*base.StringsFlag)(&work.ExecCmd), "exec", "")

0 commit comments

Comments
 (0)