在运行Golang Repo的预加入挂钩时会出现错误[命名文件必须为.go文件:。/...]

发布于 2025-02-06 23:38:20 字数 1257 浏览 3 评论 0原文

这是我的 .pre-commit-config.yam l文件的限制,

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]

在本地运行挂钩的所有文件时,本地im会遇到的错误,

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 % 

我不确定我在预订案中做错了什么。

PS:我正在使用 this 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难理解 2025-02-13 23:38:20

在您的.pre-commit-config.yaml您的types设置为text,它将将所有像文件之类的文本传递给staticcheck,但它仅期望go-files

您可能想要类型:[GO]而不是。

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.

淡紫姑娘! 2025-02-13 23:38:20

您的配置是 close ,但有一些可以改进的东西。现在,您正在安装一个NOOP Golang存储库,然后对两个。/...(Golang speak中的所有内容)和所有text> text您的存储库中的文件(可能不是您想要的!)

首先让我们来地址NOOP存储库 - 语言:Golang指示pre-Commit如何安装挂钩本身 -在这种情况下,您尚未告诉它安装任何内容(repo:local Hook通常使用affe_deppedencies安装东西),

例如,您想让您想要预先支配来管理安装(毕竟这是预先承诺的一部分 - 它可以管理您的安装,因此您无需指示贡献者如何安装所有内容)这样:

    # ...
    language: golang
    additional_dependencies: [honnef.co/go/tools/cmd/[email protected]]
    # ...

现在让我们解决要通过的文件 - 上面的@jkittner击中了这一点,但我会详细说明。

pre-commit's 参数模式

您的钩子应该期望接收args值,然后是上演文件列表。

然后从使用类型的过滤文件:

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

,将这些文件放在一起,您的当前配置就像运行staticcheck -tests = false。/... $(git ls -files)< /code>(假设您只有文本文件,我实际上没有一个很好的外壳方法来过滤二进制文件),

您可能想过滤到go files,而您可能不想double-lint每个文件 - 而是尝试一下:

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

或者,如果您始终想运行所有内容(我不建议这样做,它会一直使其一直放慢!),您可以关闭Pre-pre-提交对文件

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

免责声明的处理:我写了预订

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/[email protected]]
    # ...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文