Skip to content

Commit d34548e

Browse files
author
Jay Conrod
committed
cmd/go: avoid link error when -coverpkg covers main packages (more)
This fixes two problems missed in CL 164877. First, p.Internal.BuildInfo is now part of the cache key. This is important since p.Internal.BuildInfo causes the build action to synthesize a new source file, which affects the output. Second, recompileForTest is always called for test packages. Previously, it was only called when there were internal test sources, so the fix in CL 164877 did not apply to packages that only had external tests. Fixes #30374 Change-Id: Iac2d7e8914f0313f9ab4222299a866f67889eb2e Reviewed-on: https://go-review.googlesource.com/c/go/+/168200 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent faa7fa0 commit d34548e

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -292,17 +292,8 @@ func TestPackagesAndErrors(p *Package, cover *TestCover) (pmain, ptest, pxtest *
292292
pmain.Imports = pmain.Imports[:w]
293293
pmain.Internal.RawImports = str.StringList(pmain.Imports)
294294

295-
if ptest != p {
296-
// We have made modifications to the package p being tested
297-
// and are rebuilding p (as ptest).
298-
// Arrange to rebuild all packages q such that
299-
// the test depends on q and q depends on p.
300-
// This makes sure that q sees the modifications to p.
301-
// Strictly speaking, the rebuild is only necessary if the
302-
// modifications to p change its export metadata, but
303-
// determining that is a bit tricky, so we rebuild always.
304-
recompileForTest(pmain, p, ptest, pxtest)
305-
}
295+
// Replace pmain's transitive dependencies with test copies, as necessary.
296+
recompileForTest(pmain, p, ptest, pxtest)
306297

307298
// Should we apply coverage analysis locally,
308299
// only for this package and only for this test?
@@ -351,6 +342,14 @@ Search:
351342
return stk
352343
}
353344

345+
// recompileForTest copies and replaces certain packages in pmain's dependency
346+
// graph. This is necessary for two reasons. First, if ptest is different than
347+
// preal, packages that import the package under test should get ptest instead
348+
// of preal. This is particularly important if pxtest depends on functionality
349+
// exposed in test sources in ptest. Second, if there is a main package
350+
// (other than pmain) anywhere, we need to clear p.Internal.BuildInfo in
351+
// the test copy to prevent link conflicts. This may happen if both -coverpkg
352+
// and the command line patterns include multiple main packages.
354353
func recompileForTest(pmain, preal, ptest, pxtest *Package) {
355354
// The "test copy" of preal is ptest.
356355
// For each package that depends on preal, make a "test copy"
@@ -393,7 +392,7 @@ func recompileForTest(pmain, preal, ptest, pxtest *Package) {
393392

394393
// Don't compile build info from a main package. This can happen
395394
// if -coverpkg patterns include main packages, since those packages
396-
// are imported by pmain.
395+
// are imported by pmain. See golang.org/issue/30907.
397396
if p.Internal.BuildInfo != "" && p != pmain {
398397
split()
399398
}

src/cmd/go/internal/work/exec.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
213213
if p.Internal.CoverMode != "" {
214214
fmt.Fprintf(h, "cover %q %q\n", p.Internal.CoverMode, b.toolID("cover"))
215215
}
216+
fmt.Fprintf(h, "modinfo %q\n", p.Internal.BuildInfo)
216217

217218
// Configuration specific to compiler toolchain.
218219
switch cfg.BuildToolchainName {

src/cmd/go/testdata/script/cover_pkgall_multiple_mains.txt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,38 @@ env GO111MODULE=on
66

77
[short] skip
88

9-
go test -coverpkg=all ./main1 ./main2
9+
go test -coverpkg=all ./...
1010

1111
-- go.mod --
1212
module example.com/cov
1313

14-
-- main1/main1.go --
14+
-- mainonly/mainonly.go --
1515
package main
1616

1717
func main() {}
1818

19-
-- main1/main1_test.go --
19+
-- mainwithtest/mainwithtest.go --
2020
package main
2121

22-
import "testing"
22+
func main() {}
2323

24-
func TestMain1(t *testing.T) {}
24+
func Foo() {}
2525

26-
-- main2/main2.go --
26+
-- mainwithtest/mainwithtest_test.go --
2727
package main
2828

29-
func main() {}
29+
import "testing"
3030

31-
-- main2/main2_test.go --
32-
package main
31+
func TestFoo(t *testing.T) {
32+
Foo()
33+
}
3334

34-
import "testing"
35+
-- xtest/x.go --
36+
package x
3537

36-
func TestMain2(t *testing.T) {}
38+
-- xtest/x_test.go --
39+
package x_test
40+
41+
import "testing"
3742

43+
func TestX(t *testing.T) {}

0 commit comments

Comments
 (0)