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: method "init" should not be called for non-imported packages #21283

Closed
azavorotnii opened this issue Aug 2, 2017 · 1 comment
Closed
Milestone

Comments

@azavorotnii
Copy link

@azavorotnii azavorotnii commented Aug 2, 2017

What did you do?

In short: if package is not imported in project, but present in "coverpkg" list, its "init" method will be called. It is not expected that execution behavior depends on coverage options.

In details
I put full example here:
https://github.com/AZavorotnii/go-coverpkg-initissue

Assuming we have file structure:

.
├── bar
│   └── bar.go
├── foo
│   └── foo.go
└── main_test.go

Where main_test.go imports only foo package:

package main

import (
        "fmt"

        "github.com/AZavorotnii/go-coverpkg-initissue/foo"
        "testing"
        "os"
)

func init() {
        fmt.Println("main.")
}

func TestMain(m *testing.M) {
        fmt.Println("main main.")
	foo.Foo()

        os.Exit(m.Run())
}

Where foo.go:

package foo

import "fmt"

func init() {
        fmt.Println("foo.")
}

func Foo() {
        fmt.Println("foo Foo.")
}

Where bar.go:

package bar

import "fmt"

func init() {
        fmt.Println("bar.  <-- here is issue")
}

func Bar() {
        fmt.Println("bar Bar.")
}

Build test binary with command:

go test -c -o t main_test.go -coverprofile p.out -covermode atomic --coverpkg github.com/AZavorotnii/go-coverpkg-initissue/foo,github.com/AZavorotnii/go-coverpkg-initissue/bar

What did you expect to see?

Expect "init" calls only from imported packages, as without code coverage:

foo.
main.
main main.
foo Foo.
testing: warning: no tests to run
PASS

What did you see instead?

Init method of "bar" package is executed, even this package is not imported:

foo.
main.
bar.  <-- here is issue
main main.
foo Foo.
testing: warning: no tests to run
PASS
coverage: 75.0% of statements in github.com/AZavorotnii/go-coverpkg-initissue/foo, github.com/AZavorotnii/go-coverpkg-initissue/bar

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

go version go1.8.3 darwin/amd64

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/andriizavorotnii/work"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.8.3/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.8.3/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/ph/94zmg73j2kn_h7gg01c2fmbr0000gp/T/go-build687947162=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
@gopherbot gopherbot added this to the Unreleased milestone Aug 2, 2017
@rsc rsc changed the title x/tools/cmd/cover: method "init" should not be called for non-imported packages cmd/cover: method "init" should not be called for non-imported packages Nov 10, 2017
@rsc rsc modified the milestones: Unreleased, Go1.10 Nov 10, 2017
@gopherbot
Copy link

@gopherbot gopherbot commented Nov 10, 2017

Change https://golang.org/cl/76876 mentions this issue: cmd/go: redefine -coverpkg to be a pattern list

@gopherbot gopherbot closed this in e33794f Nov 10, 2017
@golang golang locked and limited conversation to collaborators Nov 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.