gocode-coveragegvm

Using Go 1.2 cover tool with GVM


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.


Solution

  • 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

    $ gvm use go1.2
    Now using version go1.2
    
    $ echo $GOPATH
    /usr/local/lib/gvm/pkgsets/go1.2/global
    
    $ echo $GOROOT
    /usr/local/lib/gvm/gos/go1.2
    

    To fix the issue, I did this

    $ mkdir -p $GOPATH/src/github.com/pksunkara
    $ ln -s /home/pksunkara/coding/alpaca $GOPATH/src/github.com/pksunkara/alpaca
    

    Basically I have to link the current working folder into the $GOPATH/src folder and the resultant package path for alpaca became github.com/pksunkara/alpaca.

    Now, the go test & cover works as following

    $ go test -coverprofile=coverage.out github.com/pksunkara/alpaca
    $ go tool cover -html=coverage.out
    

    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 the model directory to $GOPATH/src/model and then you will have a package path named model which you can import using import "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 as import "bill.com/server/model". Similarily with ./query and ./util you have.

    If you still have doubts, please ask. I will try to explain more.