Change default ksp.allow.all.target.configuration to false
diff --git a/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspConfigurations.kt b/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspConfigurations.kt index 37da92a..aafb830 100644 --- a/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspConfigurations.kt +++ b/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspConfigurations.kt
@@ -20,11 +20,11 @@ private const val PREFIX = "ksp" } - private val allowAllTargetConfiguration = + internal val allowAllTargetConfiguration = project.providers.gradleProperty("ksp.allow.all.target.configuration") .orNull ?.toBoolean() - ?: true + ?: false // The "ksp" configuration, applied to every compilation. private val configurationForAll = project.configurations.create(PREFIX).apply {
diff --git a/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt b/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt index 9928ac4..a8908af 100644 --- a/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt +++ b/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt
@@ -256,6 +256,13 @@ resourcesOutputDir = resourceOutputDir, androidComponent = component, ) + if (!kspConfigurations.allowAllTargetConfiguration && + project.pluginManager.hasPlugin("kotlin-multiplatform") + ) { + project.configurations.findByName("ksp")?.let { kspConfig -> + processorClasspath.setExtendsFrom(processorClasspath.extendsFrom.filter { it != kspConfig }) + } + } } // The variant API in KMP runs after KotlinCompilerPluginSupportPlugin.applyToCompilation()
diff --git a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/GradleCompilationTest.kt b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/GradleCompilationTest.kt index 6a2c60d..bda3653 100644 --- a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/GradleCompilationTest.kt +++ b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/GradleCompilationTest.kt
@@ -95,7 +95,9 @@ """.trimIndent() ) val kspConfigs = - """configurations.matching { it.name.startsWith("ksp") && !it.name.endsWith("ProcessorClasspath") }""" + """configurations.matching { + |it.name.startsWith("ksp") && it.name != "ksp" && !it.name.endsWith("ProcessorClasspath") + |}""".trimMargin() testRule.appModule.buildFileAdditions.add( """ $kspConfigs.all {
diff --git a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/ProcessorClasspathConfigurationsTest.kt b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/ProcessorClasspathConfigurationsTest.kt index e79fcfb..d98db9e 100644 --- a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/ProcessorClasspathConfigurationsTest.kt +++ b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/ProcessorClasspathConfigurationsTest.kt
@@ -48,7 +48,7 @@ // config name is <KotlinCompileTaskName>.replace("compile", "ksp") + "ProcessorClasspath" // they should extend all non-empty ksp configurations @Test - fun testConfigurationsForSinglePlatformApp() { + fun testConfigurationsForSinglePlatformAppAllowAll() { testRule.setupAppAsJvmApp() testRule.appModule.addSource("Foo.kt", "class Foo") testRule.appModule.buildFileAdditions.add( @@ -70,12 +70,12 @@ """.trimIndent() ) testRule.runner() - .withArguments(":app:testConfigurations", "--info") + .withArguments(":app:testConfigurations", "--info", "-Pksp.allow.all.target.configuration=true") .build() } @Test - fun testConfigurationsForSinglePlatformAppDisallowAll() { + fun testConfigurationsForSinglePlatformApp() { testRule.setupAppAsJvmApp() testRule.appModule.addSource("Foo.kt", "class Foo") testRule.appModule.buildFileAdditions.add( @@ -97,12 +97,12 @@ """.trimIndent() ) testRule.runner() - .withArguments(":app:testConfigurations", "-Pksp.allow.all.target.configuration=false") + .withArguments(":app:testConfigurations") .build() } @Test - fun testConfigurationsForAndroidApp() { + fun testConfigurationsForAndroidAppAllowAll() { testRule.setupAppAsAndroidApp() testRule.appModule.addSource("Foo.kt", "class Foo") testRule.appModule.buildFileAdditions.add( @@ -177,7 +177,93 @@ } """.trimIndent() ) - testRule.runner().withArguments(":app:testConfigurations").build() + testRule.runner() + .withArguments(":app:testConfigurations", "-Pksp.allow.all.target.configuration=true") + .build() + } + + @Test + fun testConfigurationsForAndroidApp() { + testRule.setupAppAsAndroidApp() + testRule.appModule.addSource("Foo.kt", "class Foo") + testRule.appModule.buildFileAdditions.add( + """ + android { + flavorDimensions += listOf("tier", "region") + + productFlavors { + create("free") { + dimension = "tier" + } + create("premium") { + dimension = "tier" + } + create("us") { + dimension = "region" + } + create("eu") { + dimension = "region" + } + } + } + $kspConfigs.all { + // Make sure ksp configs are not empty. + project.dependencies.add(name, "androidx.room:room-compiler:2.4.2") + } + tasks.register("testConfigurations") { + // Resolve all tasks to trigger classpath config creation + dependsOn(tasks["tasks"]) + doLast { + val freeUsDebugConfig = configurations["kspFreeUsDebugKotlinProcessorClasspath"] + val testFreeUsDebugConfig = configurations["kspFreeUsDebugUnitTestKotlinProcessorClasspath"] + val androidTestFreeUsDebugConfig = + configurations["kspFreeUsDebugAndroidTestKotlinProcessorClasspath"] + val freeUsDebugParentConfigs = + setOf( + "ksp", + "kspDebug", + "kspFree", + "kspUs", + "kspFreeUs", + "kspFreeUsDebug" + ) + val testFreeUsDebugParentConfigs = + setOf( + "kspTest", + "kspTestDebug", + "kspTestFree", + "kspTestUs", + "kspTestFreeUs", + "kspTestFreeUsDebug" + ) + val androidTestFreeUsDebugParentConfigs = + setOf( + "kspAndroidTest", + "kspAndroidTestDebug", + "kspAndroidTestFree", + "kspAndroidTestUs", + "kspAndroidTestFreeUs", + "kspAndroidTestFreeUsDebug" + ) + val actualFreeUsDebug = freeUsDebugConfig.extendsFrom.map { it.name }.toSet() + require(actualFreeUsDebug == freeUsDebugParentConfigs) { + "freeUsDebugConfig: expected ${'$'}freeUsDebugParentConfigs but got ${'$'}actualFreeUsDebug" + } + val actualTestFreeUsDebug = testFreeUsDebugConfig.extendsFrom.map { it.name }.toSet() + require(actualTestFreeUsDebug == testFreeUsDebugParentConfigs) { + "testFreeUsDebugConfig: expected ${'$'}testFreeUsDebugParentConfigs but got ${'$'}actualTestFreeUsDebug" + } + val actualAndroidTestFreeUsDebug = androidTestFreeUsDebugConfig.extendsFrom.map { it.name }.toSet() + require(actualAndroidTestFreeUsDebug == androidTestFreeUsDebugParentConfigs) { + "androidTestFreeUsDebugConfig: expected ${'$'}androidTestFreeUsDebugParentConfigs but got ${'$'}actualAndroidTestFreeUsDebug" + } + } + } + """.trimIndent() + ) + testRule.runner() + .withArguments(":app:testConfigurations") + .build() } @Test
diff --git a/integration-tests/src/test/kotlin/com/google/devtools/ksp/test/primary/KMPImplementedIT.kt b/integration-tests/src/test/kotlin/com/google/devtools/ksp/test/primary/KMPImplementedIT.kt index a60b972..0a31825 100644 --- a/integration-tests/src/test/kotlin/com/google/devtools/ksp/test/primary/KMPImplementedIT.kt +++ b/integration-tests/src/test/kotlin/com/google/devtools/ksp/test/primary/KMPImplementedIT.kt
@@ -528,8 +528,7 @@ gradleRunner.withArguments( "--configuration-cache-problems=warn", "clean", - ":workload:build", - "-Pksp.allow.all.target.configuration=false" + ":workload:build" ).buildAndFail().apply { Assert.assertTrue( messages.all { @@ -544,6 +543,7 @@ "--configuration-cache-problems=warn", "clean", ":workload:build", + "-Pksp.allow.all.target.configuration=true" ).build().apply { Assert.assertTrue( messages.all {
diff --git a/integration-tests/src/test/resources/hmpp/workload/build.gradle.kts b/integration-tests/src/test/resources/hmpp/workload/build.gradle.kts index 402a2e4..3ecdee7 100644 --- a/integration-tests/src/test/resources/hmpp/workload/build.gradle.kts +++ b/integration-tests/src/test/resources/hmpp/workload/build.gradle.kts
@@ -49,6 +49,16 @@ } } -dependencies { - add("ksp", project(":test-processor")) +configurations.configureEach { + val targetConfigurations = setOf( + "kspCommonMainMetadata", + "kspJvmJsMetadata", + "kspJvmLinuxX64Metadata", + "kspJvm", + "kspJs", + "kspLinuxX64" + ) + if (name in targetConfigurations) { + project.dependencies.add(this.name, project.dependencies.project(":test-processor")) + } }