How to Fix "go build: command ignored in directory without Go files"
Quick answer
You run go build and nothing happens β no binary, no error output that points at your actual code, just a message saying the directory has no Go files. This...
You run go build and nothing happens β no binary, no error output that points at your actual code, just a message saying the directory has no Go files. This means the compiler looked in the directory you're standing in and genuinely found nothing it recognizes as buildable Go source.
The Problem
Running the build from what looks like your project root produces this instead of a compiled binary:
$ go build .
build .: no non-test Go files in /home/user/projects/myapp
Or, depending on the Go version and exact command used:
$ go build ./...
no Go files in /home/user/projects/myapp/cmd
This is confusing when you can clearly see .go files sitting right there with ls.
Why It Happens
Go is stricter than it looks about what counts as a "buildable" file in a given directory. The message appears for a handful of specific reasons:
- Wrong working directory. Your source files are in a subdirectory (like
cmd/server), and you're running the build command from the repo root where only non-Go files (README, config,go.mod) live. - All files are test files. A directory containing only
_test.gofiles doesn't count as having buildable Go files for a plaingo buildβ test files are excluded from a normal build. - Build constraints exclude every file. Files with build tags like
//go:build linuxor a_windows.gosuffix get excluded entirely when building for a different OS/architecture than the tag specifies. - Files aren't named with a
.goextension β an easy mistake when renaming files manually or after a bad merge. - The directory is genuinely empty of source β it might only contain a subpackage's compiled artifacts, generated files that got deleted, or nothing at all yet.
The Fix
First, confirm exactly where you are and what's actually in the directory:
pwd
ls -la *.go
If there are no .go files at the top level, look for the actual package directory β Go projects commonly put the entry point under cmd/:
find . -name "*.go" -not -path "./vendor/*"
Build from the directory that actually contains the files, or point go build at it explicitly instead of changing directories:
go build ./cmd/server
If the directory has files but they're all _test.go, that's expected behavior, not a bug β run tests instead of a build:
go test ./...
If you suspect a build constraint is excluding files for your current OS/architecture, check the top of each .go file for a build tag:
head -3 server_windows.go
//go:build windows
// +build windows
A file like server_windows.go only builds when targeting Windows β if you're building on Linux without an equivalent server_linux.go or a platform-agnostic file, the directory can end up with zero eligible files for your current target. Check what you're actually building for:
go env GOOS GOARCH
Rename or add a corresponding file for your platform, or remove the build constraint if it's not actually needed for cross-platform separation.
Still Not Working?
If everything looks correct β right directory, real .go files, no unexpected build tags β check whether the files accidentally ended up with a different extension during a copy, rename, or bad merge (.go.bak, .go.orig, or no extension at all are common culprits after a messy merge conflict resolution):
ls -la | grep -i go
If you spot a stray extension, rename the file back to a plain .go suffix and try the build again β Go will silently ignore anything that doesn't match its exact naming and tagging rules rather than raising a clearer error about it.
It's also worth double-checking your .gitignore and any recent commits if this started happening right after pulling changes from a teammate. A misconfigured ignore rule can prevent .go files from ever being committed in the first place, so what looks like a local build problem is actually a missing file that never made it into version control:
git status
git log --oneline -- cmd/server/main.go
If git log shows no history at all for a file you expect to exist, or git status shows it as untracked on a fresh clone, check .gitignore for an overly broad pattern like a stray *.go line or an accidental directory-wide exclusion that's silently swallowing source files before they're ever committed:
cat .gitignore | grep -i go
Finally, remember that go build and go vet both respect the same build constraint rules, so if you're debugging a "no Go files" error specifically tied to platform tags, go vet can sometimes surface a clearer explanation of which files were excluded and why, since it walks the same file set the compiler would:
go vet ./...