Introduction
Go's build cache stores compiled package artifacts to speed up subsequent builds. After an OS upgrade, Go version change, or filesystem issue, the cache can contain artifacts built with incompatible toolchains or library versions, leading to cryptic linker errors and undefined symbol failures.
This issue manifests as build failures that seem unrelated to any code changes, making it particularly confusing to diagnose.
Symptoms
- go build fails with "undefined: someSymbol" after OS upgrade with no code changes
- Error shows linker failures for standard library or CGO symbols
- Build works on a clean machine but fails on the upgraded system
Common Causes
- Build cache contains artifacts compiled with old system libraries
- Go version upgraded but old cached artifacts reference incompatible ABI
- CGO-cached objects linked against old system headers
Step-by-Step Fix
- 1.Clean the build cache: Remove all cached build artifacts.
- 2.```bash
- 3.# Clean everything:
- 4.go clean -cache -modcache -i -r
# Or just the build cache: go clean -cache
# Verify cache location: go env GOCACHE # Typically: ~/.cache/go-build on Linux # ~/Library/Caches/go-build on macOS
# Then rebuild: go build ./... ```
- 1.Clean module cache if dependencies are affected: Re-download all module dependencies.
- 2.```bash
- 3.# Clean module cache:
- 4.go clean -modcache
# Re-download dependencies: go mod download
# Verify module integrity: go mod verify
# Then rebuild: go build ./... ```
- 1.Set GOCACHE to a fresh directory: Use a new cache location instead of cleaning.
- 2.```bash
- 3.# Use a temporary fresh cache:
- 4.GOCACHE=$(mktemp -d) go build ./...
# Or set a permanent new cache location: export GOCACHE=$HOME/.cache/go-build-fresh go build ./... ```
- 1.Check for stale CGO objects: CGO compilation depends on system toolchain.
- 2.```bash
- 3.# Clean CGO-related cache:
- 4.CGO_ENABLED=1 go clean -cache
- 5.go clean -cache
# Rebuild with explicit CGO: CGO_ENABLED=1 go build -v ./...
# Check CGO environment: go env CGO_ENABLED CGO_CFLAGS CGO_LDFLAGS ```
Prevention
- Run go clean -cache after OS upgrades or Go version changes
- Add cache cleaning to your upgrade checklist
- Use GOMODCACHE and GOCACHE environment variables for cache isolation in CI
- Run go mod verify periodically to detect corrupted module downloads