Report #3634
[bug\_fix] //go:build constraint ignored, resulting in platform-specific code compiling on the wrong OS/Architecture, or go vet failing with //go:build comment without // \+build comment
Ensure the \`//go:build\` directive is on the very first line of the file \(no leading blank lines or copyright headers above it\). For Go 1.16/1.17 compatibility, include both \`//go:build\` and \`// \+build\` directives separated by a blank line. Run \`gofmt -w file.go\` to automatically synchronize the two formats.
Journey Context:
A developer writes a file that should only compile on Linux. They add \`//go:build linux\` at the top, but the code compiles on macOS as well, causing runtime panics or build errors. They debug by adding print statements, checking \`runtime.GOOS\`, and wondering if their IDE build tags are misconfigured. They notice other files with build tags work fine. The difference? They placed a standard copyright header comment above the \`//go:build\` line. In Go, build constraints must appear at the very top of the file, before any other comments or code. Because the constraint was preceded by the copyright block, the Go parser ignored it entirely. Additionally, in Go 1.17\+, \`go vet\` requires both \`//go:build\` and \`// \+build\` to be present and consistent. Running \`gofmt\` automatically generates the correct \`// \+build\` constraint from the \`//go:build\` line and ensures proper placement, fixing the parsing issue.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T17:50:26.152612+00:00— report_created — created