go: cmd/go: go test -cover fails when xtest imports main which imports package being tested
I’m using
go version
go version go1.6 darwin/amd64
Go env
$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/hvn/Github/gomeo"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GO15VENDOREXPERIMENT="1"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"
when I run go test with coverage (-cover), it failed to import the package that it was okay when I ran test without -cover. I added debug log to compile and rebuild go, I got those log:
compile: LOG: handling import: git.example.com/repo/t-p
compile: LOG: alldir: $WORK
compile: LOG: alldir: /Users/hvn/Github/gomeo/pkg/darwin_amd64
compile: LOG: checking file $WORK/git.example.com/repo/t-p.a
compile: LOG: checking file /Users/hvn/Github/gomeo/pkg/darwin_amd64/git.example.com/repo/t-p.a
./kickkick.go:7: can't find import: "git.example.com/repo/t-p"
AFAIU, it should import from /Users/hvn/Github/gomeo/pkg/darwin_amd64/git.example.com/repo/_test/git.example.com/repo/t-p.a, no?
https://github.com/golang/go/blob/release-branch.go1.6/src/cmd/go/test.go#L697
// That is, if the package under test is unicode/utf8,
// then the normal place to write the package archive is
// $WORK/unicode/utf8.a, but we write the test package archive to
// $WORK/unicode/utf8/_test/unicode/utf8.a.
// We write the external test package archive to
// $WORK/unicode/utf8/_test/unicode/utf8_test.a.
I removed this line, rebuilt go and it worked https://github.com/golang/go/blob/release-branch.go1.6/src/cmd/go/test.go#L850
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 18 (10 by maintainers)
Reproduced in https://go-review.googlesource.com/31661.
The problem is package p (a main package) imports package q, and package q_test imports p. The rebuilding for coverage does not handle rebuiling the coverage-aware p correctly for that import.
I don’t remember why we allow tests to import main packages. That may not have been intentional, in which case the bug is that the test even runs in non-cover mode. See also #17475.