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/go: -coverpkg packages imported by all tests, even ones that otherwise do not use it #23910

Open
ChrisHines opened this Issue Feb 18, 2018 · 3 comments

Comments

Projects
None yet
5 participants
@ChrisHines
Contributor

ChrisHines commented Feb 18, 2018

Please answer these questions before submitting your issue. Thanks!

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

go version go1.10 windows/amd64

Does this issue reproduce with the latest release?

Yes.

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

set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Chris\AppData\Local\go-build
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Chris\Go
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\Chris\AppData\Local\Temp\go-build675280250=/tmp/go-build -gno-record-gcc-switches

What did you do?

>go get github.com/ChrisHines/coverpkgtest

>cd $GOPATH/github.com/ChrisHines/coverpkgtest

>go run main.go
9

>go test ./...
?       github.com/ChrisHines/coverpkgtest      [no test files]
ok      github.com/ChrisHines/coverpkgtest/subdir       (cached)

>go test -coverpkg=./... .
?       github.com/ChrisHines/coverpkgtest      [no test files]

>go test -coverpkg=./... ./subdir
ok      github.com/ChrisHines/coverpkgtest/subdir       0.052s  coverage: 0.0% of statements in ./...

>go test -coverpkg=. ./subdir
warning: no packages being tested depend on matches for pattern .
ok      github.com/ChrisHines/coverpkgtest/subdir       0.057s  coverage: 0.0% of statements in .

>go test -coverpkg=. ./...
?       github.com/ChrisHines/coverpkgtest      [no test files]
panic: open tmpl.txt: The system cannot find the file specified.

goroutine 1 [running]:
text/template.Must(0x0, 0x5c8ba0, 0xc042064390, 0x1)
        C:/Go/src/text/template/helper.go:23 +0x5b
FAIL    github.com/ChrisHines/coverpkgtest/subdir       0.056s

>go test -coverpkg=./... ./...
?       github.com/ChrisHines/coverpkgtest      [no test files]
panic: open tmpl.txt: The system cannot find the file specified.

goroutine 1 [running]:
text/template.Must(0x0, 0x5c8ba0, 0xc042074390, 0x1)
        C:/Go/src/text/template/helper.go:23 +0x5b
FAIL    github.com/ChrisHines/coverpkgtest/subdir       0.055s

What did you expect to see?

I expected the tests to pass regardless of how they are launched by the go tool.

What did you see instead?

Using -coverpkg=./... in combination with the test package pattern ./... caused a panic for a missing file even though the file exists. It is also confusing that running the tests on individual packages or without -coverpkg=./... does not panic.

@ianlancetaylor ianlancetaylor changed the title from cmd/go: test -coverpkg=./... ./... incompatible with template.Must for local files. to cmd/go: -coverpkg packages imported by all tests, even ones that otherwise do not use it Mar 28, 2018

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Mar 28, 2018

I think the problem is that cmd/go rewrites tests to import all packages listed in -coverpkgs. It then runs the test for package subdir in the subdir directory. In your case the main package is listed in -coverpkgs, so the test imports it. The only thing that is run in the main package is the initializers. In your program that is what fails: the initializer loads a relative file and can only run in the main package directory, not in the subdirectory.

Not sure what the right fix is.

@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Mar 28, 2018

@rsc

This comment has been minimized.

Contributor

rsc commented Apr 18, 2018

/cc @bcmills

@nvartolomei

This comment has been minimized.

nvartolomei commented Nov 28, 2018

Sounds like #27336

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment