getting error when running pre-commit hook for golang repo [named files must be .go files: ./…]


This is the contenct of my .pre-commit-config.yaml file,

- repo: local
    - id: static-checks-pramod
      name: Static Analysis
      description: This hook does static analysis
      entry: staticcheck -tests=false ./...
      language: golang
      types: [text]

on running the hooks locally for all the files locally im getting below error,

pramodchoudhari@Pramods-MacBook-Pro-2 my-repo % pre-commit run --all-files
Static Analysis..........................................................Failed
- hook id: static-checks-pramod
- exit code: 1

-: named files must be .go files: ./...
-: named files must be .go files: ./...
-: named files must be .go files: ./...
-: named files must be .go files: ./...
-: named files must be .go files: ./...
-: named files must be .go files: ./...
-: named files must be .go files: ./...
-: named files must be .go files: ./...

but if i run staticcheck command locally, it work fine as below,

pramodchoudhari@Pramods-MacBook-Pro-2 my-repo % staticcheck -tests=false ./...         
pramodchoudhari@Pramods-MacBook-Pro-2 my-repo % 

I’m not sure what i’m doing wrong in pre-commit-config.

PS: I’m using this linter for doing static analysis of my repo


your configuration is close but has a few things that can be improved. right now you’re installing a noop golang repository and then running against both ./... (everything in golang speak) and all text files in your repository (probably not what you want!)

first let’s address that noop repository — language: golang instructs pre-commit how it should install the hook itself — in this case you haven’t told it to install anything (a repo: local hook usually uses additional_dependencies to install things)

let’s say you wanted pre-commit to manage the installation (this is, part of the point of pre-commit after all — it manages your installation so you don’t need to instruct your contributors on how to install everything) — for that you’d tell pre-commit to install something like this:

    # ...
    language: golang
    additional_dependencies: []
    # ...

now let’s tackle the files being passed — @jkittner above hits this right on the head but I’ll elaborate a little bit.

pre-commit‘s argument pattern:

your hook should expect to receive the args value and then a list of staged files.

and then from filtering files with types:

text – whether the file looks like a text file

putting those to together, your current configuration is something like running staticcheck -tests=false ./... $(git ls-files) (assuming you only have text files, there isn’t really a good shell way I know of to filter out binary files)

you probably want to filter down to go files, and you probably don’t want to double-lint every file — try this instead:

    # ...
    entry: staticcheck -tests=false
    types: [go]
    # ...

alternatively, if you always want to run everything (which I wouldn’t recommend, it’ll make it slow all the time!) you could instead turn off pre-commit’s processing of files

    # ...
    entry: staticcheck -tests=false ./...
    pass_filenames: false
    always_run: true
    # ...

disclaimer: I wrote pre-commit

Answered By – Anthony Sottile

Answer Checked By – Terry (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.