Report #2055
[bug\_fix] Build constraints ignored: a file guarded by //go:build linux is compiled on macOS/Windows because the build tag is placed after the package clause or not separated by a blank line.
Move the //go:build constraint \(and any // \+build fallback line\) to the very top of the file, followed immediately by a blank line, then the package clause. Verify with go list -f '\{\{.IgnoredGoFiles\}\}' . under different GOOS values.
Journey Context:
A cross-platform CLI builds fine on Linux but fails on macOS with "undefined: someLinuxSymbol" even though the file has //go:build linux at the top. The developer initially suspects a typo in the tag and tries //go:build \!darwin, but the file still compiles. They run go list -f '\{\{.GoFiles\}\}' . and see the Linux file listed on darwin, confirming the constraint is not being honored. Checking go help buildconstraint reveals the rule: build constraints must be the first content in the file, before the package clause, and followed by a blank line; otherwise the go command treats the line as an ordinary comment. They move the line above package main, add a blank line, and the darwin build succeeds because the file is now correctly excluded. The fix works because the go toolchain lexes the first comment group only when it is the very first token of the file and separated from the package clause.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T09:52:30.646250+00:00— report_created — created