-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/go: go test using -coverpkg can change the test result #36541
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
Comments
I think this is the same underlying issue as described in #21283. |
This is working as designed (and as you say, is a duplicate of #21283). Packages instrumented for coverage with |
Thanks for the quick response @jayconrod. #21283 was closed by https://go-review.googlesource.com/c/go/+/76876/ which neither fixed the issue nor mentioned deliberately not fixing it so I wasn't sure if it was working as designed. Do you think it's worth updating the documentation for |
@jayconrod would it be reasonable to put the coverage state in a generates sub-package (instead of the package itself) and import that from the test main package? |
Sure, it's definitely worth updating the documentation. From
I think it would be feasible. Coverage data already needs to be across packages since it's imported by the generated test main package. The package holding coverage data wouldn't need to import anything else, so it wouldn't create dependency problems. That said, this would be a substantial change with some visible side effects. If you're interesting in pursuing this, I'd suggest opening a proposal issue. |
Change https://golang.org/cl/215317 mentions this issue: |
I'll try to investigate what this change would look like and if it seems doable I'll make a proposal issue. Thanks @jayconrod! |
No proposal needed. @bcmills, @matloob and I discussed this today. It doesn't seem like the current behavior is actually intended or desired, and if anyone is depending on it, it's probably only by mistake. So we'll try and fix this for the 1.15 cycle. There are a number of related issues, so rather than re-opening this one, let's consider #23910 to be the canonical tracking issue. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Construct a module where an
init()
function in one package,a
, modifies state in another package,b
. Make a test inb
that uses this state:This test will fail when
-coverpkg ./...
is used and succeed when it is not used:What did you expect to see?
I expected the outcome of the test to be the same regardless of the
-coverpkg
argument.What did you see instead?
Using
-coverpkg
will cause theinit()
functions of matching packages to be called. This can change the test result.The text was updated successfully, but these errors were encountered: