Skip to content

cmd/go: default to CGO_ENABLED=0 when CC not set and default compiler not found #47251

@jayconrod

Description

@jayconrod

When we fixed #40042, we started including version information about the C compiler in cache action keys that use it. It looks like this:

HASH[build runtime/cgo]: "CC ID=\"gcc version 8.3.0 (Debian 8.3.0-6) \"\n"

This means that if the C compiler changes, Go packages in the cache and in GOROOT/pkg built with the old version will not be used.

Consequently, runtime/cgo, net, os/user, and other packages in std that use cgo by default are stale if the user's C compiler is different than the one used to build the Go binary distribution. It almost always is.

If the user does not have a C compiler installed and has not explicitly set CGO_ENABLED=0, go build will fail to build any package that depends on these packages, even if nothing else uses cgo. This is common when building inside a Docker container and in CI. I'm worried Go 1.17 will break a lot of builds without some mitigation. For example #47215 is caused by this.

One possibility is to default CGO_ENABLED to 0 if CC is not explicitly set, and the default C compiler (usually gcc) is not in PATH. std packages would be stale, but everything could be rebuilt without a C compiler.

This may cause subtle changes in behavior. The cgo versions of net and os/user use libc code to resolve DNS queries and find users. The pure Go versions don't use libc. Also, any package, even a pure Go package may use the cgo build tag for conditional compilation, and that would change.

This is a big behavioral change late in the release cycle, though I expect the diff will be small.

cc @rsc @ianlancetaylor @bcmills @matloob

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.release-blocker

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions