Description
Go version
go1.23.0 linux/amd64
Output of go env
in your module/workspace:
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/root/.cache/go-build'
GOENV='/root/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/root/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/root/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.0'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/root/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='0'
GOMOD='/src/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build430207322=/tmp/go-build -gno-record-gcc-switches'
What did you do?
docker pull debian:12.7
docker run -it debian:12.7
apt update
apt -y install wget
wget https://go.dev/dl/go1.23.0.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.23.0.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
mkdir src && cd src
go mod init example
cat <<EOF > main.go
package main
import "C"
func main() {}
EOF
go mod tidy
go build
What did you see happen?
# go build
package example: build constraints exclude all Go files in /src
What did you expect to see?
go build
should produce a better error message indicating what happened. It's not obvious what build constraints exist, or why they could not be satisfied. No amount of go build -v
, go build -x
, or go build -work
. The only hint that something is wrong is in go env
, namely CGO_ENABLED=0
.
https://pkg.go.dev/cmd/cgo does explain that cgo produces a build constraint, and even goes on to explain that CGO_ENABLED=0 is set if the default C compiler is not found (or CC is unset). This makes sense, as the container in this bug report (intentionally) did not (yet) have gcc/clang installed. Even setting CC=/bin/true
makes go build -x
show it actually trying to compile things, as opposed to giving a confusing error message.
Google searching for this error message returns results like this StackOverflow post that imply this is a corrupted module cache, which is not the right breadcrumb here.
It would be nice if go build
could print a more detailed error message, perhaps special-casing cgo, if CGO_ENABLED=0
is set implicitly by a lack of present CC.
Note that this issue is not limited to the reduced reproduction case I provided here; I originally saw it when trying to go run
a package that transitively depends on a go module that itself uses cgo:
# go run cmd/cli/main.go
github.com/msteinert/pam/v2: build constraints exclude all Go files in /root/go/pkg/mod/github.com/msteinert/pam/v2@v2.0.0
Activity
gabyhelp commentedon Sep 5, 2024
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
seankhliao commentedon Sep 5, 2024
Duplicate of #24068