Skip to content

Commit 26b2e00

Browse files
committed
Make KSP compatible with Gradle's isolated projects
WIP Bug: https://issuetracker.google.com/issues/361415637 Test: AndroidIT.kt
1 parent 553adb6 commit 26b2e00

File tree

7 files changed

+13
-41
lines changed

7 files changed

+13
-41
lines changed

gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspAATask.kt

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,21 +167,9 @@ abstract class KspAATask @Inject constructor(
167167
cfg.moduleName.value(moduleName)
168168
val kotlinOutputDir = KspGradleSubplugin.getKspKotlinOutputDir(project, sourceSetName, target)
169169
val javaOutputDir = KspGradleSubplugin.getKspJavaOutputDir(project, sourceSetName, target)
170-
val filteredTasks =
171-
kspExtension.excludedSources.buildDependencies.getDependencies(null).map { it.name }
172170
kotlinCompilation.allKotlinSourceSetsObservable.forAll { sourceSet ->
173-
val filtered = sourceSet.kotlin.srcDirs.filter {
174-
!kotlinOutputDir.isParentOf(it) && !javaOutputDir.isParentOf(it) &&
175-
it !in kspExtension.excludedSources
176-
}.map {
177-
// @SkipWhenEmpty doesn't work well with File.
178-
project.objects.fileTree().from(it)
179-
}
180-
cfg.sourceRoots.from(filtered)
181-
cfg.javaSourceRoots.from(filtered)
182-
kspAATask.dependsOn(
183-
sourceSet.kotlin.nonSelfDeps(kspTaskName).filter { it.name !in filteredTasks }
184-
)
171+
cfg.sourceRoots.from(sourceSet.kotlin.sourceDirectories)
172+
cfg.javaSourceRoots.from(sourceSet.kotlin.sourceDirectories)
185173
}
186174
if (kotlinCompilation is KotlinCommonCompilation) {
187175
cfg.commonSourceRoots.from(kotlinCompilation.defaultSourceSet.kotlin)

gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspExtension.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ abstract class KspExtension @Inject constructor(project: Project) {
4343

4444
// Specify sources that should be excluded from KSP.
4545
// If you have a task that generates sources, you can call `ksp.excludedSources.from(task)`.
46+
// TODO - Deprecate and then remove this DSL?
4647
abstract val excludedSources: ConfigurableFileCollection
4748

4849
open val arguments: Map<String, String> get() = apOptions.get()

gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -360,16 +360,8 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
360360
)
361361
}
362362
} else {
363-
val filteredTasks =
364-
kspExtension.excludedSources.buildDependencies.getDependencies(null).map { it.name }
365363
kotlinCompilation.allKotlinSourceSetsObservable.forAll { sourceSet ->
366-
kspTask.setSource(
367-
sourceSet.kotlin.srcDirs.filter {
368-
!kotlinOutputDir.isParentOf(it) && !javaOutputDir.isParentOf(it) &&
369-
it !in kspExtension.excludedSources
370-
}
371-
)
372-
kspTask.dependsOn(sourceSet.kotlin.nonSelfDeps(kspTaskName).filter { it.name !in filteredTasks })
364+
kspTask.setSource(sourceSet.kotlin.sourceDirectories)
373365
}
374366
}
375367

@@ -603,13 +595,7 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
603595
project.files(kotlinOutputDir).builtBy(kspTaskProvider),
604596
project.files(javaOutputDir).builtBy(kspTaskProvider),
605597
)
606-
if (kotlinCompilation is KotlinCommonCompilation) {
607-
// Do not add generated sources to common source sets.
608-
// They will be observed by downstreams and violate current build scheme.
609-
kotlinCompileProvider.configure { it.source(*generatedSources) }
610-
} else {
611-
kotlinCompilation.defaultSourceSet.kotlin.srcDirs(*generatedSources)
612-
}
598+
kotlinCompileProvider.configure { it.source(*generatedSources) }
613599

614600
kotlinCompileProvider.configure { kotlinCompile ->
615601
when (kotlinCompile) {

integration-tests/src/test/kotlin/com/google/devtools/ksp/test/AndroidIT.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ class AndroidIT(useKSP2: Boolean) {
2121

2222
// Disabling configuration cache. See https://github.com/google/ksp/issues/299 for details
2323
gradleRunner.withArguments(
24-
"clean", "build", "minifyReleaseWithR8", "--configuration-cache-problems=warn", "--info", "--stacktrace"
24+
"clean",
25+
"build",
26+
"minifyReleaseWithR8",
27+
"--info",
28+
"--stacktrace",
29+
"-Dorg.gradle.unsafe.isolated-projects=true"
2530
).build().let { result ->
2631
Assert.assertEquals(TaskOutcome.SUCCESS, result.task(":workload:build")?.outcome)
2732
val mergedConfiguration = File(project.root, "workload/build/outputs/mapping/release/configuration.txt")

integration-tests/src/test/resources/playground-android/build.gradle.kts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,3 @@ buildscript {
88
google()
99
}
1010
}
11-
12-
allprojects {
13-
val testRepo: String by project
14-
repositories {
15-
maven(testRepo)
16-
mavenCentral()
17-
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/")
18-
google()
19-
}
20-
}

integration-tests/src/test/resources/playground-android/test-processor/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ repositories {
1212
maven(testRepo)
1313
mavenCentral()
1414
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/")
15+
google()
1516
}
1617

1718
dependencies {

integration-tests/src/test/resources/playground-android/workload/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ repositories {
1313
maven(testRepo)
1414
mavenCentral()
1515
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/")
16+
google()
1617
}
1718

1819
dependencies {

0 commit comments

Comments
 (0)