gitpre-commit.com

Using pre-commit.com, How can I fix go vet error "no Go files in"


Our project uses the go standards project layout. Also, I'm using the pre-commit-hooks from pre-commit.com.

In this setup, go-vet complains:

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

no Go files in <main directory of the package>

That's because due to the project layout, the main.go file(s) are in cmd/tool/main.go.

How can I fix this? I don't want to disable go-vet...

Edit: Sorry, I didn't realize that the go vet hook is not from pre-commit.com itself...

That's my .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$)

Solution

  • I dug a bit deeper in dnephin's hooks and this change to run-go-vet.sh solved my problem

    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
    

    Basically, go list will search for modules instead of packages. Why that solves my problem, I'm not so sure....