Pass absolute source paths to go tool cover - #372
Open
aroulle-ubika wants to merge 1 commit into
Open
Conversation
`go tool cover` copies each source path it is given verbatim into a
`//line <path>:1:1` directive at the top of the file it generates.
Since go.dev/issue/70478, released in Go 1.27, the compiler resolves a relative
filename in a //line directive against the directory of the file containing the
directive rather than against the current directory. Absolute filenames are
unaffected.
please_go passes relative paths, so from Go 1.27 onwards the package directory
ends up duplicated in every position recorded for an instrumented file. Code
calling runtime.Caller under `plz cover` sees paths like
lib/go/logger/lib/go/logger/logger_test.go:39
instead of
lib/go/logger/logger_test.go:39
which breaks tests asserting on caller information, and feeds wrong paths into
coverage metadata. `plz test` is unaffected because it does not instrument.
Make the paths absolute before handing them over. While here, fix an adjacent
bug: filepath.Join(dir, "_covervars.cover.go\n") ran the trailing newline
through Clean and swallowed it, so the covervars entry and the first source
shared a line in the output file list.
Author
|
@toastwaffle @samwestmoreland @Tatskaari Could someone review, plz 👼 |
toastwaffle
self-requested a review
August 28, 2026 15:49
Contributor
|
@aroulle-ubika I'll review this on Tuesday. I'm a little concerned that using absolute paths like this will lead to problems with sandboxing and remote execution, but it is too late on a Friday for me to give this the required level of thought. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
go tool covercopies each source path it is given verbatim into a//line <path>:1:1directive at the top of the file it generates.Since go.dev/issue/70478, released in Go 1.27, the compiler resolves a relative filename in a //line directive against the directory of the file containing the directive rather than against the current directory. Absolute filenames are unaffected.
please_go passes relative paths, so from Go 1.27 onwards the package directory ends up duplicated in every position recorded for an instrumented file. Code calling runtime.Caller under
plz coversees paths likeinstead of
which breaks tests asserting on caller information, and feeds wrong paths into coverage metadata.
plz testis unaffected because it does not instrument.Make the paths absolute before handing them over. While here, fix an adjacent bug: filepath.Join(dir, "_covervars.cover.go\n") ran the trailing newline through Clean and swallowed it, so the covervars entry and the first source shared a line in the output file list.