Skip to content

cmd/go: -coverpkg packages imported by all tests, even ones that otherwise do not use it #23910

Closed
@ChrisHines

Description

@ChrisHines

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.

Activity

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

ianlancetaylor commented on Mar 28, 2018

@ianlancetaylor
Contributor

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.

added
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Mar 28, 2018
added this to the Go1.11 milestone on Mar 28, 2018
rsc

rsc commented on Apr 18, 2018

@rsc
Contributor
modified the milestones: Go1.11, Go1.12 on Jul 2, 2018
nvartolomei

nvartolomei commented on Nov 28, 2018

@nvartolomei

Sounds like #27336

modified the milestones: Go1.12, Go1.13 on Nov 28, 2018

29 remaining items

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeGoCommandcmd/goNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @rsc@jayconrod@andybons@nvartolomei@ChrisHines

        Issue actions

          cmd/go: -coverpkg packages imported by all tests, even ones that otherwise do not use it · Issue #23910 · golang/go