From 759177068bff3a9d567795def45f5a2f9b5c89e3 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 15 Aug 2024 11:22:43 +0200 Subject: [PATCH 1/2] Exclude build-generated-sources too --- .../org/openrewrite/gradle/isolated/DefaultProjectParser.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java b/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java index b48de69eb..59dff0521 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java +++ b/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java @@ -750,10 +750,12 @@ public Stream parse(Project subproject, Set alreadyParsed, Exe } if (subproject.getPlugins().hasPlugin("org.jetbrains.kotlin.jvm")) { + String excludedBuildSrcPath = subproject.getProjectDir().getPath() + "/build/generated-sources"; String excludedProtosPath = subproject.getProjectDir().getPath() + "/protos/build/generated"; List kotlinPaths = unparsedSources.stream() - .filter(it -> !it.toString().startsWith(excludedProtosPath)) .filter(it -> it.toString().endsWith(".kt")) + .filter(it -> !it.toString().startsWith(excludedBuildSrcPath)) + .filter(it -> !it.toString().startsWith(excludedProtosPath)) .collect(toList()); if (!kotlinPaths.isEmpty()) { From be668d2a0be8ad7f7058dd6685d25c84c6f6902f Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 17 Aug 2024 23:47:10 +0200 Subject: [PATCH 2/2] Consistently skip source paths that start with BuildDirectory --- .../gradle/isolated/DefaultProjectParser.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java b/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java index 59dff0521..73c04884d 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java +++ b/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java @@ -147,6 +147,7 @@ static Path repositoryRoot(Project project) { } private static final Map REPO_ROOT_TO_PROVENANCE = new HashMap<>(); + private @Nullable GitProvenance gitProvenance(Path baseDir, @Nullable BuildEnvironment buildEnvironment) { try { // Computing git provenance can be expensive for repositories with many commits, ensure we do it only once per build @@ -660,13 +661,14 @@ public Stream parse(Project subproject, Set alreadyParsed, Exe } if (subproject.getPlugins().hasPlugin("org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension") || - subproject.getExtensions().findByName("kotlin") != null && subproject.getExtensions().getByName("kotlin").getClass() - .getCanonicalName().startsWith("org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension")) { + subproject.getExtensions().findByName("kotlin") != null && subproject.getExtensions().getByName("kotlin").getClass() + .getCanonicalName().startsWith("org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension")) { sourceFileStream = sourceFileStream.concat(parseMultiplatformKotlinProject(subproject, exclusions, alreadyParsed, ctx)); } Charset sourceCharset = Charset.forName(System.getProperty("file.encoding", "UTF-8")); + Path buildDirPath = baseDir.relativize(subproject.getLayout().getBuildDirectory().get().getAsFile().toPath()); for (SourceSet sourceSet : sourceSets) { Stream sourceSetSourceFiles = Stream.of(); int sourceSetSize = 0; @@ -737,7 +739,7 @@ public Stream parse(Project subproject, Set alreadyParsed, Exe .flatMap(jp -> jp.parse(javaPaths, baseDir, ctx)) .map(cu -> { if (isExcluded(exclusions, cu.getSourcePath()) || - cu.getSourcePath().startsWith(baseDir.relativize(subproject.getLayout().getBuildDirectory().get().getAsFile().toPath()))) { + cu.getSourcePath().startsWith(buildDirPath)) { return null; } return cu; @@ -750,11 +752,9 @@ public Stream parse(Project subproject, Set alreadyParsed, Exe } if (subproject.getPlugins().hasPlugin("org.jetbrains.kotlin.jvm")) { - String excludedBuildSrcPath = subproject.getProjectDir().getPath() + "/build/generated-sources"; String excludedProtosPath = subproject.getProjectDir().getPath() + "/protos/build/generated"; List kotlinPaths = unparsedSources.stream() .filter(it -> it.toString().endsWith(".kt")) - .filter(it -> !it.toString().startsWith(excludedBuildSrcPath)) .filter(it -> !it.toString().startsWith(excludedProtosPath)) .collect(toList()); @@ -770,7 +770,8 @@ public Stream parse(Project subproject, Set alreadyParsed, Exe .map(Supplier::get) .flatMap(kp -> kp.parse(kotlinPaths, baseDir, ctx)) .map(cu -> { - if (isExcluded(exclusions, cu.getSourcePath())) { + if (isExcluded(exclusions, cu.getSourcePath()) || + cu.getSourcePath().startsWith(buildDirPath)) { return null; } return cu; @@ -807,7 +808,8 @@ public Stream parse(Project subproject, Set alreadyParsed, Exe .map(Supplier::get) .flatMap(gp -> gp.parse(groovyPaths, baseDir, ctx)) .map(cu -> { - if (isExcluded(exclusions, cu.getSourcePath())) { + if (isExcluded(exclusions, cu.getSourcePath()) || + cu.getSourcePath().startsWith(buildDirPath)) { return null; } return cu; @@ -1075,6 +1077,7 @@ private SourceFileStream parseMultiplatformKotlinProject(Project subproject, Col return sourceFileStream; } + Path buildDirPath = baseDir.relativize(subproject.getLayout().getBuildDirectory().get().getAsFile().toPath()); for (String sourceSetName : sourceSetNames) { try { Object sourceSet = sourceSets.getClass().getMethod("getByName", String.class) @@ -1133,7 +1136,8 @@ private SourceFileStream parseMultiplatformKotlinProject(Project subproject, Col Stream cus = kp.parse(kotlinPaths, baseDir, ctx); alreadyParsed.addAll(kotlinPaths); cus = cus.map(cu -> { - if (isExcluded(exclusions, cu.getSourcePath())) { + if (isExcluded(exclusions, cu.getSourcePath()) || + cu.getSourcePath().startsWith(buildDirPath)) { return null; } return cu; @@ -1153,7 +1157,7 @@ private SourceFileStream parseMultiplatformKotlinProject(Project subproject, Col } private SourceFile logParseErrors(SourceFile source) { - source.getMarkers().findFirst(ParseExceptionResult.class).ifPresent(e -> { + source.getMarkers().findFirst(ParseExceptionResult.class).ifPresent(e -> { if (firstWarningLogged.compareAndSet(false, true)) { logger.warn("There were problems parsing some source files, run with --info to see full stack traces"); }