How to Fix: 'build constraints exclude all Go files' in Go

beginner🔷 Go2026-06-25| Go 1.17+, Linux, macOS, Windows

Error Message

build constraints exclude all Go files in /home/user/project/pkg
#golang#go-build#devops#troubleshooting

The ProblemYou run go build ./... expecting a smooth compilation, but the compiler stops dead. It points to a specific directory and claims it can't find any Go files, even though you can see them right there in your editor. This error is Go's way of saying: "I found files, but your current settings told me to ignore every single one of them."

Why Go is Skipping Your FilesGo uses build constraints (or build tags) to decide which files belong in a specific binary. If you're building on macOS but your files are strictly for Windows, Go filters them out. When that filter leaves the compiler with zero files, it throws this error. Most often, the culprit is one of these four issues:

  • OS/Arch Suffixes: Filenames like disk_windows.go are ignored when building on Linux or macOS.- Strict Build Tags: A file starting with //go:build integration won't compile during a standard run.- The 'Test-Only' Trap: The folder only contains *_test.go files. Go needs at least one regular .go file to define a package.- Reserved Prefixes: Go completely ignores any directory starting with a dot (.git) or an underscore (_vendor).## How to Fix It### 1. Check for Platform SuffixesIf your /pkg directory only contains service_windows.go and you are running on a MacBook (Darwin), the build will fail. Go's toolchain automatically looks for suffixes like _linux, _windows, or _amd64.
# Check your directory content
ls pkg/
# Output: logic_windows.go  logic_windows_test.go

To fix this, create a platform-agnostic file like logic.go for shared code. Alternatively, add a logic_darwin.go to support your current environment.

2. Verify Build TagsModern Go (1.17+) uses the //go:build syntax at the top of files. Check if your files are hidden behind custom tags:

//go:build pro_version

package pkg

If this tag exists, a standard go build will skip the file. You must explicitly include the tag in your command:

go build -tags=pro_version ./...

Also, watch out for the //go:build ignore directive. Developers often use this for scripts or tools they don't want included in the main binary.

3. The "Empty Package" MistakeGo requires every package directory to have at least one non-test file. If your folder looks like this:

pkg/
└── logic_test.go

The compiler will complain because there is no logic.go to provide context for the tests. Simply adding a file with package pkg is enough to satisfy the compiler.

The Pro Debugging TipDon't guess which files Go is seeing. Use the go list tool to inspect the build system's perspective. Run this command from your project root:

go list -f '{{.GoFiles}}' ./pkg

If it returns an empty list [], your constraints are still too restrictive. If it returns [logic.go], you've solved the problem. You can also check which files are being ignored and why by adding the -json flag and looking at the IgnoredGoFiles field.

Related Error Notes