coverage: Don't panic if flag.CommandLine is reassigned#4384
Merged
fmeum merged 2 commits intobazel-contrib:masterfrom Jun 27, 2025
Merged
coverage: Don't panic if flag.CommandLine is reassigned#4384fmeum merged 2 commits intobazel-contrib:masterfrom
fmeum merged 2 commits intobazel-contrib:masterfrom
Conversation
When run with `go test -cvoer`, the following test does not panic:
```
-- foo.go --
package foo
func foo() int {
return 42
}
-- foo_test.go --
package foo
import (
"flag"
"testing"
)
func TestReassign(t *testing.T) {
flag.CommandLine = flag.NewFlagSet("test", flag.ExitOnError)
_ = foo()
}
```
However, with `bazel coverage`, it does panic:
```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x20 pc=0x100f8dbe4]
goroutine 1 [running]:
github.com/bazelbuild/rules_go/go/tools/bzltestutil.ConvertCoverToLcov()
external/io_bazel_rules_go/go/tools/bzltestutil/lcov.go:39 +0x54
github.com/bazelbuild/rules_go/go/tools/bzltestutil.lcovAtExitHook()
external/io_bazel_rules_go/go/tools/bzltestutil/lcov.go:181 +0x1c
github.com/bazelbuild/rules_go/go/tools/bzltestutil.LcovTestDeps.SetPanicOnExit0({{}, 0x18?}, 0x4b?)
external/io_bazel_rules_go/go/tools/bzltestutil/lcov.go:175 +0x24
testing.(*M).after(0x1400007c280?)
GOROOT/src/testing/testing.go:2374 +0x74
testing.(*M).Run(0x1400007c280)
GOROOT/src/testing/testing.go:2185 +0x940
main.main()
bazel-out/darwin_arm64-fastbuild/bin/go_default_test_/testmain.go:157 +0x7a4
```
The cause is that the test reassigned `flag.CommandLine`
and did not restore the original value.
While that's bad behavior, `go test` does not explode,
so neither should `bazel coverage`.
This change fixes the issue by grabbing a reference to
`flag.CommandLine` before the test runs.
Contributor
Author
|
Windows test failed. Is there any way to access the generated artifact? |
Member
|
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.
What type of PR is this?
Bug fix
What does this PR do? Why is it needed?
When run with
go test -cover, the following test does not panic:However, with
bazel coverage, it does panic:The cause is that the test reassigned
flag.CommandLineand did not restore the original value.
While that's bad behavior,
go testdoes not explode,so neither should
bazel coverage.This change fixes the issue by grabbing a reference to
flag.CommandLinebefore the test runs.Which issues(s) does this PR fix?
Fixes #4383