2

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

repos:
- repo: local
  hooks:
    - 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

torek
  • 448,244
  • 59
  • 642
  • 775
pramod
  • 117
  • 1
  • 9
  • 2
    I assume you're referring to the pre-commit.com hooks here (in which case [tag:gitlab] is irrelevant). The author of this precommit hook reads SO questions if they're tagged correctly, so I have updated your tags. – torek Jun 14 '22 at 09:09
  • 1
    Meanwhile, given the assumptions I've made, the *problem* is clear enough: pre-commit.com attempts to run linters and the like only on *specific files*, rather than on your entire source tree. But for Go linters, `./...` means *run on all the files*. So you're getting a combination of: *Hey, Go linter, run on only these specific files, and run on all the Go files.* These two instructions are contradictory so the linter gives you the error you're getting. You'll need to decide whether to run on all files (and if so, figure out how to stop pre-commit.com from being so helpful) or not. – torek Jun 14 '22 at 09:13

2 Answers2

2

In your .pre-commit-config.yaml your types is set to text, which will pass all text like files to staticcheck , but it only expects go-files.

You probably want types: [go] instead.

YesThatIsMyName
  • 1,585
  • 3
  • 23
  • 30
jkittner
  • 31
  • 1
  • 2
1

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: [honnef.co/go/tools/cmd/staticcheck@2022.1.2]
    # ...

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

anthony sottile
  • 61,815
  • 15
  • 148
  • 207