runtime: Caller
can return covervars.go
as filename when called from a global variable in test with coverage
#64521
Labels
compiler/runtime
Issues related to the Go compiler and/or runtime.
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes (
1.21.4
)It doesn't reproduce with Go
1.20.x
.On the current main branch, it is shadowed by the issue described in #64520 (but I still opened this separate issue as I'm not sure whether fixing the other one would fix this one, and this one appears in a released version).
It also reproduces on different OS and architectures (eg.
linux/amd64
)What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
When using Go 1.21, when running test with coverage enabled,
runtime.Caller
can returncovervars.go
as caller filename, instead of the actual caller filename.A minimal reproducible example can be found on https://github.com/pgimalac/gist/tree/go-runtime-caller-filename-covervars-go (I could not reduce it to a single file, but it's rather small as it is).
The example declares a global variable which indirectly calls
runtime.Caller
, which returnscovervars.go
instead of the actual filename.The bug is triggered by running
go test ./... -cover
, a test which checks that the filename containspackage1
will fail.If this is not considered a bug, the documentation should make it clear that
runtime.Caller
can return something else when called from a global variable when running in a test with coverage enabled.What did you expect to see?
runtime.Caller
should return<root>/mypackage1/myfile.go
.What did you see instead?
runtime.Caller
returnscovervars.go
.The text was updated successfully, but these errors were encountered: