Conversation
b0fe057 to
6fd9aa1
Compare
linzhp
reviewed
Nov 4, 2021
linzhp
reviewed
Nov 8, 2021
The coverage support in the builder works in the following way:
1. Run `go tool cover` to create an instrumented source file, for example:
```
$ go test -work -cover database/sql
WORK=/var/folders/yf/4v4h0_t53h30t_8vc90d1gx40000gp/T/go-build105184680/
$ find $WORK -name *cover.go
...
$ less /var/folders/yf/4v4h0_t53h30t_8vc90d1gx40000gp/T/go-build105184680//b059/sql.cover.go
...
func Register(name string, driver driver.Driver) {GoCover_2_343132323961636265643234.Count[0] = 1;
driversMu.Lock()
defer driversMu.Unlock()
if driver == nil {GoCover_2_343132323961636265643234.Count[3] = 1;
panic("sql: Register driver is nil")
}
GoCover_2_343132323961636265643234.Count[1] = 1;if _, dup := drivers[name]; dup {GoCover_2_343132323961636265643234.Count[4] = 1;
panic("sql: Register called twice for driver " + name)
}
GoCover_2_343132323961636265643234.Count[2] = 1;drivers[name] = driver
}
...
// appended to end of file
var GoCover_2_343132323961636265643234 = struct {
Count [727]uint32
Pos [3 * 727]uint32
NumStmt [727]uint16
} {
...
```
2. Register each file's coverage (above, `GoCover_2_343132323961636265643234`) with
[coverdata](https://pkg.go.dev/github.com/bazelbuild/rules_go@v0.29.0/go/tools/coverdata).
This requires adding or updating an import for that package and calling `RegisterFile` in `init()`.
This is done by using `go/parser` and `go/ast` to modify the AST and `go/format` to print it
back out to the instrumented source file.
The problem is that this changes line numbers, resulting in panics
that no longer identify the correct stack trace.
`go tool cover` avoids this issue because it places the coverage
variable increments on the same line as control flow branch of
interest to avoid this problem, which it accomplishes by using the
[cmd/internal/edit](https://pkg.go.dev/cmd/internal/edit@go1.17.3)
package.
This change brings that approach to rules_go coverage instrumentation:
1. Copy in the cmd/internal/edit package for bytebuffer-based
editing.
2. Update the `registerCoverage` step to use that instead of AST
modifications & `go/format`.
Fixes #2990
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.
This copies in Go's cmd/internal/edit package for bytebuffer-based
editing.
Fixes #2990