Skip to content

cmd/go: implicit CGO_ENABLED=1 in go env can be misleading #70868

@akhilerm

Description

@akhilerm

Go version

go version go1.22.10 linux/amd64

Output of go env in your module/workspace:

GO111MODULE='auto'
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/akhil/.cache/go-build'
GOENV='/home/akhil/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/akhil/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/akhil/go'
GOPRIVATE=''
GOPROXY='direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN=''
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.10'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD=''
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 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build4043312219=/tmp/go-build -gno-record-gcc-switches'

What did you do?

  1. Tried to compile the below sample program with CGO,
package main

import (
    "crypto/tls"
    "io/ioutil"
    "log"
    "net/http"
    "crypto/x509"
)

func main() {
    caCert, err := ioutil.ReadFile("rootCA.crt")
    if err != nil {
        log.Fatal(err)
    }
    caCertPool := x509.NewCertPool()
    caCertPool.AppendCertsFromPEM(caCert)

    client := &http.Client{
        Transport: &http.Transport{
            TLSClientConfig: &tls.Config{
                RootCAs:      caCertPool,
            },
        },
    }

    _, err = client.Get("https://secure.domain.com")
    if err != nil {
        panic(err)
    }
}
  1. Following is the output of the go env and system env
akhil@akhil-ThinkPad-L14:~/W/d/golang-darwin-cgo $ go env
GO111MODULE='auto'
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/akhil/.cache/go-build'
GOENV='/home/akhil/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/akhil/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/akhil/go'
GOPRIVATE=''
GOPROXY='direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN=''
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.10'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD=''
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 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build776189878=/tmp/go-build -gno-record-gcc-switches'

akhil@akhil-ThinkPad-L14:~/W/d/golang-darwin-cgo $ env
SHELL=/bin/bash
SESSION_MANAGER=local/akhil-ThinkPad-L14:@/tmp/.ICE-unix/17411,unix/akhil-ThinkPad-L14:/tmp/.ICE-unix/17411
QT_ACCESSIBILITY=1
COLORTERM=truecolor
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu-xorg:/etc/xdg
XDG_MENU_PREFIX=gnome-
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GNOME_KEYRING_CONTROL=/run/user/1000/keyring
LANGUAGE=en_IN:en
GNOME_SHELL_SESSION_MODE=ubuntu
SSH_AUTH_SOCK=/run/user/1000/gnupg/S.gpg-agent.ssh
MEMORY_PRESSURE_WRITE=c29tZSAyMDAwMDAgMjAwMDAwMAA=
XMODIFIERS=@im=ibus
DESKTOP_SESSION=ubuntu-xorg
GPG_TTY=/dev/pts/3
GTK_MODULES=gail:atk-bridge
PWD=/home/akhil/Work/dev-config.d/golang-darwin-cgo
LOGNAME=akhil
XDG_SESSION_DESKTOP=ubuntu-xorg
XDG_SESSION_TYPE=x11
GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1
SYSTEMD_EXEC_PID=17590
XAUTHORITY=/run/user/1000/gdm/Xauthority
WINDOWPATH=2
HOME=/home/akhil
USERNAME=akhil
LANG=en_IN
XDG_CURRENT_DESKTOP=ubuntu:GNOME
MEMORY_PRESSURE_WATCH=/sys/fs/cgroup/user.slice/user-1000.slice/user@1000.service/session.slice/org.gnome.SettingsDaemon.MediaKeys.service/memory.pressure
VTE_VERSION=7600
GNOME_TERMINAL_SCREEN=/org/gnome/Terminal/screen/2f023872_25c5_4838_93b9_367cdadbb700
GOROOT=/usr/local/go
LESSCLOSE=/usr/bin/lesspipe %s %s
XDG_SESSION_CLASS=user
TERM=xterm-256color
LESSOPEN=| /usr/bin/lesspipe %s
LIBVIRT_DEFAULT_URI=qemu:///system
USER=akhil
GNOME_TERMINAL_SERVICE=:1.195
DISPLAY=:0
SHLVL=1
GSM_SKIP_SSH_AGENT_WORKAROUND=true
CSCOPE_DB=/home/akhil/cscope.out
QT_IM_MODULE=ibus
XDG_RUNTIME_DIR=/run/user/1000
DEBUGINFOD_URLS=https://debuginfod.ubuntu.com 
GCC_COLORS=error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01
XDG_DATA_DIRS=/usr/share/ubuntu-xorg:/usr/share/gnome:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
PATH=/home/akhil/Android/Sdk/platform-tools:/home/akhil/.cargo/bin:/home/akhil/.local/bin:/home/akhil/bin:/home/akhil/.cargo/bin:/home/akhil/.local/bin:/home/akhil/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/akhil/.local/share/JetBrains/Toolbox/scripts:/usr/local/go/bin:/home/akhil/go/bin
GDMSESSION=ubuntu-xorg
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
OLDPWD=/home/akhil/Work/dev-config.d
GOPATH=/home/akhil/go
_=/usr/bin/env
  1. CGO_ENABLED=1 in the go env and is unset in the system env. If we try to compile the program now, it works fine
