Fix IllegalArgumentException in Input.getRelativePath for relative paths#6843
Conversation
When a Parser$Input has a relative path (e.g. synthetic dependsOn stubs created by KotlinParser.determinePath()), calling getRelativePath() with an absolute relativeTo path causes Path.relativize() to throw IllegalArgumentException on JDK 21+ because one path is absolute and the other is relative. Return relative paths as-is since they don't need relativization. Fixes openrewrite/rewrite-gradle-plugin#424
|
It seems like the rewrite/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java Lines 369 to 390 in ae70bc3 |
Drop the "dependsOn-" filename prefix and instead track dependsOn input paths in a Set for filtering, matching how JavaParser handles dependsOn snippets.
IllegalArgumentException in Input.getRelativePath for relative paths
| public Builder dependsOn(@Language("kotlin") String... inputsAsStrings) { | ||
| this.dependsOn = Arrays.stream(inputsAsStrings) | ||
| .map(input -> Input.fromString(determinePath("dependsOn-", input), input)) | ||
| .map(input -> Input.fromString(determinePath("", input), input)) |
There was a problem hiding this comment.
I think normally the absolute path just gets pretended here?
There was a problem hiding this comment.
I went by what I'd seen in the JavaParser:
There was a problem hiding this comment.
That is doing a Paths.get("") which isn't represented here. That's the same thing that I was meaning.
There was a problem hiding this comment.
Hmm; with how that Paths.get("") is being used I couldn't mentally compute any difference, but I'll replicate it here.
rewrite/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java
Lines 369 to 389 in ae70bc3

What's Changed
When a
Parser$Inputhas a relative path (e.g. syntheticdependsOnstubs created byKotlinParser.determinePath()viaPaths.get(pkg, prefix + className + ".kt")), callinggetRelativePath(relativeTo)with an absoluterelativeTopath causesPath.relativize()to throwIllegalArgumentException: 'other' is different type of Pathon JDK 21+.This happens because JDK 21+ strictly enforces that both paths must be of the same type (both absolute or both relative) in
Path.relativize().The fix returns relative paths as-is since they're already relative and don't need relativization.