From 8273328726592d3625ab4a79049117b5b4c0cba2 Mon Sep 17 00:00:00 2001 From: Sebastian Sellmair Date: Fri, 28 Apr 2023 17:29:36 +0200 Subject: [PATCH] [Gradle] ProjectMetadataProvider: Allow nullable compiled metadata for SourceSet The commit leading to the issue described in KT-58319 was: 510eca9da1353f3c132160f1fe632c5c5f47b394 [Gradle] Implement source set inspection methods as 'suspend' In this commit one method called 'getCommonSourceSetsForMetadataCompilation' was implemented in a suspend style. This re-implementation however introduced a semantic change to the function: Previously this method (counterintuitively) returned commonMain even if the SourceSet only participated in a single platform compilation. This is because the KotlinMetadataTarget still cas a 'main' compilation that includes 'commonMain'. The previous implementation did not filter this compilation therefore found two platform types for the SourceSet, assuming that it can be compiled to metadata. This lead to a 'commonMain' compilation created which then gets correctly disabled by 'isMetadataCompilationSupported'. After the commit, a single target project did not create a 'commonMain' compilation anymore. This leads to no metadata being provided by the metadata provider. This seems OK, but the 'ProjectMetadataProviderImpl' threw an error in cases the IDE requested this metadata. ^KT-58319 Verification Pending --- .../mpp/GranularMetadataTransformation.kt | 2 +- .../plugin/mpp/ProjectMetadataProviderImpl.kt | 20 ++++++++++--------- .../plugin/mpp/transformMetadataLibraries.kt | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/GranularMetadataTransformation.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/GranularMetadataTransformation.kt index 23e618ed062..d8d6777356c 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/GranularMetadataTransformation.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/GranularMetadataTransformation.kt @@ -75,7 +75,7 @@ internal sealed class MetadataDependencyResolution( abstract class ProjectMetadataProvider : MetadataProvider() { enum class MetadataConsumer { Ide, Cli } - abstract fun getSourceSetCompiledMetadata(sourceSetName: String): FileCollection + abstract fun getSourceSetCompiledMetadata(sourceSetName: String): FileCollection? abstract fun getSourceSetCInteropMetadata(sourceSetName: String, consumer: MetadataConsumer): FileCollection? } } diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/ProjectMetadataProviderImpl.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/ProjectMetadataProviderImpl.kt index e1402d127ea..b3f6e955abb 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/ProjectMetadataProviderImpl.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/ProjectMetadataProviderImpl.kt @@ -25,7 +25,7 @@ internal fun ProjectMetadataProvider( } internal class SourceSetMetadataOutputs( - val metadata: FileCollection, + val metadata: FileCollection?, val cinterop: CInterop? ) { class CInterop( @@ -38,8 +38,11 @@ private class ProjectMetadataProviderImpl( private val sourceSetMetadataOutputs: Map ) : ProjectMetadataProvider() { - override fun getSourceSetCompiledMetadata(sourceSetName: String): FileCollection = - sourceSetMetadataOutputs[sourceSetName]?.metadata ?: error("Unexpected source set '$sourceSetName'") + override fun getSourceSetCompiledMetadata(sourceSetName: String): FileCollection? { + val metadataOutputs = sourceSetMetadataOutputs[sourceSetName] ?: error("Unexpected source set '$sourceSetName'") + return metadataOutputs.metadata + } + override fun getSourceSetCInteropMetadata(sourceSetName: String, consumer: MetadataConsumer): FileCollection? { val metadataOutputs = sourceSetMetadataOutputs[sourceSetName] ?: error("Unexpected source set '$sourceSetName'") @@ -66,18 +69,17 @@ internal suspend fun Project.collectSourceSetMetadataOutputs(): Map { +private suspend fun KotlinMultiplatformExtension.sourceSetsMetadataOutputs(): Map { KotlinPluginLifecycle.Stage.AfterFinaliseDsl.await() - return sourceSets.mapNotNull { sourceSet -> - val destination = when (val compilation = project.findMetadataCompilation(sourceSet) ?: return@mapNotNull null) { + return sourceSets.associateWith { sourceSet -> + when (val compilation = project.findMetadataCompilation(sourceSet)) { + null -> null is KotlinCommonCompilation -> compilation.output.classesDirs is KotlinSharedNativeCompilation -> compilation.output.classesDirs else -> error("Unexpected compilation type: $compilation") } - - Pair(sourceSet, destination) - }.toMap() + } } private suspend fun KotlinMultiplatformExtension.cInteropMetadataOfSourceSets( diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/transformMetadataLibraries.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/transformMetadataLibraries.kt index 4d77ca2070a..9d297ecf7a1 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/transformMetadataLibraries.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/transformMetadataLibraries.kt @@ -23,7 +23,7 @@ internal fun Project.transformMetadataLibrariesForIde( ): Map> { return when (val metadataProvider = resolution.metadataProvider) { is ProjectMetadataProvider -> resolution.visibleSourceSetNamesExcludingDependsOn.associateWith { visibleSourceSetName -> - metadataProvider.getSourceSetCompiledMetadata(visibleSourceSetName) + metadataProvider.getSourceSetCompiledMetadata(visibleSourceSetName) ?: emptyList() } is ArtifactMetadataProvider -> transformMetadataLibrariesForIde(