Skip to content

cmd/go: TestScript/autocgo fails when CGO_ENABLED=1 (go1.25.0 regression) #75340

@Lekensteyn

Description

@Lekensteyn

Go version

go version go1.25.0 linux/arm64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/root/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/root/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build163042255=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='arm64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/w/src/go.mod'
GOMODCACHE='/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/w'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/root/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/w/pkg/tool/linux_arm64'
GOVCS=''
GOVERSION='go1.25.0'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

cd src
CGO_ENABLED=1 ./all.bash

What did you see happen?

##### Testing packages.
# go tool dist test -run=^cmd/go$
vcs-test.golang.org rerouted to http://127.0.0.1:45751
https://vcs-test.golang.org rerouted to https://127.0.0.1:45827
go test proxy running at GOPROXY=http://127.0.0.1:35641/mod
--- FAIL: TestScript (0.48s)
    --- FAIL: TestScript/autocgo (0.03s)
        script_test.go:139: 2025-09-09T18:13:20Z
        script_test.go:141: $WORK=/tmp/cmd-go-test-976430197/tmpdir4240751195/autocgo3125866786
        script_test.go:163:
            # Test automatic setting of CGO_ENABLED based on $CC and what's in $PATH. (0.000s)
            # Assume we're on a system that can enable cgo normally. (0.009s)
            # Clearing CC and removing everything but Go from the PATH should usually
            # disable cgo: no C compiler anymore (unless the baked-in defaultCC is an
            # absolute path and exists. (0.015s)
            > env CC=
            > env PATH=$GOROOT/bin
            > go env CGO_ENABLED
            [stdout]
            1
            > [!abscc] stdout 0
        script_test.go:163: FAIL: testdata/script/autocgo.txt:17: stdout 0: no match for `(?m)0` in stdout
FAIL
FAIL	cmd/go	23.070s

What did you expect to see?

Tests should pass.

Reverting commit 8cd6d68 (#69994) fixes the problem as does removing CGO_ENABLED=1 at build time. The documentation in src/cmd/cgo/doc.go also became out-of-date with the offending commit. It currently still says:

The cgo tool is enabled by default for native builds on systems where
it is expected to work. It is disabled by default when cross-compiling
as well as when the CC environment variable is unset and the default
C compiler (typically gcc or clang) cannot be found on the system PATH.
You can override the default by setting the CGO_ENABLED
environment variable when running the go tool: set it to 1 to enable
the use of cgo, and to 0 to disable it.

After the commit, the default CGO_ENABLED state can be set while building the toolchain, making it invariant of cross-compilation. That seems somewhat reasonable when CC_FOR_${GOOS}_${GOARCH} is set. It is however not matching the current documentation.

I expected:

  • Tests should not fail with CGO_ENABLED set during the toolchain build.
  • Documentation should ideally match actual behavior with CGO_ENABLED set during the toolchain. To be fair, this is a very special case that does not affect the majority of users using the standard toolchain builds where CGO_ENABLED is not set at build time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.GoCommandcmd/goNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions