Skip to content

fix: emit 0/1 for CGO_ENABLED so the cgo config takes effect - #148

Open
alanshaw wants to merge 1 commit into
ipdxco:mainfrom
alanshaw:ash/fix/cgo-config
Open

fix: emit 0/1 for CGO_ENABLED so the cgo config takes effect#148
alanshaw wants to merge 1 commit into
ipdxco:mainfrom
alanshaw:ash/fix/cgo-config

Conversation

@alanshaw

@alanshaw alanshaw commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

The cgo option in go-test-config.json has never worked. The workflow renders it as CGO_ENABLED=true or CGO_ENABLED=false, and Go only recognizes 0 and 1; any other value falls back to the platform default, which is 1 on every hosted runner. Repos that set "cgo": false have been testing with cgo enabled all along. You can see it in any such repo's job log: the test step env shows CGO_ENABLED: false while go env reports CGO_ENABLED='1'.

This PR makes the regular and 32-bit test steps emit 1 or 0. The race detector step is pinned to 1, since go test -race requires cgo; a real 0 there would break the race run for every repo that sets "cgo": false without skipRace. The README documents the race caveat and points at skipRace for modules that cannot build with cgo at all.

BREAKING CHANGE: repos with "cgo": false will run cgo-free tests for the first time, and some will start failing where a dependency needs cgo on a given OS (for example, elastic/gosigar compiles without cgo on Linux but requires it on macOS). Those repos should either fix the build or drop the option.

alanshaw added a commit to fil-forge/piri that referenced this pull request Aug 20, 2026
Upgrades Forge deps and enables CGO on Darwin due to `gosigar` requiring
it on macos. Also fixes the binary builds on release.

<details>
<summary>Why does `gosigar` require CGO on Darwin?</summary>

On macOS the system stats gosigar reports only exist behind C library
interfaces, and the file that wraps them is a cgo file.

In gosigar (`github.com/elastic/gosigar`) the darwin implementation is
split
in two:

- `sigar_common_darwin.go` starts with a C preamble (`#include
<sys/sysctl.h>`, `<mach/mach_host.h>`, `<libproc.h>`, …) and `import
"C"`.
It implements `Cpu.Get`, `Mem.Get`, `Swap.Get`, `LoadAverage.Get`,
process
  info, and the `sysctlbyname` helper by calling `getloadavg`,
  `host_statistics`, `host_processor_info`, `proc_pidinfo`, and
  `sysctlbyname` directly.
- `sigar_darwin.go` and `concrete_sigar.go` are pure Go and call into
those.

When `CGO_ENABLED=0`, the Go toolchain silently drops every file that
imports
`"C"` from the package. `sigar_common_darwin.go` vanishes, and the
surviving
pure-Go files fail to compile against the now-missing pieces. That is
the
error signature piri's `make test` produced on macOS:

```
sigar_darwin.go:13:12: undefined: sysctlbyname
concrete_sigar.go:20:12: cpuUsage.Get undefined (type Cpu has no field or method Get)
```

The reason darwin uses C where Linux doesn't: on Linux all of this data
is
plain text under `/proc` and `/sys`, so `sigar_linux.go` just reads
files.
macOS has no `/proc`; CPU, memory, and process stats come from Mach
kernel
APIs (`host_statistics`, `host_processor_info`) and `libproc`, and
Apple's
only stable way to reach those is through the system C library. There is
no
pure-Go path for the Mach calls (raw syscall numbers are not a supported
interface on macOS), so cgo is unavoidable. gosigar is also a direct
port of
the old C libsigar (the VMware copyright header is still on the file),
which
is why the darwin port stayed a thin C wrapper rather than using the
partial
pure-Go `sysctl` support in `golang.org/x/sys/unix`.
</details>

Note: The build passes in CI on MacOS due to a bug in the
`unified-github-workflows` CI config that ended up enabling CGO for all
builds. It's fixed in
ipdxco/unified-github-workflows#148, but when
merged and released we'll have to fix our CI config here as we won't be
able to use [`"cgo": false`
config](https://github.com/fil-forge/piri/blob/7ec2505e021288f36a06535f2ce748528e0f9f2e/.github/workflows/go-test-config.json#L2)
since on MacOS it is required to be true.

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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

Successfully merging this pull request may close these issues.

1 participant