fix: keep absolute path when os.path.relpath raises ValueError on Windows cross-drive - #3739
Closed
zerafachris wants to merge 1 commit into
Closed
Conversation
… on Windows 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 repository root is on D:\). _adjust_args_and_chdir called os.path.relpath unconditionally after chdir-ing to the git root, so running pre-commit with --config pointing to a different drive crashed with a ValueError instead of falling back gracefully. Add a _relpath() helper that catches ValueError and returns the original (absolute) path. Replace all four os.path.relpath() call-sites in _adjust_args_and_chdir with _relpath() so that cross-drive paths are kept as absolute paths rather than raising an unhandled exception. Fixes pre-commit#2530 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Fixes #2530.
Problem
On Windows,
os.path.relpathraisesValueErrorwhen the start pathand the target path are on different drives — for example when
--configpoints toC:\configs\pre-commit-config.yamlbut the gitrepository root is on
D:\._adjust_args_and_chdircallsos.path.relpathunconditionally afteros.chdir(toplevel), so the combination of a cross-drive config (or--commit-msg-filename/try-reporepo path) and a git repo on adifferent drive crashed with an unhandled
ValueErrorinstead offalling back gracefully.
Fix
Add a
_relpath()helper that wrapsos.path.relpathand catchesValueError, returning the original path unchanged (keeping itabsolute). All four
os.path.relpathcall-sites in_adjust_args_and_chdirare replaced with_relpath().On same-drive paths the behaviour is identical to before. On
cross-drive paths the absolute path is preserved, which is valid since
absolute paths work regardless of the current directory.
Tests
test_relpath_falls_back_on_cross_drive_valueerror— unit test thatdirectly verifies
_relpathreturns the original path whenos.path.relpathraisesValueError.test_relpath_returns_relative_path_on_same_drive— verifies thenormal (non-Windows-cross-drive) code path still works.
All existing
main_test.pytests continue to pass (30 passed, 1skipped on non-Windows).