Using `shirou/gopsutil` Go package as a vendored package fails on macOS (10.14.6)

假装没事ソ 提交于 2020-08-09 09:11:50

问题


I was trying to build influx/telegraf locally and vendor all the packages using go mod vendor command. However, interestingly, the build fails after that with the following error:

# github.com/shirou/gopsutil/host
vendor/github.com/shirou/gopsutil/host/host_darwin_cgo.go:9:11: fatal error: 'include/smc.c' file not found
 #include "include/smc.c"
          ^~~~~~~~~~~~~~~

Steps to reproduce (after setting GOPATH variable):

# Fetch the project, e.g. influx/telegraf
go get -d github.com/influxdata/telegraf

# CD into the project
cd $GOPATH/src/influxdata/telegraf

# Checkout v1.14.5 (the current latest version)
git checkout v1.14.5

# Fetch the modules under vendor/ directory
go mod vendor

# Then modify the Makefile as follows:
# 1. Remove the `deps` target and its reference in the `all` target; it is not needed anymore, as we fetched all modules.
# 2. Add the `-mod vendor` flag everywhere `go build`, `go install`, or `go test` is called. This is necessary since Telegraf v1.14.5 is still on version 1.12 of Go, which requires this flag to use the modules under `vendor/` directory.

# Finally, build the project and you should get the error above
make

回答1:


It turned out that this error is caused by a well known issue whereby go mod vendor prunes non-package directories, hence causing the host/include folder of shirou/gopsutil, which contains the smc.c file, to be pruned:

https://github.com/golang/go/issues/26366

The impact of this issue on shirou/gopsutil is already reported:

https://github.com/shirou/gopsutil/issues/832

There is no easy solution apparently, but a work-around for now is to manually copy the missing directories to vendor/github.com/shirou/gopsutil. In my case, I copied freebsd_headers and include from $GOPATH/pkg/mod/github.com/shirou/gopsutil/host to $GOPATH/src/github.com/influxdata/telegraf/vendor/github.com/shirou/gopsutil and it worked.

I hope you find this helpful.




回答2:


As suggested by @Rafid, you can do a manual copy. I found this below work-around to be helpful as well.

Run these commands

go get github.com/shirou/gopsutil
go mod vendor


来源:https://stackoverflow.com/questions/62705020/using-shirou-gopsutil-go-package-as-a-vendored-package-fails-on-macos-10-14-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!