I'm using GVM to manage my go installations and paths and everything seems to work just fine - I can run tests and produce builds. I'm now trying to produce a code coverage file and am having some difficulty.
My package is defined in /home/bill/dev/server/model
.
When I run:
$ go test -cover -coverprofile cover.out
The tests run successfully and a coverage file is produced. However, the paths in the coverage file look like this:
_/home/bill/dev/server/model/activity.go:19.34,21.2 1 1
And I get the following error when I try to create an html cover file:
$ go tool cover -html=cover.out
cover: can't find "activity.go": cannot find package "_/home/bill/dev/server/model/" in any of:
/home/bill/.gvm/gos/go1.2/src/pkg/_/home/bill/dev/server/model (from $GOROOT)
/home/bill/.gvm/pkgsets/go1.2/global/src/_/home/bill/dev/server/model (from $GOPATH)
How do I fix this?
Additional details
~ gvm use go1.2
Now using version go1.2
~ echo $GOPATH
/home/bill/.gvm/pkgsets/go1.2/global
~ echo $GOROOT
/home/bill/.gvm/gos/go1.2
I tried manually setting my $GOPATH but that didn't change the cover.out file. I also tried manually editing the cover.out file but I can't figure out what paths it actually wants. In the default configuration shown above, running go test
runs as expected.
Attempting to fix GOPATH
~ export GOPATH=/home/bill/dev/
~ ln -s /home/bill/dev/server /home/bill/.gvm/gos/go1.2/src
~ go test
cannot find package "code.google.com/p/go.crypto/pbkdf2" in any of:
/home/bill/.gvm/gos/go1.2/src/pkg/code.google.com/p/go.crypto/pbkdf2 (from $GOROOT)
/home/bill/dev/src/code.google.com/p/go.crypto/pbkdf2 (from $GOPATH)
../util/log.go:4:2: cannot find package "github.com/kr/pretty" in any of:
/home/bill/.gvm/gos/go1.2/src/pkg/github.com/kr/pretty (from $GOROOT)
/home/bill/dev/src/github.com/kr/pretty (from $GOPATH)
These are additional dependencies that I previously downloaded using go get
. They end up in /home/bill/.gvm/pkgsets/go1.2/global/src which the $GOPATH used to point to. So I changed GOPATH
~ export GOPATH=/home/bill/dev/:/home/bill/.gvm/pkgsets/go1.2/global
So that the tests run again, but the cover.out file still has the same directories in it and still gives me the same error.
I had the same problem a month ago. I solved it by using the following steps.
My package name is called
alpaca
My working directory (code) is
/home/pksunkara/coding/alpaca
To fix the issue, I did this
Basically I have to link the current working folder into the
$GOPATH/src
folder and the resultant package path foralpaca
becamegithub.com/pksunkara/alpaca
.Now, the go test & cover works as following
THIS IS IMPORTANT
I stumbled a lot to fix this. I have attempted all kind of things including the ones you attempted. I understood the problem by reading about code organization in golang which should be a must read for everyone working with go.
The code organization mentioned here is very important to work with golang.
Package paths are important for golang. And you should never use local path when importing in golang. They will work but it is not recommended.
Let's assume your package name is
model
. You can simply link themodel
directory to$GOPATH/src/model
and then you will have a package path namedmodel
which you can import usingimport "model"
. But to avoid collisions, go recommends using a bigger package path name.I would recommend you to link it to
$GOPATH/src/bill.com/server/model
and import it asimport "bill.com/server/model"
. Similarily with./query
and./util
you have.If you still have doubts, please ask. I will try to explain more.