coverageredesign: Map line directive file names to exec relative paths#4424
Open
r-hang wants to merge 2 commits intobazel-contrib:masterfrom
Open
coverageredesign: Map line directive file names to exec relative paths#4424r-hang wants to merge 2 commits intobazel-contrib:masterfrom
r-hang wants to merge 2 commits intobazel-contrib:masterfrom
Conversation
coverageredesign (bazel-contrib#4397) honors line directives while the world before coverageredesign did not. This is relevant in situations where you want coverage for generated code to map to source code in a user's workspace. An example of this is a go_library rule which takes a dependency on a code generator which generates a replacement file for a user source file. The replacement file is run in the final binary but the user wants to see covarege for the source file that was an input to the generated replacement file. ``` go_library( name = "go_default_library", srcs = [ "batch.go", "consumers.go", "edges.go", "token.go", ":codegen", ], ) codegen(name = "codegen, ...) ``` The Go compiler, because of Bazel, only knows about the generated code but coverage is only relevant for the source file that the code generator replaces. Given a generated file `bazel-out/k8-fastbuild/bin/src/example.org/gen_/report_gen.go` nocoverageredesign would emit that file path in coverage files. With coverageredesign, if a line directive in the generated code changes the file name to hello.go (e.g. //line hello.go:10) then the path in the runtime emitted coverage file would be `example.org/gen_/hello.go` which is not execution root relative and would be ignored by Bazel. The prevent this code from breaking in coverage redesign, we need to map the line directive file name to the execution root relative filepath that Bazel respects to get coverage for these types of files. This change extends the "srcPathMapping" trick used in https://github.com/bazel-contrib/rules_go/pull/4397/files to honor a mapping of line directive file names to map back to exec root relative paths that Bazel understands and requires for lcov mode.
fmeum
approved these changes
Aug 14, 2025
Member
fmeum
left a comment
There was a problem hiding this comment.
Looks good to me after the comments have been resolved.
@jayconrod Could you also take a look?
| } | ||
| filename := src | ||
| if directiveName != "" { | ||
| fmt.Println("directiveName: ", directiveName) |
| content = directive[7:] // Remove "//line " | ||
| } else if strings.HasPrefix(directive, "/*line ") && strings.HasSuffix(directive, "*/") { | ||
| // Handle /*line*/ directive | ||
| content = directive[7 : len(directive)-2] // Remove "/*line " and "*/" |
Member
There was a problem hiding this comment.
Replace constants with len calls and drop the comment
sluongng
reviewed
Aug 15, 2025
| t.Errorf("find first line directive filename: got %q, want %q", got, test.want) | ||
| } | ||
| } | ||
| } |
Contributor
There was a problem hiding this comment.
Nit: I don't think testing an unexported function like this is useful. It forces future maintainers/contributors to study the implementation details when doing refactoring.
I would prefer it if we could add a high-level test, say in tests/core/coverage/, that verifies the desired behavior. The setup could be as simple as running coverage over a go_test target with genrule created one of the source files. That way, we can check for regression in future releases.
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.
coverageredesign (#4397) honors line directives while the world before coverageredesign did not.
This is relevant in situations where you want coverage for generated code to map to source code in a user's workspace.
An example of this is a go_library rule which takes a dependency on a code generator which generates a replacement file for a user source file. The replacement file is run in the final binary but the user wants to see covarege for the source file that was an input to the generated replacement file.
The Go compiler, because of Bazel, only knows about the generated code but coverage is only relevant for the source file that the code generator replaces.
Given a generated file
bazel-out/k8-fastbuild/bin/src/example.org/gen_/report_gen.gonocoverageredesign would emit that file path in coverage files.With coverageredesign, if a line directive in the generated code changes the file name to hello.go (e.g. //line hello.go:10) then the path in the runtime emitted coverage file would be
example.org/gen_/hello.gowhich is not execution root relative and would be ignored by Bazel.The prevent this code from breaking in coverage redesign, we need to map the line directive file name to the execution root relative filepath that Bazel respects to get coverage for these types of files.
This change extends the "srcPathMapping" trick used in https://github.com/bazel-contrib/rules_go/pull/4397/files to honor a mapping of line directive file names to map back to exec root relative paths that Bazel understands and requires for lcov mode.
ref #3513