Skip to content

Pass absolute source paths to go tool cover - #372

Open
aroulle-ubika wants to merge 1 commit into
please-build:masterfrom
aroulle-ubika:fix/cover-absolute-line-directives
Open

Pass absolute source paths to go tool cover#372
aroulle-ubika wants to merge 1 commit into
please-build:masterfrom
aroulle-ubika:fix/cover-absolute-line-directives

Conversation

@aroulle-ubika

Copy link
Copy Markdown

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.

`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.
@aroulle-ubika

Copy link
Copy Markdown
Author

@toastwaffle @samwestmoreland @Tatskaari

Could someone review, plz 👼

@toastwaffle
toastwaffle self-requested a review August 28, 2026 15:49
@toastwaffle

Copy link
Copy Markdown
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.

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

Successfully merging this pull request may close these issues.

2 participants