rogpeppe / go-internal

Selected Go-internal packages factored out from the standard library
BSD 3-Clause "New" or "Revised" License
858 stars 70 forks source link

testscript coverage profile merging stopped working in Go 1.20 #204

Closed alnvdl-work closed 1 year ago

alnvdl-work commented 1 year ago

To reproduce, create the following structure:

.
├── profile_test.go
└── testdata
    └── run-mycmd.txt

profile_test.go:

package profile_test

import (
    "os"
    "testing"

    "github.com/rogpeppe/go-internal/testscript"
)

func runMain() int {
    return 0
}

func Test(t *testing.T) {
    t.Parallel()
    testscript.Run(t, testscript.Params{
        Dir: "testdata",
    })
}

func TestMain(m *testing.M) {
    os.Exit(testscript.RunMain(m, map[string]func() int{
        "mycmd": runMain,
    }))
}

run-mycmd.txt:

mycmd

Then run with Go 1.20:

$ go version && go test ./... -coverprofile /tmp/cover.out
go version go1.20.2 linux/amd64
--- FAIL: Test (0.00s)
    --- FAIL: Test/run-mycmd (0.01s)
        testscript.go:429: > mycmd
            [exit status 2]
            FAIL: testdata/run-mycmd.txt:1: unexpected command failure

FAIL
coverage: [no statements]
2023/03/09 01:21:50 cannot merge cover profiles: cannot merge coverage profile from /tmp/testscript-main438601870/cover/mycmd-e2e42716620b: no lines found in profile: <nil>
FAIL    example.com/profile/cmd/profile        0.012s

With Go 1.19 and earlier:

$ go version && go test ./... -coverprofile /tmp/cover.out
go version go1.19.4 linux/amd64
ok      example.com/profile/cmd/profile        0.012s  coverage: [no statements]

Also in Go 1.20 without -coverprofile:

$ go version && go test ./... 
go version go1.20.2 linux/amd64
ok      example.com/profile/cmd/profile        0.011s

This is happening in a larger application that has been using testscript for a few years with -coverprofile, not just in this simplified example.

I haven't had the time to look into the cause yet. I'm open to working on a PR if anyone knows the cause.

mvdan commented 1 year ago

This is fixed in master, see https://github.com/rogpeppe/go-internal/issues/199.