Skip to content

Commit b27071c

Browse files
committed
cmd/go: fix coverage overlay
1 parent 0d7dc68 commit b27071c

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

src/cmd/go/internal/work/exec.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1899,12 +1899,21 @@ func (b *Builder) installHeader(ctx context.Context, a *Action) error {
18991899
// regular outputs (instrumented source files) the cover tool also
19001900
// writes a separate file (appearing first in the list of outputs)
19011901
// that will contain coverage counters and meta-data.
1902+
//
1903+
// When an overlay is in use, it ensures the coverage tool processes the overlaid
1904+
// files rather than the original source files.
19021905
func (b *Builder) cover(a *Action, infiles, outfiles []string, varName string, mode string) ([]string, error) {
19031906
pkgcfg := a.Objdir + "pkgcfg.txt"
19041907
covoutputs := a.Objdir + "coveroutfiles.txt"
19051908
odir := filepath.Dir(outfiles[0])
19061909
cv := filepath.Join(odir, "covervars.go")
19071910
outfiles = append([]string{cv}, outfiles...)
1911+
overlayInfiles := make([]string, 0, len(infiles))
1912+
for _, f := range infiles {
1913+
overlayPath := fsys.Actual(f)
1914+
overlayInfiles = append(overlayInfiles, overlayPath)
1915+
}
1916+
19081917
if err := b.writeCoverPkgInputs(a, pkgcfg, covoutputs, outfiles); err != nil {
19091918
return nil, err
19101919
}
@@ -1914,7 +1923,7 @@ func (b *Builder) cover(a *Action, infiles, outfiles []string, varName string, m
19141923
"-var", varName,
19151924
"-outfilelist", covoutputs,
19161925
}
1917-
args = append(args, infiles...)
1926+
args = append(args, overlayInfiles...)
19181927
if err := b.Shell(a).run(a.Objdir, "", nil,
19191928
cfg.BuildToolexec, args); err != nil {
19201929
return nil, err
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Test that coverage works correctly with overlays
2+
3+
env GO111MODULE=on
4+
5+
mkdir covmod
6+
cd covmod
7+
8+
-- go.mod --
9+
module example.com/covmod
10+
11+
go 1.25
12+
13+
-- a.go --
14+
package a
15+
16+
func Hello() string {
17+
return "Hello: World"
18+
}
19+
20+
func Helper() string {
21+
return "helper"
22+
}
23+
24+
-- a_test.go --
25+
package a
26+
27+
import "testing"
28+
29+
func TestHello(t *testing.T) {
30+
got := Hello()
31+
expected := "Hello: World"
32+
if got != expected {
33+
t.Fatalf("Hello() = %q, want %q", got, expected)
34+
}
35+
}
36+
37+
func TestHelper(t *testing.T) {
38+
got := Helper()
39+
expected := "helper"
40+
if got != expected {
41+
t.Fatalf("Helper() = %q, want %q", got, expected)
42+
}
43+
}
44+
45+
-- overlay/a.go --
46+
package a
47+
48+
func Hello() string {
49+
panic("overlay")
50+
}
51+
52+
func Helper() string {
53+
panic("overlay helper")
54+
}
55+
56+
-- overlay.json --
57+
{"Replace": {"a.go": "overlay/a.go"}}
58+
59+
exists overlay.json
60+
61+
go mod tidy
62+
63+
go test -v
64+
65+
! exec sh -c '! go test -overlay=overlay.json -coverpkg=example.com/covmod 2>&1 | grep -q "panic: overlay"'
66+
67+
! exec sh -c '! go test -overlay=overlay.json -run=TestHello -coverpkg=example.com/covmod 2>&1 | grep -q "panic: overlay"'
68+
69+
! exec sh -c '! go test -overlay=overlay.json -run=TestHelper -coverpkg=example.com/covmod 2>&1 | grep -q "panic: overlay helper"'
70+
71+
! go test -overlay=overlay.json -coverpkg=example.com/covmod -coverprofile=coverage.txt
72+
73+
exists coverage.txt
74+
75+
! grep -q 'overlay/a\.go' coverage.txt
76+
77+
rm -f coverage.txt

0 commit comments

Comments
 (0)