【问题标题】:Using `shirou/gopsutil` Go package as a vendored package fails on macOS (10.14.6)在 macOS (10.14.6) 上使用“shirou/gopsutil”Go 包作为供应商包失败
【发布时间】:2020-07-13 21:46:30
【问题描述】:

我试图在本地构建 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

【问题讨论】:

    标签: go telegraf govendor


    【解决方案1】:

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

    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/host 复制到$GOPATH/src/github.com/influxdata/telegraf/vendor/github.com/shirou/gopsutil,并且成功了。

    希望对您有所帮助。

    【讨论】:

    【解决方案2】:

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

    运行这些命令

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

    【讨论】:

      猜你喜欢
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 2022-11-19
      • 1970-01-01
      • 2016-01-31
      • 1970-01-01
      • 2018-10-18
      相关资源
      最近更新 更多