akhil@akhil-ThinkPad-L14:~/W/d/golang-darwin-cgo $ GOOS=darwin go build main.go
akhil@akhil-ThinkPad-L14:~/W/d/golang-darwin-cgo $ ls
main  main.go
  1. But if we export the CGO_ENABLED variable and then perform the compilation, it results in error.
akhil@akhil-ThinkPad-L14:~/W/d/golang-darwin-cgo $ export CGO_ENABLED=1
akhil@akhil-ThinkPad-L14:~/W/d/golang-darwin-cgo $ go env
GO111MODULE='auto'
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/akhil/.cache/go-build'
GOENV='/home/akhil/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/akhil/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/akhil/go'
GOPRIVATE=''
GOPROXY='direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN=''
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.10'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD=''
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 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3491146165=/tmp/go-build -gno-record-gcc-switches'
akhil@akhil-ThinkPad-L14:~/W/d/golang-darwin-cgo $ env
SHELL=/bin/bash
SESSION_MANAGER=local/akhil-ThinkPad-L14:@/tmp/.ICE-unix/17411,unix/akhil-ThinkPad-L14:/tmp/.ICE-unix/17411
QT_ACCESSIBILITY=1
COLORTERM=truecolor
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu-xorg:/etc/xdg
XDG_MENU_PREFIX=gnome-
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GNOME_KEYRING_CONTROL=/run/user/1000/keyring
LANGUAGE=en_IN:en
GNOME_SHELL_SESSION_MODE=ubuntu
SSH_AUTH_SOCK=/run/user/1000/gnupg/S.gpg-agent.ssh
MEMORY_PRESSURE_WRITE=c29tZSAyMDAwMDAgMjAwMDAwMAA=
XMODIFIERS=@im=ibus
DESKTOP_SESSION=ubuntu-xorg
GPG_TTY=/dev/pts/3
GTK_MODULES=gail:atk-bridge
PWD=/home/akhil/Work/dev-config.d/golang-darwin-cgo
LOGNAME=akhil
XDG_SESSION_DESKTOP=ubuntu-xorg
XDG_SESSION_TYPE=x11
GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1
SYSTEMD_EXEC_PID=17590
XAUTHORITY=/run/user/1000/gdm/Xauthority
WINDOWPATH=2
HOME=/home/akhil
USERNAME=akhil
LANG=en_IN
XDG_CURRENT_DESKTOP=ubuntu:GNOME
MEMORY_PRESSURE_WATCH=/sys/fs/cgroup/user.slice/user-1000.slice/user@1000.service/session.slice/org.gnome.SettingsDaemon.MediaKeys.service/memory.pressure
VTE_VERSION=7600
GNOME_TERMINAL_SCREEN=/org/gnome/Terminal/screen/2f023872_25c5_4838_93b9_367cdadbb700
GOROOT=/usr/local/go
LESSCLOSE=/usr/bin/lesspipe %s %s
XDG_SESSION_CLASS=user
TERM=xterm-256color
LESSOPEN=| /usr/bin/lesspipe %s
LIBVIRT_DEFAULT_URI=qemu:///system
USER=akhil
GNOME_TERMINAL_SERVICE=:1.195
DISPLAY=:0
SHLVL=1
GSM_SKIP_SSH_AGENT_WORKAROUND=true
CSCOPE_DB=/home/akhil/cscope.out
QT_IM_MODULE=ibus
XDG_RUNTIME_DIR=/run/user/1000
DEBUGINFOD_URLS=https://debuginfod.ubuntu.com 
GCC_COLORS=error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01
XDG_DATA_DIRS=/usr/share/ubuntu-xorg:/usr/share/gnome:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
PATH=/home/akhil/Android/Sdk/platform-tools:/home/akhil/.cargo/bin:/home/akhil/.local/bin:/home/akhil/bin:/home/akhil/.cargo/bin:/home/akhil/.local/bin:/home/akhil/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/akhil/.local/share/JetBrains/Toolbox/scripts:/usr/local/go/bin:/home/akhil/go/bin
GDMSESSION=ubuntu-xorg
CGO_ENABLED=1
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
OLDPWD=/home/akhil/Work/dev-config.d
GOPATH=/home/akhil/go
_=/usr/bin/env

Here we can see that both in go env and system env, CGO_ENABLED=1 is set

akhil@akhil-ThinkPad-L14:~/W/d/golang-darwin-cgo $ GOOS=darwin go build main.go
# runtime/cgo
cgo: C compiler "clang" not found: exec: "clang": executable file not found in $PATH

and results in the above error when compiled which is expected since I dont have clang installed in my path.

What did you see happen?

Even if CGO_ENABLED=1 is set in the go env, it doesnt come into effect unless export CGO_ENABLED=1 is used and it is set in the system env also.

What did you expect to see?

go env should give the correct information about CGO_ENABLED and the above program should fail to compile always if CGO_ENABLED=1 is present in the go env.

  • When using go1.23.4, getting similar issue. i.e irrespective of whether the system env is set to CGO_ENABLED=1 or not, the program always compiles, which should not happen as cross compilation to darwin should fail since clang is not present. Checked the 1.23.0 release notes and didnt see any specific mention of changes to compiler or linker related to cross compiling to darwin.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions