Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gothicVI opened this issue Apr 11, 2025 · 7 comments · May be fixed by #3688
Open

Comments

@gothicVI
Copy link

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.

@DimitriPapadopoulos
Copy link
Collaborator

But codespell doesn't invoke pre-commit on deleted files, does it?

@DimitriPapadopoulos
Copy link
Collaborator

DimitriPapadopoulos commented Apr 11, 2025

And codespell doesn't fail in this case:

$ codespell /non/existent/file
$ 

Could you perhaps investigate further and reproduce the issue outside of pre-commit?

EDIT: Got it, sorry about the misunderstanding:

$ codespell --exclude-file /non/existent/file
Traceback (most recent call last):
  File "/home/dp165978/.local/bin/codespell", line 8, in <module>
    sys.exit(_script_main())
             ^^^^^^^^^^^^^^
  File "/volatile/src/codespell/codespell_lib/_codespell.py", line 1102, in _script_main
    return main(*sys.argv[1:])
           ^^^^^^^^^^^^^^^^^^^
  File "/volatile/src/codespell/codespell_lib/_codespell.py", line 1261, in main
    build_exclude_hashes(exclude_file, exclude_lines)
  File "/volatile/src/codespell/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: '/non/existent/file'
$ 

@DimitriPapadopoulos
Copy link
Collaborator

DimitriPapadopoulos commented Apr 11, 2025

We could catch the FileNotFoundError and emit an error message. However, I believe codespell should still exit when passing a config file that does not exist as an argument. That's what other commands do:

$ ruff --config /non/existent/file
error: invalid value '/non/existent/file' for '--config <CONFIG_OPTION>'

  tip: A `--config` flag must either be a path to a `.toml` configuration file
       or a TOML `<KEY> = <VALUE>` pair overriding a specific configuration
       option

It looks like you were trying to pass a path to a configuration file.
The path `/non/existent/file` does not point to a configuration file

For more information, try '--help'.
$ 
$ grep --file=/non/existent/file 
grep: /non/existent/file: No such file or directory
$ 

@gothicVI
Copy link
Author

Thanks for the quick reply!

But codespell doesn't invoke pre-commit on deleted files, does it?

No, it does not.

We could catch the FileNotFoundError and emit an error message. However, I believe codespell should still exit when passing a config file that does not exist as an argument.

I think there's a misunderstanding: I totally do agree that exiting with an error is fine if the config file is missing. However, here we have a valid config but only one of the specified files within the config does not exist. I'd argue that this is not necessarily a reason to exit with a non-zero exit code - though of course it's up to you to decide ;)

The following for example exits without an error:

$ ruff check --exclude /this/does/not/exist

@luzpaz

This comment has been minimized.

@DimitriPapadopoulos
Copy link
Collaborator

DimitriPapadopoulos commented Apr 12, 2025

@luzpaz Looks similar but unrelated to me: the pyproject.toml file appears to be corrupted and tomllib raises an exception — which codespell doesn't catch.

EDIT: I would create a new issue for that. What's your opinion? Should codespell exit, or just emit a warning and keep going?

@luzpaz
Copy link
Collaborator

luzpaz commented Apr 13, 2025

What's your opinion? Should codespell exit, or just emit a warning and keep going?

Not sure what the implications for ignoring the warning would be...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants