Commit 10156b67 authored by Jay Conrod's avatar Jay Conrod

cmd/go: avoid link errors when -coverpkg covers main packages

The -coverpkg lets users specify a list of packages that should have
coverage instrumentation. This may include packages not transitively
imported by tests. For each tested package, the synthetic main package
imports all covered packages so they can be registered with
testing.RegisterCover. This makes it possible for a main package to
import another main package.

When we compile a package with p.Internal.BuildInfo set (set on main
packages by Package.load in module mode), we set
runtime/debug.modinfo. Multiple main packages may be passed to the
linker because of the above scenario, so this causes duplicate symbol
errors.

This change copies p.Internal.BuildInfo to the synthetic main package
instead of the internal test package. Additionally, it forces main
packages imported by the synthetic test main package to be recompiled
for testing. Recompiled packages won't have p.Internal.BuildInfo set.

Fixes #30374

Change-Id: I06f028d55905039907940ec89d2835f5a1040203
Reviewed-on: https://go-review.googlesource.com/c/go/+/164877
Run-TryBot: Jay Conrod <jayconrod@google.com>
Reviewed-by: default avatarBryan C. Mills <bcmills@google.com>
parent 361a0198
......@@ -129,6 +129,7 @@ func TestPackagesFor(p *Package, cover *TestCover) (pmain, ptest, pxtest *Packag
ptest.Internal.Imports = append(imports, p.Internal.Imports...)
ptest.Internal.RawImports = str.StringList(rawTestImports, p.Internal.RawImports)
ptest.Internal.ForceLibrary = true
ptest.Internal.BuildInfo = ""
ptest.Internal.Build = new(build.Package)
*ptest.Internal.Build = *p.Internal.Build
m := map[string][]token.Position{}
......@@ -186,6 +187,7 @@ func TestPackagesFor(p *Package, cover *TestCover) (pmain, ptest, pxtest *Packag
},
Internal: PackageInternal{
Build: &build.Package{Name: "main"},
BuildInfo: p.Internal.BuildInfo,
Asmflags: p.Internal.Asmflags,
Gcflags: p.Internal.Gcflags,
Ldflags: p.Internal.Ldflags,
......@@ -352,6 +354,7 @@ func recompileForTest(pmain, preal, ptest, pxtest *Package) {
copy(p1.Imports, p.Imports)
p = p1
p.Target = ""
p.Internal.BuildInfo = ""
}
// Update p.Internal.Imports to use test copies.
......@@ -361,6 +364,13 @@ func recompileForTest(pmain, preal, ptest, pxtest *Package) {
p.Internal.Imports[i] = p1
}
}
// Don't compile build info from a main package. This can happen
// if -coverpkg patterns include main packages, since those packages
// are imported by pmain.
if p.Internal.BuildInfo != "" && p != pmain {
split()
}
}
}
......
# This test checks that multiple main packages can be tested
# with -coverpkg=all without duplicate symbol errors.
# Verifies golang.org/issue/30374.
env GO111MODULE=on
[short] skip
go test -coverpkg=all ./main1 ./main2
-- go.mod --
module example.com/cov
-- main1/main1.go --
package main
func main() {}
-- main1/main1_test.go --
package main
import "testing"
func TestMain1(t *testing.T) {}
-- main2/main2.go --
package main
func main() {}
-- main2/main2_test.go --
package main
import "testing"
func TestMain2(t *testing.T) {}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment