Skip to content

Fix: os.path.relpath (ntpath.relpath on Windows) raises ValueError when the... - #3742

Closed
M001N wants to merge 2 commits into
pre-commit:mainfrom
M001N:oss-engine/584ad7b9-6340e127
Closed

Fix: os.path.relpath (ntpath.relpath on Windows) raises ValueError when the...#3742
M001N wants to merge 2 commits into
pre-commit:mainfrom
M001N:oss-engine/584ad7b9-6340e127

Conversation

@M001N

@M001N M001N commented Aug 10, 2026

Copy link
Copy Markdown

Summary

Factored out a _relpath_or_abspath(path) helper that tries os.path.relpath and falls back to os.path.abspath on ValueError. Applied it uniformly to args.config, args.files, args.commit_msg_filename, and args.repo in _adjust_args_and_chdir, replacing the previous config-only try/except.

Problem

pre-commit/pre-commit issue reference: #2530

Root Cause

os.path.relpath (ntpath.relpath on Windows) raises ValueError when the target path and the current working directory are on different drives/mounts. _adjust_args_and_chdir in pre_commit/main.py calls relpath on args.config, args.files, args.commit_msg_filename, and args.repo after chdir'ing to the git toplevel — all four share the identical cross-drive precondition, but only args.config was previously guarded (from the prior attempt); files/commit_msg_filename/repo were left unguarded.

Testing

PASS - all 32 tests in tests/main_test.py pass, including the 3 cross-drive ValueError regression tests

Related Issue

#2530

@asottile asottile closed this Aug 10, 2026
@pre-commit pre-commit locked as spam and limited conversation to collaborators Aug 10, 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.

2 participants