Skip to content

fix: handle ValueError from relpath on Windows cross-drive paths#3659

Closed
bobo-xxx wants to merge 1 commit intopre-commit:mainfrom
bobo-xxx:clawoss/fix/pre-commit-2530-cross-drive-relpath
Closed

fix: handle ValueError from relpath on Windows cross-drive paths#3659
bobo-xxx wants to merge 1 commit intopre-commit:mainfrom
bobo-xxx:clawoss/fix/pre-commit-2530-cross-drive-relpath

Conversation

@bobo-xxx
Copy link
Copy Markdown

Description

When the config file (or other paths) is on a different Windows drive than the git repository, os.path.relpath raises a ValueError because it cannot compute a relative path between paths on different drives.

This change wraps all relpath calls in _adjust_args_and_chdir with ValueError suppression, keeping the absolute path when relpath fails.

Fixes #2530

Testing

Added a regression test test_adjust_args_and_chdir_relpath_valueerror_cross_drive that simulates the cross-drive ValueError scenario.

All existing tests pass (29 passed, 1 skipped on Linux).

When the config file (or other paths) is on a different Windows drive
than the git repository, os.path.relpath raises a ValueError because
it cannot compute a relative path between paths on different drives.

This change wraps all relpath calls in _adjust_args_and_chdir with
ValueError suppression, keeping the absolute path when relpath fails.

Fixes pre-commit#2530
Copilot AI review requested due to automatic review settings April 17, 2026 19:05
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a Windows-specific failure where os.path.relpath() raises ValueError for cross-drive paths by falling back to absolute paths instead of crashing.

Changes:

  • Add a ValueError fallback for relpath operations performed during _adjust_args_and_chdir.
  • Introduce _try_relpath() and use it for file path normalization.
  • Add a regression test covering the cross-drive ValueError scenario.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
pre_commit/main.py Suppresses ValueError from os.path.relpath() during argument normalization and adds _try_relpath() helper.
tests/main_test.py Adds a regression test ensuring _adjust_args_and_chdir does not fail when relpath raises ValueError.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pre_commit/main.py
Comment on lines +177 to +178
# on windows, different drives raises ValueError
try:
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammar in the comment: “different drives raises ValueError” should be “different drives raise ValueError”.

Copilot uses AI. Check for mistakes.
Comment thread tests/main_test.py

def test_adjust_args_and_chdir_relpath_valueerror_cross_drive(in_git_dir):
# regression test for https://github.com/pre-commit/pre-commit/issues/2530
# on windows, different drives raises ValueError from relpath
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammar in the comment: “different drives raises ValueError from relpath” should be “different drives raise ValueError from relpath”.

Suggested change
# on windows, different drives raises ValueError from relpath
# on windows, different drives raise ValueError from relpath

Copilot uses AI. Check for mistakes.
Comment thread pre_commit/main.py
Comment on lines +200 to +216
with contextlib.suppress(ValueError):
# on windows, different drives raises ValueError
args.config = os.path.relpath(args.config)
if args.command in {'run', 'try-repo'}:
args.files = [os.path.relpath(filename) for filename in args.files]
args.files = [
_try_relpath(filename) for filename in args.files
]
if args.commit_msg_filename is not None:
args.commit_msg_filename = os.path.relpath(
args.commit_msg_filename,
)
with contextlib.suppress(ValueError):
# on windows, different drives raises ValueError
args.commit_msg_filename = os.path.relpath(
args.commit_msg_filename,
)
if args.command == 'try-repo' and os.path.exists(args.repo):
args.repo = os.path.relpath(args.repo)
with contextlib.suppress(ValueError):
# on windows, different drives raises ValueError
args.repo = os.path.relpath(args.repo)
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_try_relpath() is only used for args.files, while args.config, args.commit_msg_filename, and args.repo each reimplement the same ValueError fallback with separate contextlib.suppress(...) blocks. Consider using _try_relpath() consistently for all relpath conversions here to reduce repetition and keep the behavior uniform.

Copilot uses AI. Check for mistakes.
@asottile
Copy link
Copy Markdown
Member

ai slop is not acceptable

@asottile asottile closed this Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

4 participants