Skip to content

cmd/cover: coverageredesign configurable filepaths for Go coverage files #74749

@r-hang

Description

@r-hang

This is a feature request for a way to configure the filepath associated with coverage data in Go coverage files.

Instead of the importpath/filename filepath, rules_go/Bazel requires an execution root (e.g. src/) relative filepath.

This mapping configuration could be provided as a flag to go tool cover or as an additional field to the -pkgcfg option. In rules_go, only at the compilation step where go tool cover is invoked do we have enough information to produce a mapping between importpath/filename and src/ (e.g. execution root) relative filepath.

Background:

The old coverage system (nocoverageredesign) set a global Cover struct https://github.com/golang/go/blob/go1.24.5/src/testing/cover.go#L30 that
rules_go exploited to ensure that Go coverage files had filepaths that worked
with Bazel's lcov format (execution root relative) by overriding the filepath coverage
by augmenting coverage source files to adjust the global Cover struct at runtime.

Here is a sample rules_go nocoverageredesign coverage file

b mode: set
src/other_lib.go:3.42,4.14 1 1
src/other_lib.go:7.2,7.35 1 1
src/other_lib.go:4.14,6.3 1 0
src/lib.go:9.41,11.14 2 1
src/lib.go:16.2,17.38 2 1
src/lib.go:11.14,13.3 1 0
src/lib.go:13.8,15.3 1 1

which works with bazel coverage and lcov as the file paths are execution root relative.

In coverageredesign, this global Cover struct goes away so there doesn't seem to be a hook by which the filepath associated with coverage data can be modified. The format of coverage files in coverageredesign looks to be importpath/filename

b mode: set
example.com/lib/lib.go:9.41,11.14 2 1
example.com/lib/lib.go:11.14,13.3 1 0
example.com/lib/lib.go:13.8,15.3 1 1
example.com/lib/lib.go:16.2,17.38 2 1
example.com/other_lib/other_lib.go:3.42,4.14 1 1
example.com/other_lib/other_lib.go:4.14,6.3 1 0
example.com/other_lib/other_lib.go:7.2,7.35 1 1

In the current proposed implementation to keep coverageredesign working with Bazel (bazel-contrib/rules_go#4397), we find a way to augment coverage source files to track a mapping from importpath/filename -> execution root relative file because only at compilation time do we know enough information to produce this mapping.

Unfortunately, this runtime mapping requires a dependency on a global variable in "github.com/bazelbuild/rules_go/go/tools/coverdata" which makes it difficult for non-test rules_go coverage code to work because there is a reliance on this hidden test dependency that we can't ensure outside of rules_go generated test mains.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureRequestIssues asking for a new feature that does not need a proposal.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolProposalIssues describing a requested change to a Go tool or command-line program.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions