Copilot commented on code in PR #282:
URL: 
https://github.com/apache/maven-artifact-plugin/pull/282#discussion_r4480226361


##########
src/main/java/org/apache/maven/plugins/artifact/buildinfo/AbstractBuildinfoMojo.java:
##########
@@ -451,8 +451,15 @@ private Toolchain getToolchain() {
 
     protected String relative(File file) {
         File basedir = session.getTopLevelProject().getBasedir();
-        int length = basedir.getPath().length();
-        String path = file.getPath();
-        return path.substring(length + 1);
+        return relative(basedir, file);
+    }
+
+    /**
+     * Computes the path of {@code file} relative to {@code basedir}, 
correctly handling a {@code file} that
+     * is not actually below {@code basedir} (for example a build artifact 
copied outside the reactor), instead
+     * of assuming {@code basedir}'s path is always a literal prefix of {@code 
file}'s path.
+     */
+    static String relative(File basedir, File file) {
+        return basedir.toPath().relativize(file.toPath()).toString();

Review Comment:
   `Path.relativize()` throws `IllegalArgumentException` when `basedir` and 
`file` have different roots (e.g., different Windows drives) or one path is 
absolute while the other is relative. Given the method’s Javadoc explicitly 
mentions artifacts potentially being outside the reactor, this can still occur 
and would crash message rendering. Consider catching `IllegalArgumentException` 
and falling back to a safe representation (e.g., `file.toPath().toString()` or 
an absolute/normalized form), and/or normalizing/absolutizing both paths before 
relativizing. If you choose not to handle it, the Javadoc should explicitly 
document the exception behavior.



##########
src/main/java/org/apache/maven/plugins/artifact/buildinfo/AbstractBuildinfoMojo.java:
##########
@@ -451,8 +451,15 @@ private Toolchain getToolchain() {
 
     protected String relative(File file) {
         File basedir = session.getTopLevelProject().getBasedir();
-        int length = basedir.getPath().length();
-        String path = file.getPath();
-        return path.substring(length + 1);
+        return relative(basedir, file);
+    }
+
+    /**
+     * Computes the path of {@code file} relative to {@code basedir}, 
correctly handling a {@code file} that
+     * is not actually below {@code basedir} (for example a build artifact 
copied outside the reactor), instead
+     * of assuming {@code basedir}'s path is always a literal prefix of {@code 
file}'s path.
+     */
+    static String relative(File basedir, File file) {
+        return basedir.toPath().relativize(file.toPath()).toString();

Review Comment:
   The new implementation introduces a new failure mode 
(`IllegalArgumentException`) for different-root and absolute-vs-relative 
inputs. If you add the recommended fallback/handling, please add unit coverage 
for that branch (e.g., absolute vs relative mismatch, and/or different root 
guarded by an OS conditional) to prevent regressions.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to