gopsutil provides a version 2 and a version 3 of the library, both of which use cgo. If the same project has both version 2 and 3 on their dependency tree, this causes build issues on macOS (and possibly other platforms).
Copy both files to a directory and run go build. This will cause a linker error:
❯ go build
# example.com
/usr/local/Cellar/go/1.17.1/libexec/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
duplicate symbol '_readdrivestat' in:
/var/folders/6d/hrpbskwn3nv8qnm51fdpmh5c0000gn/T/go-link-455232793/000002.o
/var/folders/6d/hrpbskwn3nv8qnm51fdpmh5c0000gn/T/go-link-455232793/000005.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
While no one would directly import both library versions, it is possible for both library versions to be on the dependency tree at the same time (this is what happens on open-telemetry/opentelemetry-collector-contrib#5425).
Expected behavior
The program should build correctly.
Environment (please complete the following information):
[ ] Windows: [paste the result of ver]
[ ] Linux: [paste contents of /etc/os-release and the result of uname -a]
[x] Mac OS: [paste the result of sw_vers and uname -a]
[ ] FreeBSD: [paste the result of freebsd-version -k -r -u and uname -a]
[ ] OpenBSD: [paste the result of uname -a]
❯ sw_vers
ProductName: macOS
ProductVersion: 11.6
BuildVersion: 20G165
❯ uname -a
Darwin Pablos-MacBook-Pro.local 20.6.0 Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64 x86_64 i386 MacBookPro15,2 Darwin
Additional context
This happens because both v2 and v3 define readdrivestat on gopsutil/disk/iostat_darwin.h and gopsutil/v3/disk/iostat_darwin.h. Renaming symbols on one of the versions solves the issue for me.
Maybe it's a good idea to add a CI test trying to build a program that uses both v3 and v2 of the library at the same time? Happy to do that if it seems like a good idea
Describe the bug
gopsutil provides a version 2 and a version 3 of the library, both of which use cgo. If the same project has both version 2 and 3 on their dependency tree, this causes build issues on macOS (and possibly other platforms).
To Reproduce
A minimal example is the following program
with
go.mod
:Copy both files to a directory and run
go build
. This will cause a linker error:While no one would directly import both library versions, it is possible for both library versions to be on the dependency tree at the same time (this is what happens on open-telemetry/opentelemetry-collector-contrib#5425).
Expected behavior
The program should build correctly.
Environment (please complete the following information):
ver
]/etc/os-release
and the result ofuname -a
]sw_vers
anduname -a
]freebsd-version -k -r -u
anduname -a
]uname -a
]Additional context
This happens because both v2 and v3 define
readdrivestat
ongopsutil/disk/iostat_darwin.h
andgopsutil/v3/disk/iostat_darwin.h
. Renaming symbols on one of the versions solves the issue for me.