问题描述
在 repo 中设置的预提交钩子之一是 yapf (.pre-commit-config.yaml
):
repos:
# Note: the YAPF config is found in `.style.yapf` and `.yapfignore`
- repo: https://github.com/pre-commit/mirrors-yapf
rev: v0.29.0
hooks:
- id: yapf
每当我对包含在 .yapfignore
中的文件进行更改并运行预提交挂钩时,挂钩都会失败:
pre-commit run yapf
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to ****************
yapf.....................................................................Failed
- hook id: yapf
- exit code: 1
yapf: Input filenames did not match any python files
有谁知道如何避免 yapf 对 .yapfignore
中包含的文件失败?
解决方法
好的,问题的原因是 yapf 将 .yapfignore
中的文件视为不存在的文件,因此您会得到:
yapf: Input filenames did not match any python files
如果您对 .yapfignore
文件中的任何文件运行 yapf,则会出错。为了解决 pre-commit
中的这个问题,我添加了一个不在 .yapfignore
中的现有 python 文件作为参数,因此 yapf 总是有一个文件要运行。
在您的输出和您看到的行为中发生了一些事情,因此我将分别解释它们:
$ pre-commit run yapf
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to ****************
此处的输出表明 pre-commit
接受了您未暂存的更改并丢弃了它们,可能没有显示您的预期。在演示正在发生的事情时,您可能需要 pre-commit run yapf --all-files
进入实际问题。
pre-commit 不知道您使用的任何工具的实现细节(在某种程度上,yapf
也不知道!)
当文件在 .yapfignore
中并且您将其传递给 yapf
时,它的行为就好像它不存在一样(这很奇怪!)
$ tail -n999 hello_wat.py .yapfignore
==> hello_wat.py <==
x = 5+ 4
==> .yapfignore <==
hello*.py
$ yapf hello_wat.py
yapf: Input filenames did not match any python files
您可能想要做的是利用 pre-commit
的 exclude
,以便文件永远不会传递给 yapf
!以我的例子为例(你没有分享你的 .yapfignore
或最小的案例)
repos:
- repo: https://github.com/pre-commit/mirrors-yapf
rev: v0.29.0
hooks:
- id: yapf
exclude: ^hello\.*\.py$
现在 yapf 不会针对 hello*.py
运行!
免责声明:我是 pre-commit 的创建者。