Skip to content
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

cmd/go: coverage profile wonky with generics #68603

Closed
karalabe opened this issue Jul 26, 2024 · 6 comments
Closed

cmd/go: coverage profile wonky with generics #68603

karalabe opened this issue Jul 26, 2024 · 6 comments

Comments

@karalabe
Copy link
Contributor

karalabe commented Jul 26, 2024

Go version

go version go1.22.5 darwin/arm64

Output of go env in your module/workspace:

-

What did you do?

Go coverage reports seems to be an arbitrary rollercoaster when generics are involved.

Here's a report saying that a function in my package has no coverage:

https://app.codecov.io/github/karalabe/ssz/commit/f4f8d91d4d6d8a8b874f35934265b34b414a8c27/blob/codec.go#L70

And the exact same report calls that function with positive coverage:

https://app.codecov.io/github/karalabe/ssz/commit/f4f8d91d4d6d8a8b874f35934265b34b414a8c27/blob/tests/testtypes/consensus-spec-tests/gen_single_field_test_struct_ssz.go#L14

What did you see happen?

A call to function F is reported covered, but function F itself is reported uncovered.

What did you expect to see?

If the coverage tool reports a call to function F, I expect F to be reported as called.

@karalabe karalabe changed the title import/path: issue title cmd/go: coverage profile wonky with generics Jul 26, 2024
@karalabe
Copy link
Contributor Author

karalabe commented Jul 26, 2024

FWIW, most of the methods in those files are reported as uncovered, or parts of it covered and parts not, but the tests are driven by a very comprehensive suite, so I'm quite certain most - if not 100% even - lines should be reported covered in the package.

@karalabe
Copy link
Contributor Author

karalabe commented Jul 26, 2024

And I can demo it is wonky.

Add a print statement to the function:

diff --git a/codec.go b/codec.go
index 02fcae0..15fa686 100644
--- a/codec.go
+++ b/codec.go
@@ -5,6 +5,7 @@
 package ssz
 
 import (
+       "fmt"
        "math/big"
 
        "github.com/holiman/uint256"
@@ -68,6 +69,7 @@ func DefineBool[T ~bool](c *Codec, v *T) {
 
 // DefineUint8 defines the next field as a uint8.
 func DefineUint8[T ~uint8](c *Codec, n *T) {
+       fmt.Println("YOLO")
        if c.enc != nil {
                EncodeUint8(c.enc, *n)
                return

Run the tests with coverage and see how many times that print is hit.

go test ./... -coverprofile cover.txt --covermode=atomic -v | grep YOLO | wc

     128     128     640

Report the coverage for that line (72 based on the diff):

cat cover.txt | grep codec.go:7

github.com/karalabe/ssz/codec.go:71.44,73.18 2 0
github.com/karalabe/ssz/codec.go:73.18,76.3 2 0
github.com/karalabe/ssz/codec.go:77.2,77.18 1 0
github.com/karalabe/ssz/codec.go:77.18,80.3 2 0

Coverage HTML display:

Screenshot 2024-07-26 at 11 59 15

@seankhliao
Copy link
Member

Do you have a self contained reproducer for the issue?

@karalabe
Copy link
Contributor Author

Does a 1.8GB repo count as self contained?

@karalabe
Copy link
Contributor Author

Figured it out, you need go test -coverpkg=./... ./... ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants