Commit 24781a1f authored by Jay Conrod's avatar Jay Conrod

cmd/go: fix link error for -coverpkg in GOPATH mode

If a generated test main package transitively depends on a main
package, the main package will now always be rebuilt as a library and
will not be compiled with '-p main'.

This expands the fix for #30907, which only applied to packages with
the BuildInfo set (main packages built in module mode). Linking
multiple packages with BuildInfo caused link errors, but it appears
these errors apply to some symbols in GOPATH mode.

Fixes #34114

Change-Id: Ic1e53437942269a950dd7e45d163707922c92edd
Reviewed-on: https://go-review.googlesource.com/c/go/+/195279
Run-TryBot: Jay Conrod <jayconrod@google.com>
Reviewed-by: default avatarBryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 79877e5f
...@@ -399,10 +399,13 @@ func recompileForTest(pmain, preal, ptest, pxtest *Package) { ...@@ -399,10 +399,13 @@ func recompileForTest(pmain, preal, ptest, pxtest *Package) {
} }
} }
// Don't compile build info from a main package. This can happen // Force main packages the test imports to be built as libraries.
// if -coverpkg patterns include main packages, since those packages // Normal imports of main packages are forbidden by the package loader,
// are imported by pmain. See golang.org/issue/30907. // but this can still happen if -coverpkg patterns include main packages:
if p.Internal.BuildInfo != "" && p != pmain { // covered packages are imported by pmain. Linking multiple packages
// compiled with '-p main' causes duplicate symbol errors.
// See golang.org/issue/30907, golang.org/issue/34114.
if p.Name == "main" && p != pmain {
split() split()
} }
} }
......
# This test checks that multiple main packages can be tested # This test checks that multiple main packages can be tested
# with -coverpkg=all without duplicate symbol errors. # with -coverpkg=all without duplicate symbol errors.
# Verifies golang.org/issue/30374. # Verifies golang.org/issue/30374, golang.org/issue/34114.
env GO111MODULE=on
[short] skip [short] skip
cd $GOPATH/src/example.com/cov
env GO111MODULE=on
go test -coverpkg=all ./...
env GO111MODULE=off
go test -coverpkg=all ./... go test -coverpkg=all ./...
-- go.mod -- -- $GOPATH/src/example.com/cov/go.mod --
module example.com/cov module example.com/cov
-- mainonly/mainonly.go -- -- $GOPATH/src/example.com/cov/mainonly/mainonly.go --
package main package main
func main() {} func main() {}
-- mainwithtest/mainwithtest.go -- -- $GOPATH/src/example.com/cov/mainwithtest/mainwithtest.go --
package main package main
func main() {} func main() {}
func Foo() {} func Foo() {}
-- mainwithtest/mainwithtest_test.go -- -- $GOPATH/src/example.com/cov/mainwithtest/mainwithtest_test.go --
package main package main
import "testing" import "testing"
...@@ -32,10 +35,10 @@ func TestFoo(t *testing.T) { ...@@ -32,10 +35,10 @@ func TestFoo(t *testing.T) {
Foo() Foo()
} }
-- xtest/x.go -- -- $GOPATH/src/example.com/cov/xtest/x.go --
package x package x
-- xtest/x_test.go -- -- $GOPATH/src/example.com/cov/xtest/x_test.go --
package x_test package x_test
import "testing" import "testing"
......
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