0

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$)
Markus Grunwald
  • 133
  • 1
  • 10
  • You can use `--no-verify` flag to skip hooks. – Maroun Nov 11 '20 at 10:18
  • 1
    @Maroun obviously. But this would be the worst "solution", even disabling `go vet` would be better because then I'd get all the other checks in the suite... – Markus Grunwald Nov 11 '20 at 10:58
  • @MarkusGrunwald can you share your configuration? the go-vet hook isn't part of one of the pre-commit/ projects so it's probably a 3rd party. it may also be useful to tag this with [go-vet] since it's likely this isn't a pre-commit problem (which just calls underlying tools) – anthony sottile Nov 11 '20 at 16:57

1 Answers1

0

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....

Markus Grunwald
  • 133
  • 1
  • 10