shirou / gopsutil

psutil for golang
Other
10.56k stars 1.58k forks source link

Version 2 and 3 of the library should have different symbol names #1136

Closed mx-psi closed 3 years ago

mx-psi commented 3 years ago

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

package main

import (
    _ "github.com/shirou/gopsutil/disk"
    _ "github.com/shirou/gopsutil/v3/disk"
)

func main() {}

with go.mod:

module example.com

go 1.17

require (
    github.com/shirou/gopsutil v3.21.8+incompatible
    github.com/shirou/gopsutil/v3 v3.21.8
)

require (
    github.com/StackExchange/wmi v1.2.1 // indirect
    github.com/go-ole/go-ole v1.2.5 // indirect
    golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71 // indirect
)

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):

❯ 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.

mx-psi commented 3 years ago

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