Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go build produces bad error message when CGO_ENABLED=0 but need to build using cgo #69288

Closed
achernya opened this issue Sep 5, 2024 · 2 comments

Comments

@achernya
Copy link

achernya commented Sep 5, 2024

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/[email protected]
@seankhliao
Copy link
Member

Duplicate of #24068

@seankhliao seankhliao marked this as a duplicate of #24068 Sep 5, 2024
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants