Skip to content

Commit

Permalink
Fix #231 non-source code files in subprojects parsed multiple times w…
Browse files Browse the repository at this point in the history
…ith incorrect paths
  • Loading branch information
sambsnyd committed Sep 23, 2023
1 parent ce73e20 commit 2553b54
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private static StringBuilder repeat(int repeat) {

public Collection<Path> listSources() {
// Use a sorted collection so that gradle input detection isn't thrown off by ordering
Set<Path> result = new TreeSet<>(omniParser(emptySet()).acceptedPaths(project.getProjectDir().toPath()));
Set<Path> result = new TreeSet<>(omniParser(emptySet()).build().acceptedPaths(baseDir, project.getProjectDir().toPath()));
//noinspection deprecation
JavaPluginConvention javaConvention = project.getConvention().findPlugin(JavaPluginConvention.class);
if (javaConvention != null) {
Expand All @@ -292,6 +292,7 @@ public void dryRun(Path reportPath, boolean dumpGcActivity, Consumer<Throwable>
heapMetrics.bindTo(meterRegistry);
new JvmMemoryMetrics().bindTo(meterRegistry);

//noinspection deprecation
File rewriteBuildDir = new File(project.getBuildDir(), "/rewrite");
if (rewriteBuildDir.exists() || rewriteBuildDir.mkdirs()) {
File rewriteGcLog = new File(rewriteBuildDir, "rewrite-gc.csv");
Expand Down Expand Up @@ -715,6 +716,7 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
.map(Supplier::get)
.flatMap(jp -> jp.parse(javaPaths, baseDir, ctx))
.map(cu -> {
//noinspection deprecation
if (isExcluded(exclusions, cu.getSourcePath()) ||
cu.getSourcePath().startsWith(baseDir.relativize(subproject.getBuildDir().toPath()))) {
return null;
Expand Down Expand Up @@ -804,12 +806,13 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe

for (File resourcesDir : sourceSet.getResources().getSourceDirectories()) {
if (resourcesDir.exists()) {
OmniParser omniParser = omniParser(alreadyParsed);
List<Path> accepted = omniParser.acceptedPaths(resourcesDir.toPath());
OmniParser omniParser = omniParser(alreadyParsed).build();
List<Path> accepted = omniParser.acceptedPaths(baseDir, resourcesDir.toPath());
sourceSetSourceFiles = Stream.concat(
sourceSetSourceFiles,
omniParser.parse(accepted, resourcesDir.toPath(), new InMemoryExecutionContext())
omniParser.parse(accepted, baseDir, new InMemoryExecutionContext())
);
alreadyParsed.addAll(accepted);
sourceSetSize += accepted.size();
}
}
Expand Down Expand Up @@ -936,13 +939,14 @@ private SourceFileStream parseGradleFiles(

protected SourceFileStream parseNonProjectResources(Project subproject, Set<Path> alreadyParsed, ExecutionContext ctx, List<Marker> projectProvenance, Stream<SourceFile> sourceFiles) {
//Collect any additional yaml/properties/xml files that are NOT already in a source set.
OmniParser omniParser = omniParser(alreadyParsed);
List<Path> accepted = omniParser.acceptedPaths(subproject.getProjectDir().toPath());
return SourceFileStream.build("", s -> {
}).concat(omniParser.parse(accepted, subproject.getProjectDir().toPath(), ctx), accepted.size());
OmniParser omniParser = omniParser(alreadyParsed)
.build();
List<Path> accepted = omniParser.acceptedPaths(baseDir, subproject.getProjectDir().toPath());
return SourceFileStream.build("", s -> {})
.concat(omniParser.parse(accepted, baseDir, ctx), accepted.size());
}

private OmniParser omniParser(Set<Path> alreadyParsed) {
private OmniParser.Builder omniParser(Set<Path> alreadyParsed) {
return OmniParser.builder(
OmniParser.RESOURCE_PARSERS,
PlainTextParser.builder()
Expand All @@ -952,8 +956,7 @@ private OmniParser omniParser(Set<Path> alreadyParsed) {
)
.exclusionMatchers(pathMatchers(baseDir, mergeExclusions(project, baseDir, extension)))
.exclusions(alreadyParsed)
.sizeThresholdMb(extension.getSizeThresholdMb())
.build();
.sizeThresholdMb(extension.getSizeThresholdMb());
}

private static Collection<String> mergeExclusions(Project project, Path baseDir, RewriteExtension extension) {
Expand Down
21 changes: 17 additions & 4 deletions plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ class RewriteRunTest : RewritePluginTest {
}

@Test
fun `rewriteRun applies built-in AutoFormat to a multi-project build`(
fun `rewriteRun applies recipe to a multi-project build`(
@TempDir projectDir: File
) {
val bTestClassExpected = """
package com.foo;
import org.junit.Test;
public class BTestClass {
Expand All @@ -171,14 +171,23 @@ class RewriteRunTest : RewritePluginTest {
}
""".trimIndent()
gradleProject(projectDir) {
rewriteYaml("""
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.FormatAndAddProperty
recipeList:
- org.openrewrite.java.format.AutoFormat
- org.openrewrite.properties.ChangePropertyKey:
oldPropertyKey: foo
newPropertyKey: bar
""")
buildGradle("""
plugins {
id("org.openrewrite.rewrite")
id("java")
}
rewrite {
activeRecipe("org.openrewrite.java.format.AutoFormat")
activeRecipe("org.openrewrite.FormatAndAddProperty")
exclusion("**/BTestClass.java")
}
Expand Down Expand Up @@ -216,6 +225,7 @@ class RewriteRunTest : RewritePluginTest {
public void passes() { }
}
""")
propertiesFile("test.properties", "foo=baz\n")
}
}
subproject("b") {
Expand All @@ -231,7 +241,7 @@ class RewriteRunTest : RewritePluginTest {
//language=java
val aTestClassExpected = """
package com.foo;
import org.junit.Test;
public class ATestClass {
Expand All @@ -245,6 +255,9 @@ class RewriteRunTest : RewritePluginTest {
assertThat(aTestClassFile.readText()).isEqualTo(aTestClassExpected)
val bTestClassFile = File(projectDir, "b/src/test/java/com/foo/BTestClass.java")
assertThat(bTestClassFile.readText()).isEqualTo(bTestClassExpected)

val propertiesFile = File(projectDir, "a/src/test/resources/test.properties")
assertThat(propertiesFile.readText()).isEqualTo("bar=baz\n")
}

@Suppress("ClassInitializerMayBeStatic", "StatementWithEmptyBody", "ConstantConditions")
Expand Down

0 comments on commit 2553b54

Please sign in to comment.