PathTool.getRelativeFilePath: fix broken Windows drive-letter regex - #384
Open
elharo wants to merge 5 commits into
Open
PathTool.getRelativeFilePath: fix broken Windows drive-letter regex#384elharo wants to merge 5 commits into
elharo wants to merge 5 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes Windows drive-letter handling in PathTool.getRelativeFilePath() by replacing a broken String.matches()-based regex with a direct character-level prefix check, restoring the intended drive normalization and cross-drive mismatch behavior.
Changes:
- Replaced the non-matching Windows drive-letter regex with a simple
charAt/Character.isLetterprefix check for\C:-style paths. - Added a regression test ensuring paths on different Windows drives (with a leading backslash) return
null.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/main/java/org/apache/maven/shared/utils/PathTool.java | Replaces the broken regex-based Windows drive-letter detection with a character-level check to correctly strip a leading \ before C:. |
| src/test/java/org/apache/maven/shared/utils/PathToolTest.java | Adds a regression test covering cross-drive relative path behavior when paths include a leading backslash before the drive letter. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
elharo
force-pushed
the
fix/pathtool-windows-drive-regex
branch
from
July 24, 2026 13:15
3401647 to
43eb1ef
Compare
…t 'any leading slashes'
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.
The regex
^\\[a-zA-Z]:at lines 146 and 149 had two bugs:\\[matched a literal[character, not a backslash, because[was escaped by\String.matches()requires the entire string to match the pattern, but this regex was intended to match only the leading\C:prefix — so it never matched any real pathThe Windows drive-letter normalization code was effectively dead. Paths like
\C:\foo(produced byFile.getPath()on Windows) were never getting their leading\stripped, causing the drive-letter comparison logic at lines 154–175 to be skipped. This could produce incorrect relative paths or fail to detect mismatched drives (returning a path instead ofnull).Fix: Replaced the broken regex with a simple character-level check:
Fixes #383