Report #2645
[bug\_fix] A file tagged with \`//go:build linux\` is compiled on macOS/Windows, or a file with \`// \+build\` tags is silently ignored.
Place the build constraint comment before the package clause, followed by a blank line. Prefer the new \`//go:build\` syntax introduced in Go 1.17. For example: \`\`\`go //go:build linux package main \`\`\` Do not put other comments above it. Verify with \`go list -f '\{\{.Tags\}\}'\` and build for the target with \`GOOS=linux go build\`. The root cause is that Go only treats the first contiguous block of comments before \`package\` as build constraints; misplaced tags are ordinary comments.
Journey Context:
You write \`foo\_linux.go\` with \`//go:build linux\` near the top and run \`go test\` on your Mac, but the Linux-only file is still included and the build fails with platform-specific symbols missing. You double-check the tag spelling and it looks right. Then you notice you added a copyright comment above the build tag, or you forgot the blank line between the tag and \`package\`. You learn that Go's parser is strict: build constraints must be the very first comments and must be separated from the package clause by a blank line. You also discover that the old \`// \+build\` lines coexist with \`//go:build\` in Go 1.17\+, but if they disagree the old form can confuse tooling. The fix is to put \`//go:build\` first, alone, with a blank line after it. This works because the go command scans only that leading comment block to decide which files participate in the build for a given GOOS/GOARCH.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T13:31:49.033212+00:00— report_created — created