aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Conrod <jayconrod@google.com>2019-09-13 11:10:36 -0400
committerBryan C. Mills <bcmills@google.com>2019-09-25 14:29:09 +0000
commit613adc62688a609e4c113a768fc49ab979812b68 (patch)
tree4672933e62a6c2bb2a154fbd56e44957953fd011
parent1dc1e7c346658717a03c09f3b5f35b91c0c0c6a6 (diff)
downloadgo-613adc62688a609e4c113a768fc49ab979812b68.tar.gz
go-613adc62688a609e4c113a768fc49ab979812b68.zip
[release-branch.go1.13] 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 #34223 Change-Id: Ic1e53437942269a950dd7e45d163707922c92edd Reviewed-on: https://go-review.googlesource.com/c/go/+/195279 Run-TryBot: Jay Conrod <jayconrod@google.com> Reviewed-by: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> (cherry picked from commit 24781a1faf62ac5c9a553af6f46b787e972f5539) Reviewed-on: https://go-review.googlesource.com/c/go/+/195281
-rw-r--r--src/cmd/go/internal/load/test.go11
-rw-r--r--src/cmd/go/testdata/script/cover_pkgall_multiple_mains.txt21
2 files changed, 19 insertions, 13 deletions
diff --git a/src/cmd/go/internal/load/test.go b/src/cmd/go/internal/load/test.go
index afff5deaaa..2864fb5ebb 100644
--- a/src/cmd/go/internal/load/test.go
+++ b/src/cmd/go/internal/load/test.go
@@ -399,10 +399,13 @@ func recompileForTest(pmain, preal, ptest, pxtest *Package) {
}
}
- // 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. See golang.org/issue/30907.
- if p.Internal.BuildInfo != "" && p != pmain {
+ // Force main packages the test imports to be built as libraries.
+ // Normal imports of main packages are forbidden by the package loader,
+ // but this can still happen if -coverpkg patterns include main packages:
+ // 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()
}
}
diff --git a/src/cmd/go/testdata/script/cover_pkgall_multiple_mains.txt b/src/cmd/go/testdata/script/cover_pkgall_multiple_mains.txt
index ab7cd66949..f21cd8b3a8 100644
--- a/src/cmd/go/testdata/script/cover_pkgall_multiple_mains.txt
+++ b/src/cmd/go/testdata/script/cover_pkgall_multiple_mains.txt
@@ -1,29 +1,32 @@
# 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
+# Verifies golang.org/issue/30374, golang.org/issue/34114.
[short] skip
+cd $GOPATH/src/example.com/cov
+
+env GO111MODULE=on
+go test -coverpkg=all ./...
+env GO111MODULE=off
go test -coverpkg=all ./...
--- go.mod --
+-- $GOPATH/src/example.com/cov/go.mod --
module example.com/cov
--- mainonly/mainonly.go --
+-- $GOPATH/src/example.com/cov/mainonly/mainonly.go --
package main
func main() {}
--- mainwithtest/mainwithtest.go --
+-- $GOPATH/src/example.com/cov/mainwithtest/mainwithtest.go --
package main
func main() {}
func Foo() {}
--- mainwithtest/mainwithtest_test.go --
+-- $GOPATH/src/example.com/cov/mainwithtest/mainwithtest_test.go --
package main
import "testing"
@@ -32,10 +35,10 @@ func TestFoo(t *testing.T) {
Foo()
}
--- xtest/x.go --
+-- $GOPATH/src/example.com/cov/xtest/x.go --
package x
--- xtest/x_test.go --
+-- $GOPATH/src/example.com/cov/xtest/x_test.go --
package x_test
import "testing"