Home › Go

build constraints exclude all Go files in /path/to/package — how to fix

package example.com/app/internal/sys: build constraints exclude all Go files in /home/me/app/internal/sys

Quick fix: See why with go list -f '{{.IgnoredGoFiles}}' ./internal/sys and go env GOOS GOARCH CGO_ENABLED. Then build for the right target (GOOS=linux go build), pass the needed tag (go build -tags integration), enable cgo (CGO_ENABLED=1), or add a file that builds on every platform.

What it means

Before compiling, Go decides which files belong to the current build. A file is left out when:

If that leaves zero files, the package can't be built and you get this error.

Find out which files are ignored

go env GOOS GOARCH CGO_ENABLED
go list -f '{{.GoFiles}} | ignored: {{.IgnoredGoFiles}}' ./internal/sys
head -3 internal/sys/*.go        # look at the //go:build lines

Cause 1: the package only supports other platforms

//go:build linux

package sys

On macOS or Windows this file is excluded. If every file in the package looks like that, building on your machine fails.

Fix: build for the target platform:

GOOS=linux GOARCH=amd64 go build ./...

PowerShell:

$env:GOOS="linux"; $env:GOARCH="amd64"; go build ./...

Or, if the package should work everywhere, add an implementation for the other platforms, e.g. sys_other.go:

//go:build !linux

package sys

func Setup() error { return nil }

Make sure every exported function exists in both variants.

Cause 2: custom build tags not passed

Files guarded by your own tags are only included when you ask for them:

//go:build integration
go test -tags integration ./...
go build -tags "integration debug" ./...

Cause 3: cgo is disabled

Files with import "C" are excluded when CGO_ENABLED=0. That's the default when cross-compiling and when no C compiler is found, and many Dockerfiles set it explicitly.

go env CGO_ENABLED
CGO_ENABLED=1 go build ./...

Enabling cgo needs a C compiler (gcc/clang) for the target. Alternatively, choose a pure-Go library (for SQLite, for example, modernc.org/sqlite instead of github.com/mattn/go-sqlite3).

Cause 4: only test files, or the wrong directory

A directory containing only _test.go files can be tested but not built with go build. Also check that you didn't point the command at a folder of examples or generated code that is guarded with //go:build ignore:

//go:build ignore

Files with this line are never built; they're meant to be run with go run file.go.

Cause 5: malformed build line

A build constraint must come before the package clause and be followed by a blank line. Typos such as // go:build linux (space after //) are treated as normal comments, while a wrong expression like //go:build linux && windows excludes the file everywhere. Run go vet to catch malformed constraints.

In VS Code / gopls

The editor may show this error for files of another platform or tag even though your build works. Tell gopls which tags or platform to use in settings.json:

"gopls": {
  "buildFlags": ["-tags=integration"]
},
"go.toolsEnvVars": { "GOOS": "linux" }

Last updated 2026-11-29.