-
-
Notifications
You must be signed in to change notification settings - Fork 723
coverageredesign: Map line directive file names to exec relative paths #4424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
coverageredesign: Map line directive file names to exec relative paths #4424
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
left a comment
There was a problem hiding this 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) |
There was a problem hiding this comment.
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 "*/" |
There was a problem hiding this comment.
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) | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
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