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

Closed
ChrisHines opened this issue Feb 18, 2018 · 6 comments
Labels
GoCommand cmd/go NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@ChrisHines
Copy link
Contributor

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 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 Mar 28, 2018
@ianlancetaylor
Copy link
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.

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 28, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Mar 28, 2018
@rsc
Copy link
Contributor

rsc commented Apr 18, 2018

/cc @bcmills

@nvartolomei
Copy link

Sounds like #27336

@forsaken628
Copy link

forsaken628 commented Dec 18, 2020

test

go test -v -coverpkg=./... ./...

-- go.mod --
module test
-- main.go --
package main

import "flag"

func init() { flag.Parse() }
-- bar/bar.go --
package bar
-- bar/bar_test.go --
package bar
--- FAIL: TestScript (0.01s)
    --- FAIL: TestScript/test_cover_main_init (1.22s)
        script_test.go:213: 
            > go test -v -coverpkg=./... ./...
            [stdout]
            ?           test    [no test files]
            flag provided but not defined: -test.testlogfile
            Usage of $WORK/tmp/go-build1087252013/b037/bar.test:
            FAIL        test/bar        0.779s
            FAIL
            [exit status 1]
            FAIL: testdata/script/test_cover_main_init.txt:1: unexpected command failure
            
FAIL
FAIL    cmd/go  2.598s
FAIL

link
https://golang.org/cl/278892

maybe skip main package without test files

Index: src/cmd/go/internal/test/test.go
<+>UTF-8
===================================================================
diff --git a/src/cmd/go/internal/test/test.go b/src/cmd/go/internal/test/test.go
--- a/src/cmd/go/internal/test/test.go	(revision dfaf69eccc66f44e9ca150d062baadf53c574438)
+++ b/src/cmd/go/internal/test/test.go	(revision cfab536ac976f7597165cfac002c80cc6613ad5f)
@@ -731,6 +731,11 @@
 				continue
 			}

+			// Don't call main.init, if no test file.
+			if p.Name == "main" && len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
+				continue
+			}
+
 			if haveMatch {
 				testCoverPkgs = append(testCoverPkgs, p)
 			}

@slinkydeveloper
Copy link

slinkydeveloper commented May 19, 2021

If you want to try a bigger project to reproduce the issue, clone https://github.com/knative/eventing and run the test with:

go test -race -coverpkg=./... ./...

For more details: knative/pkg#2113

Arista-Jenkins pushed a commit to aristanetworks/cloudvision-go that referenced this issue May 2, 2022
gocover is doing tricks to be able to process coverage across packages
and defining same flags globally breaks this:
golang/go#23910
golang/go#27336

A simple solution is to not make those flags global.

Change-Id: I1a4fda3bcca7132e54bb1755a1a5bde52d01c340
Arista-Jenkins pushed a commit to aristanetworks/cloudvision-go that referenced this issue Dec 1, 2022
gocover is doing tricks to be able to process coverage across packages
and defining same flags globally breaks this:
golang/go#23910
golang/go#27336

A simple solution is to not make those flags global.

Change-Id: I1a4fda3bcca7132e54bb1755a1a5bde52d01c340
@thanm
Copy link
Contributor

thanm commented May 18, 2023

FYI, we've moved away from the model in which all -coverpkg packages are force-imported into every test, this was done as part of the coverage redesign in Go 1.20. This is spawn a few bugs (ex: #58770) but I have a fix out for review that should resolve that (https://go.dev/cl/495452). Closing this bug out, please reopen if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go 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

10 participants