Repository navigation
PathTool.getRelativeFilePath: fix broken Windows drive-letter regex - #384
Merged
Merged
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.
elharo
force-pushed
the
fix/pathtool-windows-drive-regex
branch
from
July 24, 2026 13:15
3401647 to
43eb1ef
Compare
…t 'any leading slashes'
slachiewicz
approved these changes
Aug 3, 2026
slachiewicz
added a commit
that referenced
this pull request
Sep 10, 2026
#403 changed getRelativeFilePath to return an empty string rather than null when the two paths are on different Windows drives. #384 was branched before that and merged the same day without a rebase, so its new test still asserted null and master has been red since. Do not restore assertNull here: #403 is the newer contract, and two other tests in this class already assert the empty string. Verified: mvn -B verify on JDK 17 -> Tests run: 787, Failures: 0
slachiewicz
added a commit
that referenced
this pull request
Sep 10, 2026
#403 changed getRelativeFilePath to return an empty string rather than null when the two paths are on different Windows drives. #384 was branched before that and merged the same day without a rebase, so its new test still asserted null and master has been red since. Do not restore assertNull here: #403 is the newer contract, and two other tests in this class already assert the empty string. Verified: mvn -B verify on JDK 17 -> Tests run: 787, Failures: 0
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