From 2d14378b308eda7087e5cb70a6813afa0a2666ef Mon Sep 17 00:00:00 2001 From: Sergey Igushkin Date: Fri, 12 Feb 2021 13:33:51 +0300 Subject: [PATCH] Fix extracted metadata for IDE erased when scopes conflict, KT-44845 The root cause was that extractors for different scopes used the same base directory, and each erased its contents. Using different base dirs fixes the issue. Issue #KT-44845 (cherry picked from commit a719656118d0264b9e38a5249d49442356dd3e6a) --- .../kotlin/gradle/HierarchicalMppIT.kt | 35 +++++++++++++++++++ .../plugin/sources/DefaultKotlinSourceSet.kt | 2 +- .../sources/SourceSetMetadataStorageForIde.kt | 3 ++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/HierarchicalMppIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/HierarchicalMppIT.kt index e9cc1e7e4ce7a..e1367280f8553 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/HierarchicalMppIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/HierarchicalMppIT.kt @@ -585,6 +585,41 @@ class HierarchicalMppIT : BaseGradleIT() { } } + @Test + fun testMixedScopesFilesExistKt44845() { + publishThirdPartyLib(withGranularMetadata = true) + + transformNativeTestProjectWithPluginDsl("my-lib-foo", gradleVersion, "hierarchical-mpp-published-modules").run { + gradleBuildScript().appendText( + """ + ${"\n"} + dependencies { + "jvmAndJsMainImplementation"("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2") + "jvmAndJsMainCompileOnly"("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1") + } + """.trimIndent() + ) + + testDependencyTransformations { reports -> + val reportsForJvmAndJsMain = reports.filter { it.sourceSetName == "jvmAndJsMain" } + val thirdPartyLib = reportsForJvmAndJsMain.single { + it.scope == "api" && it.groupAndModule.startsWith("com.example") + } + val coroutinesCore = reportsForJvmAndJsMain.single { + it.scope == "implementation" && it.groupAndModule.contains("kotlinx-coroutines-core") + } + val serialization = reportsForJvmAndJsMain.single { + it.scope == "compileOnly" && it.groupAndModule.contains("kotlinx-serialization-json") + } + listOf(thirdPartyLib, coroutinesCore, serialization).forEach { report -> + assertTrue(report.newVisibleSourceSets.isNotEmpty(), "Expected visible source sets for $report") + assertTrue(report.useFiles.isNotEmpty(), "Expected non-empty useFiles for $report") + report.useFiles.forEach { assertTrue(it.isFile, "Expected $it to exist for $report") } + } + } + } + } + private fun Project.testDependencyTransformations( subproject: String? = null, check: CompiledProject.(reports: Iterable) -> Unit diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/sources/DefaultKotlinSourceSet.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/sources/DefaultKotlinSourceSet.kt index 51712cbfbd978..3009f2a36bbf3 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/sources/DefaultKotlinSourceSet.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/sources/DefaultKotlinSourceSet.kt @@ -156,7 +156,7 @@ class DefaultKotlinSourceSet( ?.associateBy { ModuleIds.fromComponent(project, it.dependency) } ?: emptyMap() - val baseDir = SourceSetMetadataStorageForIde.sourceSetStorage(project, this@DefaultKotlinSourceSet.name) + val baseDir = SourceSetMetadataStorageForIde.sourceSetStorageWithScope(project, this@DefaultKotlinSourceSet.name, scope) if (metadataDependencyResolutionByModule.values.any { it is MetadataDependencyResolution.ChooseVisibleSourceSets }) { if (baseDir.isDirectory) { diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/sources/SourceSetMetadataStorageForIde.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/sources/SourceSetMetadataStorageForIde.kt index 922ad99e095da..a1380cd09c346 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/sources/SourceSetMetadataStorageForIde.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/sources/SourceSetMetadataStorageForIde.kt @@ -39,4 +39,7 @@ object SourceSetMetadataStorageForIde { } fun sourceSetStorage(project: Project, sourceSetName: String) = projectStorage(project).resolve(sourceSetName) + + internal fun sourceSetStorageWithScope(project: Project, sourceSetName: String, scope: KotlinDependencyScope) = + sourceSetStorage(project, sourceSetName).resolve(scope.scopeName) } \ No newline at end of file