Skip to content

Commit 3e7ec13

Browse files
committed
cmd/go: fix build config for 'go list -cover'
When 'go list -cover' is run in a way that triggers package builds (for example, -export), ensure that the build step actually includes coverage instrumentation as part of the config. Without this we will wind up with incorrect build IDs. Fixes #60755. Change-Id: Ic84ab9e301d075bee5ff9d6828370a1708be0035 Reviewed-on: https://go-review.googlesource.com/c/go/+/502877 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Run-TryBot: Than McIntosh <[email protected]>
1 parent 30b17f4 commit 3e7ec13

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/cmd/go/internal/list/list.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,9 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
730730
a.Deps = append(a.Deps, b.AutoAction(work.ModeInstall, work.ModeInstall, p))
731731
}
732732
}
733+
if cfg.Experiment.CoverageRedesign && cfg.BuildCover {
734+
load.PrepareForCoverageBuild(pkgs)
735+
}
733736
b.Do(ctx, a)
734737
}
735738

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,50 @@ go install m/example
1616
# with -cover.
1717
stale -cover m/example
1818

19+
# Collect build ID from for m/example built with -cover.
20+
go list -cover -export -f '{{.BuildID}}' m/example
21+
cp stdout $WORK/listbuildid.txt
22+
23+
# Now build the m/example binary with coverage.
24+
go build -cover -o $WORK/m.exe m/example
25+
26+
# Ask for the binary build ID by running "go tool buildid".
27+
go tool buildid $WORK/m.exe
28+
cp stdout $WORK/rawtoolbuildid.txt
29+
30+
# Make sure that the two build IDs agree with respect to the
31+
# m/example package. Build IDs from binaries are of the form X/Y/Z/W
32+
# where Y/Z is the package build ID; running the program below will
33+
# pick out the parts of the ID that we want.
34+
env GOCOVERDIR=$WORK
35+
exec $WORK/m.exe $WORK/rawtoolbuildid.txt
36+
cp stdout $WORK/toolbuildid.txt
37+
38+
# Build IDs should match here.
39+
cmp $WORK/toolbuildid.txt $WORK/listbuildid.txt
40+
1941
-- go.mod --
2042
module m
2143

2244
go 1.20
2345
-- example/main.go --
2446
package main
2547

48+
import (
49+
"fmt"
50+
"os"
51+
"strings"
52+
)
53+
2654
func main() {
27-
println("hi mom")
55+
println(os.Args[1])
56+
content, err := os.ReadFile(os.Args[1])
57+
if err != nil {
58+
os.Exit(1)
59+
}
60+
fields := strings.Split(strings.TrimSpace(string(content)), "/")
61+
if len(fields) != 4 {
62+
os.Exit(2)
63+
}
64+
fmt.Println(fields[1] + "/" + fields[2])
2865
}

0 commit comments

Comments
 (0)