问题
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