fix(toolshed): keep the pre-commit-installed check advisory instead of failing the run - #2548
Open
LeSingh1 wants to merge 1 commit into
Open
fix(toolshed): keep the pre-commit-installed check advisory instead of failing the run#2548LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
The module docstring says this check should "Warn (without failing)", and
main() returns 0 on both of its branches. Three paths contradict that:
1. `_git_hooks_dir` runs `git rev-parse --git-path hooks` with `check=True`.
Outside a work tree git exits 128, so the advisory hook raises
CalledProcessError and fails the whole `pre-commit run`.
2. If git is not on PATH, `subprocess.run` raises FileNotFoundError. That is
not the FileNotFoundError the code catches -- that one guards `open()` --
so it escapes as a traceback.
3. The `open()` guard catches only FileNotFoundError, so any other reason the
hooks path is unreadable (a directory, a dangling symlink, restrictive
permissions) also escapes.
All three were reproduced by calling `main()` directly:
outside a git repo -> CalledProcessError: ... exit status 128
git missing on PATH -> FileNotFoundError: [Errno 2] ... 'git'
hooks/pre-commit is a directory -> IsADirectoryError: [Errno 21]
The hook is configured with `always_run: true`, so it fires on every local
`pre-commit run`. Any of these turns a nudge to run `pre-commit install` into
a hard failure of an unrelated commit.
Treat "cannot determine whether the hook is installed" as "not installed":
that is the situation the warning already describes, and it is the only
outcome consistent with a check that cannot block.
Adds tests under toolshed/tests/ covering the installed, missing,
foreign-hook, no-work-tree, no-git, and unreadable-path cases.
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The module docstring is explicit about the contract:
and
main()returns0on both of its branches. Three paths contradict that.1.
check=Trueon a command that legitimately fails.git rev-parseexits 128 outside a work tree, so the advisory check raisesCalledProcessErrorand fails the wholepre-commit run.2. Missing
githits the wrong handler. Ifgitis not onPATH,subprocess.runraisesFileNotFoundError— which is not theFileNotFoundErrorthe code catches. That one guardsopen(), further down. So it escapes as a traceback.3. The
open()guard is too narrow. It catches onlyFileNotFoundError, so any other reason the hooks path is unreadable — a directory, a dangling symlink, restrictive permissions — also escapes.All three reproduced by calling
main()directly:The hook is configured with
always_run: trueandpass_filenames: false, so it fires on every localpre-commit run. Any of these turns a nudge to runpre-commit installinto a hard failure of an otherwise-fine commit — and the failure text points atgit rev-parse, not at anything the contributor did.Fix
Treat "cannot determine whether the hook is installed" as "not installed". That is exactly the situation the existing warning describes, and it is the only outcome consistent with a check that by design cannot block.
_git_hooks_dir()returnsNoneonOSErrororsubprocess.SubprocessError(covering both the exit-128 and the no-gitcases) and on an empty answer.open()guard widens fromFileNotFoundErrortoOSError.No change when git answers normally and the hook is present or absent — the existing quiet/warn behavior is untouched.
Tests
toolshed/check_precommit_installed.pyhad no tests. Addedtoolshed/tests/test_check_precommit_installed.pycovering:monkeypatch.chdir(tmp_path));gitmissing fromPATH(monkeypatchedsubprocess.run, so nogitbinary is required and the test is hermetic);The new directory is added to the existing nightly tooling job alongside
ci/tools/tests.Verification
Executed in full (pure Python, no GPU, no CUDA):
ruff check/ruff format --checkclean on both changed Python files; index verified clean before committing (restore done withcpaside +git show upstream/main:<path> >, nevergit checkout --).