You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "elharo (via GitHub)" <gi...@apache.org> on 2026/07/01 12:38:14 UTC
[PR] PathTool.getRelativeFilePath: fix broken Windows drive-letter regex [maven-shared-utils]
elharo opened a new pull request, #384:
URL: https://github.com/apache/maven-shared-utils/pull/384
The regex `^\\[a-zA-Z]:` at lines 146 and 149 had two bugs:
1. The `\\[` matched a literal `[` character, not a backslash, because `[` was escaped by `\`
2. `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 path
The Windows drive-letter normalization code was effectively dead. Paths like `\C:\foo` (produced by `File.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 of `null`).
**Fix:** Replaced the broken regex with a simple character-level check:
```java
if (toPath.length() > 2 && toPath.charAt(0) == '\\'
&& Character.isLetter(toPath.charAt(1)) && toPath.charAt(2) == ':')
```
Fixes https://github.com/apache/maven-shared-utils/issues/383
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] PathTool.getRelativeFilePath: fix broken Windows drive-letter regex [maven-shared-utils]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #384:
URL: https://github.com/apache/maven-shared-utils/pull/384#discussion_r3644891641
##########
src/test/java/org/apache/maven/shared/utils/PathToolTest.java:
##########
@@ -102,6 +102,15 @@ public void testGetRelativePath2Parm() {
assertEquals("", PathTool.getRelativePath("/usr/local/java/bin/java.sh", "/usr/local/"));
}
+ @Test
+ public void testGetRelativeFilePathWithDifferentWindowsDrives() {
+ // Tests that the regex fix at line 146 correctly strips a leading backslash
+ // before a Windows drive letter. The old regex "^\\[a-zA-Z]:" matched the
+ // literal string "[a-zA-Z]:" instead of a backslash + drive letter.
+ // Different drives with leading backslash should return null.
Review Comment:
The test comment is now outdated and brittle: it refers to a “regex fix” and a specific line number. The implementation was changed to a character-level check, and line numbers will drift over time, making this comment misleading.
##########
src/main/java/org/apache/maven/shared/utils/PathTool.java:
##########
@@ -143,10 +143,16 @@ public static String getRelativeFilePath(final String oldPath, final String newP
String toPath = new File(newPath).getPath();
// strip any leading slashes if its a windows path
- if (toPath.matches("^\\[a-zA-Z]:")) {
+ if (toPath.length() > 2
Review Comment:
This comment says “strip any leading slashes” and uses “its”/“windows”, but the code now specifically strips a leading backslash before a Windows drive letter. Updating the comment avoids confusion about what input shapes are being normalized.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] PathTool.getRelativeFilePath: fix broken Windows drive-letter regex [maven-shared-utils]
Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo merged PR #384:
URL: https://github.com/apache/maven-shared-utils/pull/384
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org