0

我们的项目使用go 标准项目布局。另外,我正在使用pre-commit.com中的 pre-commit-hooks 。

在此设置中,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$)
4

1 回答 1

0

我在 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 将搜索模块而不是包。为什么这能解决我的问题,我不太确定....

于 2020-11-12T09:51:37.750 回答