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

huangapple go评论78阅读模式
英文:

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

问题

这是我的 .pre-commit-config.yaml 文件的内容:

repos:
- repo: local
  hooks:
    - id: static-checks-pramod
      name: 静态分析
      description: 这个钩子执行静态分析
      entry: staticcheck -tests=false ./...
      language: golang
      types: [text]

在本地运行所有文件的钩子时,我得到以下错误:

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

但是,如果我在本地运行 staticcheck 命令,它可以正常工作,如下所示:

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

我不确定在 pre-commit-config 中做错了什么。

PS:我正在使用 这个 linter 对我的代码库进行静态分析。

英文:

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

答案1

得分: 2

在你的.pre-commit-config.yaml文件中,你的types被设置为text,这将会将所有文本文件传递给staticcheck,但它只期望go-files

你可能想要改为types:

英文:

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

答案2

得分: 1

你的配置基本正确,但有一些可以改进的地方。目前,你正在安装一个空的golang存储库,然后针对./...(golang中的所有内容)和存储库中的所有text文件运行。

首先,让我们解决一下空的存储库问题--language: golang指示pre-commit如何安装钩子本身--在这种情况下,你没有告诉它安装任何东西(repo: local钩子通常使用additional_dependencies来安装依赖项)。

假设你希望pre-commit管理安装(这是pre-commit的一部分目的--它管理你的安装,因此你不需要告诉贡献者如何安装所有东西)--为此,你可以告诉pre-commit安装类似于以下内容的东西:

    # ...
    language: golang
    additional_dependencies: [honnef.co/go/tools/cmd/staticcheck@2022.1.2]
    # ...

现在让我们解决一下传递的文件问题--@jkittner上面已经指出了这一点,但我会详细解释一下。

pre-commit参数模式

> 你的钩子应该期望接收args值,然后是一系列暂存的文件。

然后从使用类型过滤文件

> text - 文件是否看起来像文本文件

将它们结合起来,你当前的配置类似于运行staticcheck -tests=false ./... $(git ls-files)(假设你只有文本文件,我不知道有什么好的shell方法来过滤二进制文件)

你可能希望将其过滤为go文件,并且可能不希望对每个文件进行重复检查--可以尝试使用以下配置:

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

或者,如果你始终希望运行所有内容(我不建议这样做,它会使速度变慢!),你可以关闭pre-commit对文件的处理:

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

免责声明:我编写了pre-commit。

英文:

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

huangapple
  • 本文由 发表于 2022年6月14日 12:35:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/72611583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定