Skip to content

Commit

Permalink
Make KSP compatible with Gradle's isolated projects
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-pollom committed Feb 3, 2025
1 parent 553adb6 commit ba5efd4
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,9 @@ abstract class KspAATask @Inject constructor(
cfg.moduleName.value(moduleName)
val kotlinOutputDir = KspGradleSubplugin.getKspKotlinOutputDir(project, sourceSetName, target)
val javaOutputDir = KspGradleSubplugin.getKspJavaOutputDir(project, sourceSetName, target)
val filteredTasks =
kspExtension.excludedSources.buildDependencies.getDependencies(null).map { it.name }
kotlinCompilation.allKotlinSourceSetsObservable.forAll { sourceSet ->
val filtered = sourceSet.kotlin.srcDirs.filter {
!kotlinOutputDir.isParentOf(it) && !javaOutputDir.isParentOf(it) &&
it !in kspExtension.excludedSources
}.map {
// @SkipWhenEmpty doesn't work well with File.
project.objects.fileTree().from(it)
}
cfg.sourceRoots.from(filtered)
cfg.javaSourceRoots.from(filtered)
kspAATask.dependsOn(
sourceSet.kotlin.nonSelfDeps(kspTaskName).filter { it.name !in filteredTasks }
)
cfg.sourceRoots.from(sourceSet.kotlin.sourceDirectories)
cfg.javaSourceRoots.from(sourceSet.kotlin.sourceDirectories)
}
if (kotlinCompilation is KotlinCommonCompilation) {
cfg.commonSourceRoots.from(kotlinCompilation.defaultSourceSet.kotlin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ abstract class KspExtension @Inject constructor(project: Project) {

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

open val arguments: Map<String, String> get() = apOptions.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinCommonCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinSharedNativeCompilation
Expand Down Expand Up @@ -360,16 +359,8 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
)
}
} else {
val filteredTasks =
kspExtension.excludedSources.buildDependencies.getDependencies(null).map { it.name }
kotlinCompilation.allKotlinSourceSetsObservable.forAll { sourceSet ->
kspTask.setSource(
sourceSet.kotlin.srcDirs.filter {
!kotlinOutputDir.isParentOf(it) && !javaOutputDir.isParentOf(it) &&
it !in kspExtension.excludedSources
}
)
kspTask.dependsOn(sourceSet.kotlin.nonSelfDeps(kspTaskName).filter { it.name !in filteredTasks })
kspTask.setSource(sourceSet.kotlin.sourceDirectories)
}
}

Expand Down Expand Up @@ -603,13 +594,7 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
project.files(kotlinOutputDir).builtBy(kspTaskProvider),
project.files(javaOutputDir).builtBy(kspTaskProvider),
)
if (kotlinCompilation is KotlinCommonCompilation) {
// Do not add generated sources to common source sets.
// They will be observed by downstreams and violate current build scheme.
kotlinCompileProvider.configure { it.source(*generatedSources) }
} else {
kotlinCompilation.defaultSourceSet.kotlin.srcDirs(*generatedSources)
}
kotlinCompileProvider.configure { it.source(*generatedSources) }

kotlinCompileProvider.configure { kotlinCompile ->
when (kotlinCompile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ class AndroidIT(useKSP2: Boolean) {

// Disabling configuration cache. See https://github.com/google/ksp/issues/299 for details
gradleRunner.withArguments(
"clean", "build", "minifyReleaseWithR8", "--configuration-cache-problems=warn", "--info", "--stacktrace"
"clean",
"build",
"minifyReleaseWithR8",
"--info",
"--stacktrace",
"-Dorg.gradle.unsafe.isolated-projects=true"
).build().let { result ->
Assert.assertEquals(TaskOutcome.SUCCESS, result.task(":workload:build")?.outcome)
val mergedConfiguration = File(project.root, "workload/build/outputs/mapping/release/configuration.txt")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,3 @@ buildscript {
google()
}
}

allprojects {
val testRepo: String by project
repositories {
maven(testRepo)
mavenCentral()
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/")
google()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repositories {
maven(testRepo)
mavenCentral()
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/")
google()
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ repositories {
maven(testRepo)
mavenCentral()
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/")
google()
}

dependencies {
Expand Down

0 comments on commit ba5efd4

Please sign in to comment.