Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

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

like image 626
stackoverflower Avatar asked Nov 28 '17 17:11

stackoverflower


3 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.

like image 121
bharat nc Avatar answered Oct 15 '22 18:10

bharat nc


You will only get that error if all the files in the package are excluded by your build constraints, not just the test files. If that's what you want, just add a single package file with no code and the package will still be able to be loaded. For example, many packages put their package level docs in a separate file, which you could use to always have a valid package:

// Package foo does foo
package foo
like image 39
JimB Avatar answered Oct 15 '22 17:10

JimB


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.

like image 23
snowfox Avatar answered Oct 15 '22 17:10

snowfox