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
It turned out that this error is caused by a well known issue whereby
go mod vendorprunes non-package directories, hence causing thehost/includefolder ofshirou/gopsutil, which contains thesmc.cfile, to be pruned:https://github.com/golang/go/issues/26366
The impact of this issue on
shirou/gopsutilis 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 copiedfreebsd_headersandincludefrom$GOPATH/pkg/mod/github.com/shirou/gopsutil/hostto$GOPATH/src/github.com/influxdata/telegraf/vendor/github.com/shirou/gopsutiland it worked.I hope you find this helpful.