Skip to content
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

cmd/cover: -coverpkg causes init methods to be run for non-imported packages #46212

Closed
benmoss opened this issue May 17, 2021 · 3 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@benmoss
Copy link

benmoss commented May 17, 2021

What version of Go are you using (go version)?

$ go version
go version go1.16.4 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/cesium/.cache/go-build"
GOENV="/home/cesium/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/cesium/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/cesium/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.4"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/cesium/workspace/foo/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build292183072=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Very similar to #21283, but I think the distinction here is that if you run:

go test ./... -coverpkg all

or

go test ./... -coverpkg foo/qux

you get init calls from the qux package happening in the foo test suite, when qux is not imported. Without coverpkg this package does not get imported.

I have made a repro with more information in the README here: https://github.com/benmoss/go-coverpkg-inits

What did you expect to see?

The qux package init function should only be run during tests that import it.

What did you see instead?

It is executed even when it is not imported.

@benmoss benmoss changed the title cmd/cover: -coverpkg makes multiple packages run in the same process cmd/cover: -coverpkg causes init methods to be run for non-imported packages May 17, 2021
@benmoss
Copy link
Author

benmoss commented May 17, 2021

You can also use the repro from #21283, https://github.com/AZavorotnii/go-coverpkg-initissue

$ go test -coverpkg all -v
foo.
main.
main main.
foo Foo.
testing: warning: no tests to run
PASS
coverage: 13.8% of statements in all
ok      go-coverpkg-initissue   0.001s
go test -coverpkg all -v ./...
foo.
main.
bar.  <-- here is issue
main main.
foo Foo.
testing: warning: no tests to run
PASS
coverage: 14.3% of statements in all
ok      go-coverpkg-initissue   (cached)        coverage: 14.3% of statements in all [no tests to run]
?       go-coverpkg-initissue/bar       [no test files]
?       go-coverpkg-initissue/foo       [no test files]

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 18, 2021
@dmitshur dmitshur added this to the Backlog milestone May 18, 2021
@dmitshur
Copy link
Contributor

Not sure if the problem is in cmd/cover or how cmd/go uses it, but it looks more like the latter. CC @matloob, @jayconrod, @matloob.

@jayconrod
Copy link
Contributor

This is a consequence of the go command linking all packages with coverage instrumentation into the test binary, even if they wouldn't be linked otherwise.

I'll close this as a duplicate of #23910, which is the root problem.

@golang golang locked and limited conversation to collaborators May 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants