fix: handle cross-drive ValueError in _adjust_args_and_chdir on Windows#3658
Closed
terminalchai wants to merge 1 commit intopre-commit:mainfrom
Closed
fix: handle cross-drive ValueError in _adjust_args_and_chdir on Windows#3658terminalchai wants to merge 1 commit intopre-commit:mainfrom
terminalchai wants to merge 1 commit intopre-commit:mainfrom
Conversation
On Windows, os.path.relpath() raises ValueError when the path and the current working directory are on different drives (e.g. config on C: but the git repo is on D:). Introduce a small _try_relpath() helper that wraps os.path.relpath in a try/except ValueError and returns the original path unchanged when the drives differ. All four os.path.relpath calls in _adjust_args_and_chdir (config, files, commit_msg_filename, repo) are updated to use this helper, so every cross-drive combination is covered. Fixes pre-commit#2530
Member
|
sorry we don't accept ai slop prs |
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.
Summary
Fixes #2530
On Windows, os.path.relpath() raises ValueError when the path and the current working directory are on different drives — e.g. a config at C:\Users\me.pre-commit-config.yaml while the git repo lives on D:. This causes an unhandled traceback instead of a helpful error message.
Root cause
_adjust_args_and_chdir calls os.path.relpath() unconditionally on �rgs.config, �rgs.files, �rgs.commit_msg_filename, and �rgs.repo after doing os.chdir(toplevel). When the git root and the path being relativised are on different Windows drives, Python raises:
\
ValueError: path is on mount 'C:', start on mount 'D:'
\\
Fix
Introduce a small _try_relpath() helper (as suggested by @asottile in #2530 — option 2):
\\python
def _try_relpath(path: str) -> str:
try:
return os.path.relpath(path)
except ValueError:
return path
\\
All four os.path.relpath calls in _adjust_args_and_chdir are updated to use _try_relpath, so every cross-drive combination (config, files list, commit-msg filename, try-repo repo path) is covered. On POSIX and same-drive Windows the behaviour is identical to before.
Tests added