Skip to content

BUG: pre-commit hook gets invoked after file deletion and crashes if excluded file does not exist #3687

@gothicVI

Description

@gothicVI

After having deleted a file, the pre-commit hook fails with

codespell................................................................Failed
- hook id: codespell
- exit code: 1

Traceback (most recent call last):
  File "/home/user/.cache/pre-commit/repoga7ec3e6/py_env-python3.11/bin/codespell", line 8, in <module>
    sys.exit(_script_main())
             ^^^^^^^^^^^^^^
  File "/home/user/.cache/pre-commit/repoga7ec3e6/py_env-python3.11/lib/python3.11/site-packages/codespell_lib/_codespell.py", line 1102, in _script_main
    return main(*sys.argv[1:])
           ^^^^^^^^^^^^^^^^^^^
  File "/home/user/.cache/pre-commit/repoga7ec3e6/py_env-python3.11/lib/python3.11/site-packages/codespell_lib/_codespell.py", line 1261, in main
    build_exclude_hashes(exclude_file, exclude_lines)
  File "/home/user/.cache/pre-commit/repoga7ec3e6/py_env-python3.11/lib/python3.11/site-packages/codespell_lib/_codespell.py", line 726, in build_exclude_hashes
    with open(filename, encoding="utf-8") as f:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'block_reason.txt'

where

$ git status 
On branch dev
Your branch is up to date with 'origin/dev'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	deleted:    block_reason.txt

and

$ cat .pre-commit-config.yaml
...
- repo: https://github.com/codespell-project/codespell
  rev: v2.4.1
  hooks:
  - id: codespell
    args: ['--exclude-file', 'block_reason.txt']
...

when removing the args part it works again but I think it might be worth wile to either ignore the argument if the file does not exist and emit a warning and (or) not at all invoke pre-commit on deleted files.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions