Skip to content

cmd/go: fix coverage overlay #73824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/cmd/go/internal/work/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -1899,12 +1899,21 @@ func (b *Builder) installHeader(ctx context.Context, a *Action) error {
// regular outputs (instrumented source files) the cover tool also
// writes a separate file (appearing first in the list of outputs)
// that will contain coverage counters and meta-data.
//
// When an overlay is in use, it ensures the coverage tool processes the overlaid
// files rather than the original source files.
func (b *Builder) cover(a *Action, infiles, outfiles []string, varName string, mode string) ([]string, error) {
pkgcfg := a.Objdir + "pkgcfg.txt"
covoutputs := a.Objdir + "coveroutfiles.txt"
odir := filepath.Dir(outfiles[0])
cv := filepath.Join(odir, "covervars.go")
outfiles = append([]string{cv}, outfiles...)
overlayInfiles := make([]string, 0, len(infiles))
for _, f := range infiles {
overlayPath := fsys.Actual(f)
overlayInfiles = append(overlayInfiles, overlayPath)
}

if err := b.writeCoverPkgInputs(a, pkgcfg, covoutputs, outfiles); err != nil {
return nil, err
}
Expand All @@ -1914,7 +1923,7 @@ func (b *Builder) cover(a *Action, infiles, outfiles []string, varName string, m
"-var", varName,
"-outfilelist", covoutputs,
}
args = append(args, infiles...)
args = append(args, overlayInfiles...)
if err := b.Shell(a).run(a.Objdir, "", nil,
cfg.BuildToolexec, args); err != nil {
return nil, err
Expand Down
77 changes: 77 additions & 0 deletions src/cmd/go/testdata/script/cover_overlay.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Test that coverage works correctly with overlays

env GO111MODULE=on

mkdir covmod
cd covmod

-- go.mod --
module example.com/covmod

go 1.25

-- a.go --
package a

func Hello() string {
return "Hello: World"
}

func Helper() string {
return "helper"
}

-- a_test.go --
package a

import "testing"

func TestHello(t *testing.T) {
got := Hello()
expected := "Hello: World"
if got != expected {
t.Fatalf("Hello() = %q, want %q", got, expected)
}
}

func TestHelper(t *testing.T) {
got := Helper()
expected := "helper"
if got != expected {
t.Fatalf("Helper() = %q, want %q", got, expected)
}
}

-- overlay/a.go --
package a

func Hello() string {
panic("overlay")
}

func Helper() string {
panic("overlay helper")
}

-- overlay.json --
{"Replace": {"a.go": "overlay/a.go"}}

exists overlay.json

go mod tidy

go test -v

! exec sh -c '! go test -overlay=overlay.json -coverpkg=example.com/covmod 2>&1 | grep -q "panic: overlay"'

! exec sh -c '! go test -overlay=overlay.json -run=TestHello -coverpkg=example.com/covmod 2>&1 | grep -q "panic: overlay"'

! exec sh -c '! go test -overlay=overlay.json -run=TestHelper -coverpkg=example.com/covmod 2>&1 | grep -q "panic: overlay helper"'

! go test -overlay=overlay.json -coverpkg=example.com/covmod -coverprofile=coverage.txt

exists coverage.txt

! grep -q 'overlay/a\.go' coverage.txt

rm -f coverage.txt