Skip to content

Commit

Permalink
Handle partially-overlapping source sets better. Parse each directory…
Browse files Browse the repository at this point in the history
… exactly once.
  • Loading branch information
sambsnyd committed Sep 29, 2023
1 parent e34bcf0 commit 8c1986c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private static void configureProject(Project project, RewriteExtension extension
return 2;
}
})).forEach(sourceSet -> {
for (File file : sourceSet.getAllSource().getSourceDirectories().getFiles()) {
for (File file : sourceSet.getAllJava().getSourceDirectories().getFiles()) {
if (!sourceDirs.add(file.getAbsolutePath())) {
Task compileTask = project.getTasks().getByPath(sourceSet.getCompileJavaTaskName());
compileTask.setEnabled(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Collections.*;
Expand Down Expand Up @@ -653,15 +654,7 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
}

Set<String> sourceDirs = new HashSet<>();
sourceSetLoop:
for (SourceSet sourceSet : sourceSets) {
// Detect SourceSets which overlap other sourceSets and disable the compilation task of the overlapping
// source set. Skip parsing directories already covered by another source set.
for (File file : sourceSet.getAllSource().getSourceDirectories().getFiles()) {
if (!sourceDirs.add(file.getAbsolutePath())) {
continue sourceSetLoop;
}
}
Stream<SourceFile> sourceSetSourceFiles = Stream.of();
int sourceSetSize = 0;

Expand All @@ -672,11 +665,24 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
javaCompileTask.getSourceCompatibility(),
javaCompileTask.getTargetCompatibility());

List<Path> javaPaths = sourceSet.getAllJava().getFiles().stream()
.filter(it -> it.isFile() && it.getName().endsWith(".java"))
.map(File::toPath)
List<Path> unparsedSources = sourceSet.getAllSource()
.getSourceDirectories()
.filter(it -> it.exists() && !alreadyParsed.contains(baseDir.relativize(it.toPath())))
.getFiles()
.stream()
.flatMap(sourceDir -> {
try {
return Files.walk(sourceDir.toPath());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
})
.filter(Files::isRegularFile)
.map(Path::toAbsolutePath)
.map(Path::normalize)
.collect(Collectors.toList());
List<Path> javaPaths = unparsedSources.stream()
.filter(it -> it.toString().endsWith(".java"))
.collect(toList());

// classpath doesn't include the transitive dependencies of the implementation configuration
Expand Down Expand Up @@ -731,11 +737,8 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
}

if (subproject.getPlugins().hasPlugin("org.jetbrains.kotlin.jvm")) {
List<Path> kotlinPaths = sourceSet.getAllSource().getFiles().stream()
.filter(it -> it.isFile() && it.getName().endsWith(".kt"))
.map(File::toPath)
.map(Path::toAbsolutePath)
.map(Path::normalize)
List<Path> kotlinPaths = unparsedSources.stream()
.filter(it -> it.toString().endsWith(".kt"))
.collect(toList());

if (!kotlinPaths.isEmpty()) {
Expand All @@ -762,13 +765,9 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
logger.info("Scanned {} Kotlin sources in {}/{}", kotlinPaths.size(), subproject.getPath(), sourceSet.getName());
}
}

if (subproject.getPlugins().hasPlugin(GroovyPlugin.class)) {
List<Path> groovyPaths = sourceSet.getAllSource().getFiles().stream()
.filter(it -> it.isFile() && it.getName().endsWith(".groovy"))
.map(File::toPath)
.map(Path::toAbsolutePath)
.map(Path::normalize)
List<Path> groovyPaths = unparsedSources.stream()
.filter(it -> it.toString().endsWith(".groovy"))
.collect(toList());

if (!groovyPaths.isEmpty()) {
Expand Down Expand Up @@ -805,7 +804,7 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
}

for (File resourcesDir : sourceSet.getResources().getSourceDirectories()) {
if (resourcesDir.exists()) {
if (resourcesDir.exists() && !alreadyParsed.contains(baseDir.relativize(resourcesDir.toPath()))) {
OmniParser omniParser = omniParser(alreadyParsed);
List<Path> accepted = omniParser.acceptedPaths(baseDir, resourcesDir.toPath());
sourceSetSourceFiles = Stream.concat(
Expand All @@ -819,6 +818,11 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe

JavaSourceSet sourceSetProvenance = JavaSourceSet.build(sourceSet.getName(), dependencyPaths, javaTypeCache, false);
sourceFileStream = sourceFileStream.concat(sourceSetSourceFiles.map(addProvenance(sourceSetProvenance)), sourceSetSize);
// Some source sets get misconfigured to have the same directories as other source sets
// This causes duplicate source files to be parsed, so once a source set has been parsed exclude it from future parsing
for (File file : sourceSet.getAllSource().getSourceDirectories().getFiles()) {
alreadyParsed.add(baseDir.relativize(file.toPath()));
}
}
SourceFileStream gradleFiles = parseGradleFiles(exclusions, alreadyParsed, ctx);
sourceFileStream = sourceFileStream.concat(gradleFiles, gradleFiles.size());
Expand Down
Loading

0 comments on commit 8c1986c

Please sign in to comment.