go test fails with "can't load package build constraints exclude all Go files"

Viewed 15054

When I run this command

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

go fails with this for some packages that has all tests marked with // +build !integration

can't load package: build constraints exclude all Go files

Is there a way I can make go test ignore those packages in this case, instead of failing? Thanks

4 Answers

For whatever it's worth : I was seeing similar errors while building my code. In my case, my main file had // +build go1.13 at the top of it and I was trying to get the file built using go1.12.x. Removing +build go1.13 fixed the issue.

The package you are importing might contain some source files with a conditional complication flag, which is a directive you may find at the top of the files like:

//+build linux darwin windows
package main

import "fmt"

func main() {
    fmt.Println("hello")
}

The +build directive followed by one or multiple platforms indicates where this source code is supposed to be compiled on.

Therefore, you can simply try removing this directive to resolve the failure. It worked for me.

Following on from snowfox's answer, if you can't remove the directive (becuase it was likely put there for a reason), you can still run the test by using the tag that was applied to that file (more info here).

If your file start looks like this:

//go:build bar
package foo

Then you can run the test by running go test ./foo_test.go --tags=bar

Related