简体   繁体   English

运行 golang repo 的预提交挂钩时出错 [命名文件必须是 .go 文件:./...]

[英]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.yam l file,这是我的.pre-commit-config.yaml文件的内容,

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,但是如果我在本地运行 staticcheck 命令,它可以正常工作,如下所示,

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 PS:我正在使用这个linter 对我的 repo 进行静态分析

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 .在您的.pre-commit-config.yaml ,您的types设置为text ,这会将所有类似文件的文本传递给staticcheck ,但它只需要go-files

You probably want types: [go] instead.您可能需要types: [go]代替。

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!)现在您正在安装 noop golang存储库,然后针对./... (golang 中的所有内容)和存储库中的所有text文件(可能不是您想要的!)

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)首先让我们解决 noop 存储库—— language: golang指示pre-commit它应该如何安装钩子本身——在这种情况下,你没有告诉它安装任何东西(一个repo: local钩子通常使用additional_dependencies依赖项来安装东西)

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:假设您希望预先提交来管理安装(毕竟这是预先提交的一部分——它管理你的安装,所以你不需要指导你的贡献者如何安装所有东西)——对于你会告诉 pre-commit 安装这样的东西:

    # ...
    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.现在让我们来处理正在传递的文件——上面的@jkittner 很清楚这一点,但我会详细说明一下。

pre-commit 's argument pattern : pre-commit参数模式

your hook should expect to receive the args value and then a list of staged files.您的钩子应该期望收到args值,然后是暂存文件列表。

and then from filtering files with types :然后从过滤文件类型

text - whether the file looks like a text file text - 文件是否看起来像文本文件

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)把它们放在一起,你当前的配置就像运行staticcheck -tests=false ./... $(git ls-files) (假设你只有文本文件,我知道没有一个好的shell方式过滤掉二进制文件)

you probably want to filter down to go files, and you probably don't want to double-lint every file -- try this instead:您可能想要过滤到 go 文件,并且您可能不想对每个文件都进行双重检查 - 试试这个:

    # ...
    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或者,如果你总是想运行所有东西(我不建议这样做,它会一直让它变慢!)你可以改为关闭 pre-commit 对文件的处理

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

disclaimer: I wrote pre-commit免责声明:我写了预提交

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM