Skip to content

Commit

Permalink
Do not add Kotlin and Groovy sources from build/ to LST (#322)
Browse files Browse the repository at this point in the history
* Exclude build-generated-sources too

* Consistently skip source paths that start with BuildDirectory
  • Loading branch information
timtebeek authored Aug 18, 2024
1 parent fda6783 commit d9c519d
Showing 1 changed file with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ static Path repositoryRoot(Project project) {
}

private static final Map<Path, GitProvenance> 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
Expand Down Expand Up @@ -660,13 +661,14 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> 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<SourceFile> sourceSetSourceFiles = Stream.of();
int sourceSetSize = 0;
Expand Down Expand Up @@ -737,7 +739,7 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> 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;
Expand All @@ -752,8 +754,8 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
if (subproject.getPlugins().hasPlugin("org.jetbrains.kotlin.jvm")) {
String excludedProtosPath = subproject.getProjectDir().getPath() + "/protos/build/generated";
List<Path> kotlinPaths = unparsedSources.stream()
.filter(it -> !it.toString().startsWith(excludedProtosPath))
.filter(it -> it.toString().endsWith(".kt"))
.filter(it -> !it.toString().startsWith(excludedProtosPath))
.collect(toList());

if (!kotlinPaths.isEmpty()) {
Expand All @@ -768,7 +770,8 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> 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;
Expand Down Expand Up @@ -805,7 +808,8 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> 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;
Expand Down Expand Up @@ -1073,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)
Expand Down Expand Up @@ -1131,7 +1136,8 @@ private SourceFileStream parseMultiplatformKotlinProject(Project subproject, Col
Stream<SourceFile> 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;
Expand All @@ -1151,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");
}
Expand Down

0 comments on commit d9c519d

Please sign in to comment.