Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion rewrite-core/src/main/java/org/openrewrite/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ public static List<Input> fromResource(String resource, String delimiter, @Nulla
}

public Path getRelativePath(@Nullable Path relativeTo) {
return relativeTo == null ? path : relativeTo.relativize(path);
if (relativeTo == null || path.isAbsolute() != relativeTo.isAbsolute()) {
return path;
}
return relativeTo.relativize(path);
}

public EncodingDetectingInputStream getSource(ExecutionContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,28 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path re
ParsingExecutionContextView pctx = ParsingExecutionContextView.view(ctx);
ParsingEventListener parsingListener = pctx.getParsingListener();

Set<Path> dependsOnPaths = dependsOn == null ? emptySet() :
dependsOn.stream().map(Input::getPath).collect(toSet());
// dependsOn inputs have relative paths (from determinePath), but source inputs
// may have absolute paths. Resolve dependsOn paths to match so that
// Path.relativize in getRelativePath doesn't throw IllegalArgumentException.
List<Input> resolvedDependsOn = dependsOn;
if (dependsOn != null && relativeTo != null && relativeTo.isAbsolute()) {
resolvedDependsOn = new ArrayList<>(dependsOn.size());
for (Input dep : dependsOn) {
if (!dep.getPath().isAbsolute()) {
resolvedDependsOn.add(Input.fromString(relativeTo.resolve(dep.getPath()), dep.getSource(pctx).readFully()));
} else {
resolvedDependsOn.add(dep);
}
}
}

Set<Path> dependsOnPaths = resolvedDependsOn == null ? emptySet() :
resolvedDependsOn.stream().map(i -> i.getRelativePath(relativeTo)).collect(toSet());

// TODO: FIR and disposable may not be necessary using the IR.
Disposable disposable = Disposer.newDisposable();
CompiledSource compilerCus;
List<Input> acceptedInputs = ListUtils.concatAll(dependsOn, acceptedInputs(sources).collect(toList()));
List<Input> acceptedInputs = ListUtils.concatAll(resolvedDependsOn, acceptedInputs(sources).collect(toList()));
try {
compilerCus = parse(acceptedInputs, disposable, pctx);
} catch (Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
package org.openrewrite.kotlin;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.openrewrite.test.RewriteTest;

import java.nio.file.Path;

import static org.openrewrite.kotlin.Assertions.kotlin;

class KotlinParserTest implements RewriteTest {
Expand All @@ -40,4 +43,24 @@ class MyClass
);
}

@Test
void dependsOnWithAbsoluteRelativeTo(@TempDir Path tempDir) {
rewriteRun(
spec -> spec
.relativeTo(tempDir)
.parser(KotlinParser.builder().dependsOn("""
package foo.bar

class MyClass
""")),
kotlin(
"""
import foo.bar.MyClass

val myClass: MyClass? = null
"""
)
);
}

}