【问题标题】:Using pre-commit.com, How can I fix go vet error "no Go files in"使用 pre-commit.com,如何修复 go vet 错误“没有 Go 文件”
【发布时间】:2020-11-11 10:16:27
【问题描述】:

我们的项目使用go standards project layout。另外,我正在使用来自pre-commit.com 的预提交钩子。

在此设置中,go-vet 抱怨:

go vet...................................................................Failed
- hook id: go-vet
- exit code: 1

no Go files in <main directory of the package>

这是因为由于项目布局,main.go 文件位于cmd/tool/main.go

我该如何解决这个问题?我不想禁用 go-vet...

编辑: 抱歉,我没有意识到 go vet 钩子不是来自 pre-commit.com 本身...

这是我的 .pre-commit-config.yaml:

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
  rev: v2.4.0
  hooks:
    - id: trailing-whitespace
    - id: end-of-file-fixer
    - id: check-yaml
    - id: check-added-large-files

- repo: git://github.com/dnephin/pre-commit-golang
  rev: master
  hooks:
    - id: go-build
    - id: go-critic
    - id: go-cyclo
      args: [-over=30]
    - id: go-fmt
    - id: go-imports
    # - id: go-lint
    - id: go-mod-tidy
    # - id: go-unit-tests
    - id: go-vet
    # - id: golangci-lint
    # - id: gometalinter
    # - id: validate-toml

exclude: (^vendor/|_test.go$)

【问题讨论】:

  • 您可以使用--no-verify 标志跳过钩子。
  • @Maroun 显然。但这将是最糟糕的“解决方案”,甚至禁用go vet 会更好,因为这样我就会得到套件中的所有其他检查......
  • @MarkusGrunwald 你能分享你的配置吗? go-vet 钩子不是预提交/项目之一的一部分,因此它可能是第 3 方。用 [go-vet] 标记它也可能很有用,因为这可能不是预提交问题(它只是调用底层工具)

标签: git pre-commit.com


【解决方案1】:

我对 dnephin 的钩子进行了更深入的研究,对 run-go-vet.sh 的更改解决了我的问题

diff --git a/run-go-vet.sh b/run-go-vet.sh
index 0d21ea4..3de9948 100755
--- a/run-go-vet.sh
+++ b/run-go-vet.sh
@@ -1,6 +1,6 @@
 #!/usr/bin/env bash
 set -e
-pkg=$(go list)
+pkg=$(go list -m)
 for dir in $(echo $@|xargs -n1 dirname|sort -u); do
   go vet $pkg/$dir
 done

基本上,go list 将搜索模块而不是包。 为什么解决了我的问题,我不太确定....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    相关资源
    最近更新 更多