cmd/dist: `{CC,CXX}_FOR_TARGET` is incorrectly embedded into output when cross-compiling
Go version
go version go1.26.7 linux/arm64
Output of go env in your module/workspace:
AR='ar'
CC='/nix/store/0hvd25jcaxcq45gkrgwqmd4gfqixg99h-aarch64-unknown-linux-gnu-gcc-wrapper-15.3.0/bin/aarch64-unknown-linux-gnu-cc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='/nix/store/0hvd25jcaxcq45gkrgwqmd4gfqixg99h-aarch64-unknown-linux-gnu-gcc-wrapper-15.3.0/bin/aarch64-unknown-linux-gnu-c++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/graysontinker/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/graysontinker/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -pthread -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2578406192=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='arm64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/home/graysontinker/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/graysontinker/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/nix/store/54yyhgzwghim2l37xy2qn9ihh4zwi2ps-go-aarch64-unknown-linux-gnu-1.26.7/share/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/graysontinker/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/nix/store/54yyhgzwghim2l37xy2qn9ihh4zwi2ps-go-aarch64-unknown-linux-gnu-1.26.7/share/go/pkg/tool/linux_arm64'
GOVCS=''
GOVERSION='go1.26.7'
GOWORK=''
PKG_CONFIG='pkg-config'What did you do?
Cross-compile Go with this Nix derivation. This is mostly provided so you can see how the environment variables are being set. Then, attempt to compile a CGo program with the cross-compiled toolchain.
What did you see happen?
cgo: exec /nix/store/0hvd25jcaxcq45gkrgwqmd4gfqixg99h-aarch64-unknown-linux-gnu-gcc-wrapper-15.3.0/bin/aarch64-unknown-linux-gnu-cc: fork/exec /nix/store/0hvd25jcaxcq45gkrgwqmd4gfqixg99h-aarch64-unknown-linux-gnu-gcc-wrapper-15.3.0/bin/aarch64-unknown-linux-gnu-cc: exec format errorWhen cross-compiling, CC and CC_FOR_TARGET naturally point at a compiler on the building host. Therefore, it doesn't make any sense for these paths to be embedded into the output toolchain: the target host would be unable to run these formats.
This comment confirms that this isn't the intended behavior, and that this is the purpose of the CC_FOR_$GOOS_$GOARCH variables.
What did you expect to see?
The embedded C compiler location should remain the default unless overridden by a CC_FOR_$GOOS_$GOARCH environment variable.
This seemingly occurs because the output build.go:compilerEnv is used for both building the binary (possibly correctly?) and choosing which paths to embed (incorrectly). A fix probably involves splitting this function into one gathering build-time compilers (the first two clauses of the current function), and one gathering compilers to embed (the third clause). I have a tentative patch written for this, but wanted to see the thoughts of others on whether this is a correct understanding of the source of the problem before submitting it.
Source: golang/go