Skip to content

Commit a26d9df

Browse files
committed
Prevent cross-scope source files from being parsed as PlainText
When processMainSources or processTestSources runs independently (e.g. scoped listSourceFiles), resource parsing can walk the entire project tree if a resource directory points at the project root. Source files belonging to the other scope are not yet in alreadyParsed, so they get claimed by PlainTextParser. The real parse in the other scope then produces a duplicate entry, and the PlainText version shadows the properly typed one — making annotations invisible to recipes like FindAnnotations. Fix: pre-populate alreadyParsed with the opposite scope's source file paths before resource parsing begins. This mirrors what the non-scoped listSourceFiles gets for free by processing both scopes with a shared alreadyParsed set.
1 parent b7b08c4 commit a26d9df

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ private Stream<SourceFile> processMainSources(
395395
List<Path> mainKotlinSources = (kotlinSourceDir != null) ? listKotlinSources(mavenProject.getBasedir().toPath().resolve(kotlinSourceDir)) : Collections.emptyList();
396396
alreadyParsed.addAll(mainKotlinSources);
397397

398+
// Prevent resource parsers from claiming test sources as PlainText
399+
// when a resource directory encompasses the project root
400+
collectSourceFilePaths(mavenProject.getTestCompileSourceRoots(), alreadyParsed);
398401
logInfo(mavenProject, "Parsing source files");
399402
List<Path> dependencies = mavenProject.getCompileClasspathElements().stream()
400403
.distinct()
@@ -461,6 +464,10 @@ private Stream<SourceFile> processTestSources(
461464
List<Path> testKotlinSources = (kotlinSourceDir != null) ? listKotlinSources(mavenProject.getBasedir().toPath().resolve(kotlinSourceDir)) : Collections.emptyList();
462465
alreadyParsed.addAll(testKotlinSources);
463466

467+
// Prevent resource parsers from claiming main sources as PlainText
468+
// when a resource directory encompasses the project root
469+
collectSourceFilePaths(mavenProject.getCompileSourceRoots(), alreadyParsed);
470+
464471
Stream<SourceFile> parsedJava = Stream.empty();
465472
if (!testJavaSources.isEmpty()) {
466473
parsedJava = javaParserBuilder.build().parse(testJavaSources, baseDir, ctx);
@@ -748,6 +755,21 @@ private <T extends SourceFile> UnaryOperator<T> addProvenance(Path baseDir, List
748755
};
749756
}
750757

758+
/**
759+
* Walk the given source roots and add all Java and Kotlin file paths to the provided set.
760+
* This prevents resource parsers from claiming source files belonging to another scope
761+
* as PlainText when a resource directory encompasses the entire project root.
762+
*/
763+
static void collectSourceFilePaths(List<String> sourceRoots, Set<Path> paths) throws MojoExecutionException {
764+
for (String root : sourceRoots) {
765+
Path rootPath = Paths.get(root);
766+
if (Files.isDirectory(rootPath)) {
767+
paths.addAll(listJavaSources(rootPath));
768+
paths.addAll(listKotlinSources(rootPath));
769+
}
770+
}
771+
}
772+
751773
private static List<Path> listJavaSources(Path sourceDirectory) throws MojoExecutionException {
752774
return listSources(sourceDirectory, ".java");
753775
}

0 commit comments

Comments
 (0)