Skip to content

PathTool.getRelativeFilePath: fix broken Windows drive-letter regex - #384

Open
elharo wants to merge 5 commits into
masterfrom
fix/pathtool-windows-drive-regex
Open

PathTool.getRelativeFilePath: fix broken Windows drive-letter regex#384
elharo wants to merge 5 commits into
masterfrom
fix/pathtool-windows-drive-regex

Conversation

@elharo

@elharo elharo commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

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:

if (toPath.length() > 2 && toPath.charAt(0) == '\\'
        && Character.isLetter(toPath.charAt(1)) && toPath.charAt(2) == ':')

Fixes #383

@elharo elharo added the bug Something isn't working label Jul 1, 2026
@elharo
elharo requested review from Copilot and rfscholte July 24, 2026 10:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.isLetter prefix 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.

Comment thread src/test/java/org/apache/maven/shared/utils/PathToolTest.java Outdated
Comment thread src/main/java/org/apache/maven/shared/utils/PathTool.java Outdated
@elharo
elharo force-pushed the fix/pathtool-windows-drive-regex branch from 3401647 to 43eb1ef Compare July 24, 2026 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PathTool.getRelativeFilePath broken Windows drive letter regex

2 participants