You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "gnodet (via GitHub)" <gi...@apache.org> on 2026/10/22 12:45:25 UTC

[PR] Fix Windows path with drive letter lost in RepositoryUriUtils.basedir() [maven-resolver]

gnodet opened a new pull request, #2224:
URL: https://github.com/apache/maven-resolver/pull/2224

   ## Problem
   
   `RepositoryUriUtils.basedir()` treats the Windows drive letter (e.g. `C`) as a URL scheme, because `protocol()` returns `"C"` for `"C:\\Users\\..."`. The `if (!protocol.isEmpty())` branch then strips it, producing a drive-relative path (`\\Users\\...`) instead of an absolute path.
   
   `new File("\\Users\\...").toURI()` resolves that against `new File(".").getAbsolutePath()`, i.e. the JVM working directory. On Windows GitHub Actions runners, the working directory is on `D:` while `java.io.tmpdir` is on `C:`, so a settings.xml `<localRepository>C:\\Users\\runneradmin\\...\\repo</localRepository>` round-trips as `D:\\Users\\RUNNER~1\\...\\repo`, causing path comparison failures.
   
   This is the root cause behind the Windows CI failures in apache/maven#13677 (where `new LocalRepository(settings.getLocalRepository())` goes through this code path).
   
   ## Fix
   
   Two changes in `RepositoryUriUtils`:
   
   1. **`basedir()`**: detect when the "protocol" is a single uppercase letter (Windows drive letter) and return the URL unchanged instead of stripping it.
   
   2. **`toUri()`**: switch from `new File(basedir(url)).toURI()` to `Paths.get(basedir(url)).toUri()` so that path parsing is fully lexical and never involves `File`'s working-directory-relative resolution.
   
   ## Test
   
   Added a Windows-only test in `LocalRepositoryTest` that creates a `LocalRepository` from a plain `C:\\...` path string and asserts the drive letter is preserved in `getBasePath()`.
   
   - [x] I hereby declare this contribution to be licensed under the Apache License Version 2.0, January 2004
   - [x] I hereby declare that I have filed an Apache Individual Contributor License Agreement.


-- 
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] Fix Windows path with drive letter lost in RepositoryUriUtils.basedir() [maven-resolver]

Posted by "gnodet-bot (via GitHub)" <gi...@apache.org>.
gnodet-bot commented on code in PR #2224:
URL: https://github.com/apache/maven-resolver/pull/2224#discussion_r4357000579


