Skip to content

fix: keep absolute path when os.path.relpath raises ValueError on Windows cross-drive - #3739

Closed
zerafachris wants to merge 1 commit into
pre-commit:mainfrom
zerafachris:fix/cross-drive-config-path-windows
Closed

fix: keep absolute path when os.path.relpath raises ValueError on Windows cross-drive#3739
zerafachris wants to merge 1 commit into
pre-commit:mainfrom
zerafachris:fix/cross-drive-config-path-windows

Conversation

@zerafachris

Copy link
Copy Markdown

Fixes #2530.

Problem

On Windows, os.path.relpath raises ValueError when the start path
and the target path are on different drives — for example when
--config points to C:\configs\pre-commit-config.yaml but the git
repository root is on D:\.

_adjust_args_and_chdir calls os.path.relpath unconditionally after
os.chdir(toplevel), so the combination of a cross-drive config (or
--commit-msg-filename / try-repo repo path) and a git repo on a
different drive crashed with an unhandled ValueError instead of
falling back gracefully.

Fix

Add a _relpath() helper that wraps os.path.relpath and catches
ValueError, returning the original path unchanged (keeping it
absolute). All four os.path.relpath call-sites in
_adjust_args_and_chdir are 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 that
    directly verifies _relpath returns the original path when
    os.path.relpath raises ValueError.
  • test_relpath_returns_relative_path_on_same_drive — verifies the
    normal (non-Windows-cross-drive) code path still works.

All existing main_test.py tests continue to pass (30 passed, 1
skipped on non-Windows).

… 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>
@asottile asottile closed this Aug 6, 2026
@pre-commit pre-commit locked as spam and limited conversation to collaborators Aug 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

ValueError on Windows when config is on a different drive than the git repo

2 participants