1

我试图在influx/telegraf本地构建并使用go mod vendor命令供应所有包。然而,有趣的是,构建失败后出现以下错误:

# 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"
          ^~~~~~~~~~~~~~~

重现步骤(设置 GOPATH 变量后):

# 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
4

2 回答 2

3

事实证明,这个错误是由一个众所周知的问题引起的,即go mod vendor修剪非包目录,从而导致包含文件的host/include文件夹被修剪:shirou/gopsutilsmc.c

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

shirou/gopsutil已经报告了此问题的影响:

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

显然没有简单的解决方案,但目前的解决方法是将丢失的目录手动复制到vendor/github.com/shirou/gopsutil. 在我的情况下,我复制freebsd_headersinclude$GOPATH/pkg/mod/github.com/shirou/gopsutil/hostto$GOPATH/src/github.com/influxdata/telegraf/vendor/github.com/shirou/gopsutil并且它有效。

我希望你觉得这有帮助。

于 2020-07-02T21:06:25.197 回答
0

正如@Rafid 所建议的,您可以进行手动复制。我发现下面的解决方法也很有帮助。

运行这些命令

go get github.com/shirou/gopsutil
go mod vendor
于 2020-07-13T13:46:30.807 回答