fix: emit 0/1 for CGO_ENABLED so the cgo config takes effect - #148
Open
alanshaw wants to merge 1 commit into
Open
fix: emit 0/1 for CGO_ENABLED so the cgo config takes effect#148alanshaw wants to merge 1 commit into
alanshaw wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
cgooption ingo-test-config.jsonhas never worked. The workflow renders it asCGO_ENABLED=trueorCGO_ENABLED=false, and Go only recognizes0and1; any other value falls back to the platform default, which is1on every hosted runner. Repos that set"cgo": falsehave been testing with cgo enabled all along. You can see it in any such repo's job log: the test step env showsCGO_ENABLED: falsewhilego envreportsCGO_ENABLED='1'.This PR makes the regular and 32-bit test steps emit
1or0. The race detector step is pinned to1, sincego test -racerequires cgo; a real0there would break the race run for every repo that sets"cgo": falsewithoutskipRace. The README documents the race caveat and points atskipRacefor modules that cannot build with cgo at all.BREAKING CHANGE: repos with
"cgo": falsewill 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/gosigarcompiles without cgo on Linux but requires it on macOS). Those repos should either fix the build or drop the option.