Skip to content

bazel coverage panics if flag.CommandLine is reassigned #4383

@abhinav

Description

@abhinav

When run with go test -cover, 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions