A Makefile for Go Vendor Experiment

I recently started using the new Go vendor experiment in one of my projects.

I really love not having rewritten go imports in my code. I always thought that was a horrible hack to solve the problem and it was not very clear how to really use your code that way.

But with the new vendor directory things like go test -v ./... will try running all the tests in your vendor directory!

I kind of wondered if maybe I should simply lower the number of packages I have, but decided against it in favor of the solution on the page for Godeps.

NOTE: There is a “bug” in the vendor experiment that makes using ./… with the go tool (like go install) consider all packages inside the vendor directory: https://github.com/golang/go/issues/11659. As a workaround you can do:

$ go <cmd> $(go list ./... | grep -v /vendor/)

So basically you need to run this to get the normal results I had from the go test command recommended by seemingly every project.

go test -v -race $(go list ./... | grep -v /vendor/)

Which is nice cause it’s a solution, but I hated it so I made a make file for this. It was a bit tricky for me since I don’t usually use make files and this isn’t a very well documented use case.

ENV=GO15VENDOREXPERIMENT=1
PACKAGES=$(shell go list ./... | grep -v /vendor/)

test:
	$(ENV) go test -v -race $(PACKAGES)

First off, since we’re using the vendor experiment we need to pass the flags since we could be running this anywhere and it’s possible the proper environment variables haven’t been set.

Next I used the shell-thingy (which was only discovered to work like that after trial and error so the name is unknown to me) to tell make to store the result of that shell expression which contains all the packages we want go commands to work with from before excluding the vendor directory.

We then run the command with the environment variables and correct packages and the tests run!