运行 go vet with dep 时找不到头文件

Header file not found when running go vet with dep

当 运行 在我的项目中使用 vendored 依赖项时,我得到了这个错误。

$ go vet ./...
# <project path...>/vendor/github.com/ethereum/go-ethereum/crypto/secp256k1
vendor/github.com/ethereum/go-ethereum/crypto/secp256k1/curve.go:42:10: fatal error: libsecp256k1/include/secp256k1.h: No such file or directory
 #include "libsecp256k1/include/secp256k1.h"

我以为这是开发环境中缺少的依赖,但是在查看原始项目源时,包含路径是相对于源文件的。

为什么找不到文件?

go 的一些依赖管理工具不会提供项目引用的所有代码。这意味着在某些情况下,可以在带有 cgo 的 go 文件中使用的 C 代码不包含在 vendor 目录中。

我曾两次使用两个单独的供应商工具遇到过这个问题,但是有工作可以支持这些 use cases

到目前为止我发现的最简单的方法是使用govendor然后导入完整的目录以确保所有需要的文件都在那里。这是一个非常简单的解决方案,它忽略了在 go 项目中包含 c 依赖项的很多复杂性,但修复了这个问题,同时没有永久修复这个问题。

go get github.com/kardianos/govendor
govendor init
govendor add +e
# Remove the directory that is missing the c dependencies
rm -rf ./vendor/github.com/ethereum/go-ethereum/crypto/secp256k1/
# Add the file and include all files
# https://github.com/kardianos/govendor/issues/247
govendor add github.com/ethereum/go-ethereum/crypto/secp256k1/^

Gopkg.toml中你可以添加

[prune]
  go-tests = true
  unused-packages = true
  non-go = true

  [[prune.project]]
    name = "github.com/ethereum/go-ethereum"
    non-go = false
    unused-packages = false