Skip to content

Commit 24781a1

Browse files
author
Jay Conrod
committed
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 <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 79877e5 commit 24781a1

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,13 @@ func recompileForTest(pmain, preal, ptest, pxtest *Package) {
399399
}
400400
}
401401

402-
// Don't compile build info from a main package. This can happen
403-
// if -coverpkg patterns include main packages, since those packages
404-
// are imported by pmain. See golang.org/issue/30907.
405-
if p.Internal.BuildInfo != "" && p != pmain {
402+
// Force main packages the test imports to be built as libraries.
403+
// Normal imports of main packages are forbidden by the package loader,
404+
// but this can still happen if -coverpkg patterns include main packages:
405+
// covered packages are imported by pmain. Linking multiple packages
406+
// compiled with '-p main' causes duplicate symbol errors.
407+
// See golang.org/issue/30907, golang.org/issue/34114.
408+
if p.Name == "main" && p != pmain {
406409
split()
407410
}
408411
}

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

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
# This test checks that multiple main packages can be tested
22
# with -coverpkg=all without duplicate symbol errors.
3-
# Verifies golang.org/issue/30374.
4-
5-
env GO111MODULE=on
3+
# Verifies golang.org/issue/30374, golang.org/issue/34114.
64

75
[short] skip
6+
cd $GOPATH/src/example.com/cov
7+
8+
env GO111MODULE=on
9+
go test -coverpkg=all ./...
810

11+
env GO111MODULE=off
912
go test -coverpkg=all ./...
1013

11-
-- go.mod --
14+
-- $GOPATH/src/example.com/cov/go.mod --
1215
module example.com/cov
1316

14-
-- mainonly/mainonly.go --
17+
-- $GOPATH/src/example.com/cov/mainonly/mainonly.go --
1518
package main
1619

1720
func main() {}
1821

19-
-- mainwithtest/mainwithtest.go --
22+
-- $GOPATH/src/example.com/cov/mainwithtest/mainwithtest.go --
2023
package main
2124

2225
func main() {}
2326

2427
func Foo() {}
2528

26-
-- mainwithtest/mainwithtest_test.go --
29+
-- $GOPATH/src/example.com/cov/mainwithtest/mainwithtest_test.go --
2730
package main
2831

2932
import "testing"
@@ -32,10 +35,10 @@ func TestFoo(t *testing.T) {
3235
Foo()
3336
}
3437

35-
-- xtest/x.go --
38+
-- $GOPATH/src/example.com/cov/xtest/x.go --
3639
package x
3740

38-
-- xtest/x_test.go --
41+
-- $GOPATH/src/example.com/cov/xtest/x_test.go --
3942
package x_test
4043

4144
import "testing"

0 commit comments

Comments
 (0)