Skip to content

Commit ba5efd4

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 ba5efd4

File tree

7 files changed

+13
-42
lines changed

7 files changed

+13
-42
lines changed

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

+2-14
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)

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

+1
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()

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

+2-17
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
5757
import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact
5858
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption
5959
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
60-
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinCommonCompilation
6160
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation
6261
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmCompilation
6362
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinSharedNativeCompilation
@@ -360,16 +359,8 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
360359
)
361360
}
362361
} else {
363-
val filteredTasks =
364-
kspExtension.excludedSources.buildDependencies.getDependencies(null).map { it.name }
365362
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 })
363+
kspTask.setSource(sourceSet.kotlin.sourceDirectories)
373364
}
374365
}
375366

@@ -603,13 +594,7 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
603594
project.files(kotlinOutputDir).builtBy(kspTaskProvider),
604595
project.files(javaOutputDir).builtBy(kspTaskProvider),
605596
)
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-
}
597+
kotlinCompileProvider.configure { it.source(*generatedSources) }
613598

614599
kotlinCompileProvider.configure { kotlinCompile ->
615600
when (kotlinCompile) {

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

+6-1
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")

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

-10
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-
}

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

+1
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 {

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

+1
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)