Skip to content

Conversation

@r-hang
Copy link
Contributor

@r-hang r-hang commented Aug 13, 2025

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.

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.

ref #3513

r-hang added 2 commits August 13, 2025 23:25
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.
Copy link
Member

@fmeum fmeum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug print?

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 "*/"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace constants with len calls and drop the comment

t.Errorf("find first line directive filename: got %q, want %q", got, test.want)
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

3 participants