Use File(URI).toPath() to fix Windows drive letter handling#6993
Use File(URI).toPath() to fix Windows drive letter handling#6993pdelagrave merged 1 commit intomainfrom
Conversation
URI.getPath() on file:///C:/... returns /C:/... which Paths.get(String) cannot handle on Windows. new File(URI).toPath() correctly decodes percent-encoded non-ASCII characters AND handles Windows drive letters.
| if ("file".equals(scheme)) { | ||
| // A maven repository can be expressed as a URI with a file scheme | ||
| Path path = Paths.get(URI.create(baseUri + "maven-metadata-local.xml").getPath()); | ||
| Path path = new File(URI.create(baseUri + "maven-metadata-local.xml")).toPath(); |
There was a problem hiding this comment.
If you drop the getPath on the URI that should fix this.
ie.
Paths.get(URI.create(baseUri + "maven-metadata-local.xml"))
There was a problem hiding this comment.
But that also just reverts the original change to unescape non ascii chars like ü.
There was a problem hiding this comment.
I saw that late sorry (CC was waiting for the workflow to go green before merging, and had no instruction to check for new comments before merging, updated CLAUDE.md now).
That's what we used to do until this PR last week: #6960
There was a problem hiding this comment.
I'm observing that Java NIO Path does unescape the URI string correctly using the original code, so I think we're still missing something that's not being addressed by either of these two PRs.
There was a problem hiding this comment.
@Test
void fileUriWithNonAsciiPath(@TempDir Path tempDir) throws Exception {
Path repo = tempDir.resolve("müller/.m2/repository");
String uri = "file://" + repo.toAbsolutePath() + "/";
assertThatThrownBy(() -> Paths.get(URI.create(uri)))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Bad escape");
}
@Test
void fileUriWithNonAsciiPath(@TempDir Path tempDir) throws Exception {
Path repo = tempDir.resolve("müller/.m2/repository");
String uri = "file://" + repo.toAbsolutePath() + "/";
assertDoesNotThrow(() -> new File(URI.create(uri)).toPath());
}
Problem
v8.75.6 changed
Paths.get(URI.create(...))toPaths.get(URI.create(...).getPath())to fix percent-encoded non-ASCII characters in file URIs. However,URI.getPath()onfile:///C:/Users/...returns/C:/Users/...— the leading/before the drive letter causesPaths.get(String)to fail on Windows.Solution
Replace
Paths.get(URI.create(...).getPath())withnew File(URI.create(...)).toPath()in all three locations.new File(URI)internally callsURI.getPath()for percent-decoding and thenfs.fromURIPath()which strips the leading/on Windows drive letters — handling both non-ASCII characters and Windows paths correctly.