##########
maven-resolver-api/src/test/java/org/eclipse/aether/repository/RepositoryUriUtilsTest.java:
##########
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eclipse.aether.repository;
+
+import java.net.URI;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Tests for {@link RepositoryUriUtils#toUri(String)}, focusing on Windows drive-letter handling.
+ * These tests exercise the string-parsing logic and run on all platforms.
+ */
+public class RepositoryUriUtilsTest {
+
+    /**
+     * A plain Windows path with an uppercase drive letter must produce a {@code file:} URI
+     * whose path starts with the drive letter, not a relative path missing the drive.
+     */
+    @Test
+    void windowsUppercaseDriveLetterPreserved() {
+        URI uri = RepositoryUriUtils.toUri("C:\\Users\\foo\\repo");

Review Comment:
   πŸ”§ **Nit β€” Cross-platform test coverage gap**: `RepositoryUriUtilsTest` runs on all platforms (no `@EnabledOnOs` annotation), but the three Windows-path tests call `toUri("C:\\Users\\foo\\repo")` on Linux, where `Paths.get("C:\\Users\\foo\\repo")` treats the input as a *relative* path (backslash is not a path separator on Linux). The resulting URI resolves against CWD and looks like `file:///cwd/C%3A%5CUsers%5Cfoo%5Crepo`. The `assertContainsDriveLetter` helper then finds `C:` in the encoded path and reports a pass β€” but it’s testing the wrong code path entirely.
   
   Two options:
   1. Add `@EnabledOnOs(OS.WINDOWS)` to `windowsUppercaseDriveLetterPreserved`, `windowsLowercaseDriveLetterPreserved`, and `windowsDriveLetterForwardSlash`.
   2. Assert the specific output rather than using `assertContainsDriveLetter`, so the assertion fails fast on Linux when the wrong branch is taken.
   
   The `fileUrlWithWindowsDriveLetter()` and `fileUrlTripleSlashWithWindowsDriveLetter()` tests use `file:` URLs (not bare Windows paths) and are correct to run on all platforms.



-- 
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] Fix Windows path with drive letter lost in RepositoryUriUtils.basedir() [maven-resolver]

Posted by "gnodet-bot (via GitHub)" <gi...@apache.org>.
gnodet-bot commented on code in PR #2224:
URL: https://github.com/apache/maven-resolver/pull/2224#discussion_r4348519929


##########
maven-resolver-api/src/main/java/org/eclipse/aether/repository/RepositoryUriUtils.java:
##########
@@ -43,7 +43,9 @@ public static URI toUri(String repositoryUrl) {
                 || protocol.isEmpty()
                 || protocol.length() == 1
                         && (Character.isLetter(protocol.charAt(0)) && Character.isUpperCase(protocol.charAt(0)))) {
-            return new File(basedir(repositoryUrl)).toURI();
+            // Use Path.of() to parse the local path lexically, without resolving against the JVM
+            // working directory via File (which may be on a different drive on Windows).

Review Comment:
   πŸ”§ **Nit:** The comment says "Use `Path.of()`" but the code below uses `Paths.get()`. These are functionally identical, but the comment should match what the code actually calls β€” `Paths.get()` is also the right choice since `Path.of()` requires Java 11+ and this module targets Java 8.
   
   ```suggestion
               // Use Paths.get() to parse the local path lexically, without resolving against the JVM
               // working directory via File (which may be on a different drive on Windows).
   ```



-- 
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] Fix Windows path with drive letter lost in RepositoryUriUtils.basedir() [maven-resolver]

Posted by "gnodet-bot (via GitHub)" <gi...@apache.org>.
gnodet-bot commented on code in PR #2224:
URL: https://github.com/apache/maven-resolver/pull/2224#discussion_r4356905894


##########
maven-resolver-api/src/main/java/org/eclipse/aether/repository/RepositoryUriUtils.java:
##########
@@ -113,6 +130,15 @@ private static String basedir(String url) {
         return retValue.trim();
     }
 
+    /**
+     * Returns {@code true} if the given protocol token is a single ASCII letter, which on Windows indicates
+     * a drive letter (e.g. {@code C} in {@code C:\path} or {@code c} in {@code c:/path}) rather than a URL scheme.
+     * The check is locale-independent.
+     */
+    private static boolean isWindowsDriveLetter(String protocol) {
+        return protocol.length() == 1 && Character.isLetter(protocol.charAt(0));

Review Comment:
   πŸ”§ **Nit β€” Javadoc/implementation mismatch**: The Javadoc says "single ASCII letter" and "locale-independent", but `Character.isLetter()` is Unicode-aware and returns `true` for non-ASCII letters (e.g. `'\u00F1'` Γ±). Use an explicit ASCII range check:
   
   ```suggestion
       private static boolean isWindowsDriveLetter(String protocol) {
           if (protocol.length() != 1) {
               return false;
           }
           char c = protocol.charAt(0);
           return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
       }
   ```



##########
maven-resolver-api/src/main/java/org/eclipse/aether/repository/RepositoryUriUtils.java:
##########
@@ -39,11 +39,22 @@ private RepositoryUriUtils() {}
      */
     public static URI toUri(String repositoryUrl) {
         final String protocol = protocol(repositoryUrl);
-        if ("file".equals(protocol)
-                || protocol.isEmpty()
-                || protocol.length() == 1
-                        && (Character.isLetter(protocol.charAt(0)) && Character.isUpperCase(protocol.charAt(0)))) {
-            return new File(basedir(repositoryUrl)).toURI();
+        if ("file".equals(protocol)) {
+            // Parse as a proper file: URI so that percent-encoding and Windows paths like
+            // file:/C:/... or file:///C:/... are handled correctly by the JDK.
+            // Normalize single-slash file URIs (file:/D:/...) to triple-slash (file:///D:/...)
+            // so that Paths.get(URI) works on Windows: the JDK strips the leading '/' when
+            // the authority component is empty (""), giving "D:/..." which Windows accepts.
+            String normalized = repositoryUrl;
+            if (repositoryUrl.startsWith("file:/") && !repositoryUrl.startsWith("file://")) {
+                // file:/X:/... β†’ file:///X:/... (single-slash non-standard form)
+                normalized = "file://" + repositoryUrl.substring("file:".length());
+            }
+            return Paths.get(URI.create(normalized)).toUri();

Review Comment:
   ⚠️ **Regression**: `URI.create(normalized)` throws `IllegalArgumentException` for URLs with unencoded spaces or opaque `file:` URLs, breaking existing `FileTransporterTest` cases:
   - `file:repo space` β†’ `Illegal character in opaque part` (`testInit_OpaqueUrlSpacesDecoded`)
   - `file:/repo space` β†’ normalized to `file:///repo space` β†’ `Illegal character in path` (`testInit_HierarchicalUrlSpacesDecoded`)
   - `file:repo%20space` β†’ `URI is not hierarchical` β€” `Paths.get(URI)` requires a hierarchical URI; opaque `file:` URIs fail (`testInit_OpaqueUrlSpaces`)
   
   The old `new File(basedir(url)).toURI()` tolerated all of these because `File(String)` doesn't treat the string as a URI. Consider handling opaque `file:` URLs (no leading `/` after `file:`) by stripping the scheme and using `Paths.get(String)`, and separately percent-encoding spaces before `URI.create()` for the hierarchical case.



-- 
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] Fix Windows path with drive letter lost in RepositoryUriUtils.basedir() [maven-resolver]

Posted by "Copilot (via GitHub) via issues" <is...@maven.apache.org>.
Copilot commented on code in PR #2224:
URL: https://github.com/apache/maven-resolver/pull/2224#discussion_r4459598407


##########
maven-resolver-api/src/test/java/org/eclipse/aether/repository/LocalRepositoryTest.java:
##########
@@ -49,4 +55,80 @@ void allSomePaths() {
     private void doIt(File file) {
         new LocalRepository(file.toPath().toUri().toASCIIString());
     }
+
+    /**
+     * Verifies that a plain Windows path string with an uppercase drive letter (e.g. {@code C:\Users\foo\repo})
+     * passed to {@link LocalRepository#LocalRepository(String)} preserves the drive letter and does not
+     * resolve against the JVM working directory. Previously, {@code RepositoryUriUtils.basedir()}
+     * treated the drive letter as a URL scheme and stripped it, producing a drive-relative path
+     * ({@code \Users\foo\repo}) that {@code new File(...).toURI()} then resolved against the JVM
+     * working directory β€” which can be on a different drive on Windows CI runners.
+     */
+    @Test
+    @EnabledOnOs(OS.WINDOWS)
+    void windowsPlainPathUppercaseDriveLetterPreserved() {
+        // Build the path from the system temp dir so it is always on a valid drive and avoids
+        // hardcoding a specific username or profile path (which may not exist on all runners).
+        Path tmpBase = Paths.get(System.getProperty("java.io.tmpdir"));
+        char driveLetter = Character.toUpperCase(tmpBase.getRoot().toString().charAt(0));

Review Comment:
   When the temporary directory and working directory use the same drive, this uppercase-drive test can pass even with the original drive-stripping bug. Select a drive different from the working-directory drive so the existing full-path assertion reliably detects the cross-drive regression. The target directory and drive do not need to exist.



-- 
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] Fix Windows path with drive letter lost in RepositoryUriUtils.basedir() [maven-resolver]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2224:
URL: https://github.com/apache/maven-resolver/pull/2224#discussion_r4349954932


##########
maven-resolver-api/src/test/java/org/eclipse/aether/repository/LocalRepositoryTest.java:
##########
@@ -49,4 +54,33 @@ void allSomePaths() {
     private void doIt(File file) {
         new LocalRepository(file.toPath().toUri().toASCIIString());
     }
+
+    /**
+     * Verifies that a plain Windows path string (e.g. {@code C:\Users\foo\repo}) passed to
+     * {@link LocalRepository#LocalRepository(String)} preserves the drive letter and does not
+     * resolve against the JVM working directory. Previously, {@code RepositoryUriUtils.basedir()}
+     * treated the drive letter as a URL scheme and stripped it, producing a drive-relative path
+     * ({@code \Users\foo\repo}) that {@code Path.of(...).toUri()} then resolved against the JVM
+     * working directory β€” which can be on a different drive on Windows CI runners.
+     */
+    @Test
+    @EnabledOnOs(OS.WINDOWS)
+    void windowsPlainPathPreservesDriveLetter() {

Review Comment:
   Given the fix special-cases drive-letter parsing, consider adding a second assertion/case in this Windows test for a lowercase drive letter input (e.g., `c:\\...`) and/or forward-slash form (`C:/...`). This helps prevent regressions where drive-letter detection only works for one casing or separator style.



##########
maven-resolver-api/src/test/java/org/eclipse/aether/repository/LocalRepositoryTest.java:
##########
@@ -49,4 +54,33 @@ void allSomePaths() {
     private void doIt(File file) {
         new LocalRepository(file.toPath().toUri().toASCIIString());
     }
+
+    /**
+     * Verifies that a plain Windows path string (e.g. {@code C:\Users\foo\repo}) passed to
+     * {@link LocalRepository#LocalRepository(String)} preserves the drive letter and does not
+     * resolve against the JVM working directory. Previously, {@code RepositoryUriUtils.basedir()}
+     * treated the drive letter as a URL scheme and stripped it, producing a drive-relative path
+     * ({@code \Users\foo\repo}) that {@code Path.of(...).toUri()} then resolved against the JVM
+     * working directory β€” which can be on a different drive on Windows CI runners.
+     */
+    @Test
+    @EnabledOnOs(OS.WINDOWS)
+    void windowsPlainPathPreservesDriveLetter() {
+        // Use the current drive letter so the path is always valid on any Windows machine.
+        String currentDrive = Paths.get(".").toAbsolutePath().getRoot().toString(); // e.g. "C:\"
+        char driveLetter = currentDrive.charAt(0);
+        String plainPath = driveLetter + ":\\Users\\runneradmin\\AppData\\Local\\Temp\\my-repo";
+
+        LocalRepository repo = new LocalRepository(plainPath);
+        String repoPath = repo.getBasePath().toString();

Review Comment:
   The test hard-codes a user/profile-specific subpath (`\\Users\\runneradmin\\...`) and later asserts full string equality. Even if the repository doesn’t need to exist, string round-tripping can differ due to normalization (separator normalization, case normalization, etc.), which risks flakiness across Windows environments. Prefer building the path from an OS-provided base (e.g., `java.io.tmpdir` / `Paths.get(System.getProperty(\"java.io.tmpdir\"))`) and asserting using `Path` equality/normalization (e.g., compare `Paths.get(plainPath)` to `repo.getBasePath()`), while still separately asserting the drive letter is preserved.



##########
maven-resolver-api/src/main/java/org/eclipse/aether/repository/RepositoryUriUtils.java:
##########
@@ -76,7 +78,15 @@ private static String basedir(String url) {
         String retValue;
 
         if (!protocol.isEmpty()) {
-            retValue = url.substring(protocol.length() + 1);
+            // A single uppercase letter is a Windows drive letter (e.g. "C:\path"), not a URL scheme.
+            // Keep it in the path; do not strip it as if it were a protocol prefix.
+            if (protocol.length() == 1
+                    && Character.isLetter(protocol.charAt(0))
+                    && Character.isUpperCase(protocol.charAt(0))) {
+                retValue = url;
+            } else {
+                retValue = url.substring(protocol.length() + 1);
+            }
         } else {

Review Comment:
   Drive-letter detection is currently limited to *uppercase* single-letter prefixes. Windows paths can be provided as `c:\\...` as well; in that case this code will still treat `c` as a URL scheme and strip the drive letter (reintroducing the original bug). Consider detecting drive letters by pattern (e.g., `^[A-Za-z]:[\\\\/].*`) rather than `isUpperCase`, and apply the same predicate consistently in both `basedir()` and the `toUri()` protocol-branch condition.



##########
maven-resolver-api/src/test/java/org/eclipse/aether/repository/LocalRepositoryTest.java:
##########
@@ -49,4 +54,33 @@ void allSomePaths() {
     private void doIt(File file) {
         new LocalRepository(file.toPath().toUri().toASCIIString());
     }
+
+    /**
+     * Verifies that a plain Windows path string (e.g. {@code C:\Users\foo\repo}) passed to
+     * {@link LocalRepository#LocalRepository(String)} preserves the drive letter and does not
+     * resolve against the JVM working directory. Previously, {@code RepositoryUriUtils.basedir()}
+     * treated the drive letter as a URL scheme and stripped it, producing a drive-relative path
+     * ({@code \Users\foo\repo}) that {@code Path.of(...).toUri()} then resolved against the JVM
+     * working directory β€” which can be on a different drive on Windows CI runners.
+     */
+    @Test
+    @EnabledOnOs(OS.WINDOWS)
+    void windowsPlainPathPreservesDriveLetter() {
+        // Use the current drive letter so the path is always valid on any Windows machine.
+        String currentDrive = Paths.get(".").toAbsolutePath().getRoot().toString(); // e.g. "C:\"
+        char driveLetter = currentDrive.charAt(0);
+        String plainPath = driveLetter + ":\\Users\\runneradmin\\AppData\\Local\\Temp\\my-repo";
+
+        LocalRepository repo = new LocalRepository(plainPath);
+        String repoPath = repo.getBasePath().toString();
+
+        // The resolved path must start with the same drive letter, not a different one.
+        assertEquals(
+                String.valueOf(driveLetter),
+                repoPath.substring(0, 1).toUpperCase(),
+                "Drive letter must be preserved when parsing a plain Windows path: " + repoPath);

Review Comment:
   `String#toUpperCase()` is locale-sensitive; in uncommon locales this can produce unexpected results for certain letters. Since this is a single character comparison, prefer a locale-independent approach (e.g., `Character.toUpperCase(...)`) or use `toUpperCase(Locale.ROOT)` to keep the assertion deterministic.



-- 
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] Fix Windows path with drive letter lost in RepositoryUriUtils.basedir() [maven-resolver]

Posted by "cstamas (via GitHub) via issues" <is...@maven.apache.org>.
cstamas merged PR #2224:
URL: https://github.com/apache/maven-resolver/pull/2224


-- 
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