Using Go 1.2 cover tool with GVM

2.2k views Asked by At

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.

4

There are 4 answers

0
Pavan Kumar Sunkara On BEST ANSWER

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.

8
VonC On

Double check the value of $GOPATH, as set by gvm.

I would try setting $GOPATH manually just for testing to /home/bill (with a symlink src->dev), just to see if a go test -cover produces files a cover.out with the correct file path in it.

0
Daniel YC Lin On

Have you try to put a issue in gvm's developer site? https://github.com/moovweb/gvm (I'm not sure is this the major site)

0
gepoch On

Here's the way to get all of the advantages of GVM without having to ruin your ideal go development environment as described here, and without having to resort to clunky special-case symlink hacks.

Suppose I've set all of my development up according to the standard in ~/go (so package foo in my github would be in ~/go/github.com/gepoch/foo)

First of all, we're going to make a special-use pkgset that will happily reference our development environment. Simply run:

$ gvm pkgset create dev

This will add the pkgset. Next, we can do some customization on where exactly it puts the go path. Run:

$ gvm pkgenv dev

You should see your favorite text editor pop open with a bunch of environment variable definitions. Simply change the GOPATH entry to include your dev root! For example, I change this:

export GOPATH; GOPATH="$GVM_ROOT/pkgsets/go1.2/dev"

Into this:

export GOPATH; GOPATH="$GVM_ROOT/pkgsets/go1.2/dev:$HOME/go"

Additionally, have gvm set up your path correctly by changing this:

export PATH; PATH="${GVM_ROOT}/pkgsets/go1.2/global/bin:${GVM_ROOT}/gos/go1.2/bin:${GVM_OVERLAY_PREFIX}/bin:${GVM_ROOT}/bin:${PATH}"

into this:

export PATH; PATH="${GVM_ROOT}/pkgsets/go1.2/global/bin:${GVM_ROOT}/gos/go1.2/bin:${GVM_OVERLAY_PREFIX}/bin:${GVM_ROOT}/bin:${PATH}:$HOME/go/bin"

Restart your terminal, and that's it! Whenever you run $ gvm pkgset use dev you'll have easy access to your dev environment.

That means (among many other things) that this works as intended:

$ go test -coverprofile=coverage.out github.com/gepoch/foo
$ go tool cover -html=coverage.out

You can add this to any pkgset environment that you wish, for easy access to the dev tree.