From d1826374a6b1ef91b3e72a13d955d3667ef397b6 Mon Sep 17 00:00:00 2001 From: Sergey Igushkin Date: Fri, 24 Jan 2020 17:56:18 +0300 Subject: [PATCH] Fix the K/N stdlib leaking into other common source sets The Kotlin/Native stdlib was added to the shared-native source sets in order for the IDE to import the stdlib dependency. However, it was added to the '*Implementation' configuration, from where it was also collected for the dependency source set visibility inference algorithm. The current implementation of source sets visibility inference only works for module dependencies and is not quite suitable for local file dependencies. The implementation should likely be fixed, but for now, it's enough to add the dependency directly to the place where the visibility inference results are taken from by the IDE during import, i.e. to the '*ImplementationDependenciesMetadata' configurations. Also, since all tests were flawed by the leakin K/N stdlib, the K2Metadata/Klib compiler mistakenly didn't include its own built-ins to the module dependencies (and everything worked because the K/N built-ins were there). Now that the K/N doesn't leak, the compiler must add its own built-ins. --- .../cli/metadata/K2MetadataKlibSerializer.kt | 2 +- .../jetbrains/kotlin/gradle/KlibBasedMppIT.kt | 20 ++++++++++++++++++- .../GradleKotlinCompilerWork.kt | 2 +- .../native/KotlinNativeTargetPreset.kt | 2 +- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/metadata/K2MetadataKlibSerializer.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/metadata/K2MetadataKlibSerializer.kt index d185b7f87c8..1a7791c08b6 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/metadata/K2MetadataKlibSerializer.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/metadata/K2MetadataKlibSerializer.kt @@ -52,7 +52,7 @@ internal class K2MetadataKlibSerializer(private val metadataVersion: BuiltInsBin LockBasedStorageManager("K2MetadataKlibSerializer") ) - val analyzer = runCommonAnalysisForSerialization(environment, false, dependencyContainer) + val analyzer = runCommonAnalysisForSerialization(environment, true, dependencyContainer) if (analyzer == null || analyzer.hasErrors()) return diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KlibBasedMppIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KlibBasedMppIT.kt index 61eaf856984..c1185726b61 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KlibBasedMppIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KlibBasedMppIT.kt @@ -8,8 +8,9 @@ package org.jetbrains.kotlin.gradle import org.jetbrains.kotlin.gradle.util.modify import org.jetbrains.kotlin.konan.target.HostManager import org.junit.Assume -import kotlin.test.Ignore +import java.io.File import kotlin.test.Test +import kotlin.test.assertFalse /** FIXME (sergey.igushkin): please enable these tests back as soon as the Kotlin/Native version that is bundled with the * Kotlin distribution supports compilation to klib and targetless klibs. @@ -89,6 +90,23 @@ class KlibBasedMppIT : BaseGradleIT() { assertSuccessful() assertTasksExecuted(*tasksToExecute.toTypedArray()) + + assertFileExists("build/classes/kotlin/metadata/jvmAndJsMain/manifest") + assertFileExists("build/classes/kotlin/metadata/iosMain/iosMain.klib") + + // Check that the common and JVM+JS source sets don't receive the Kotlin/Native stdlib in the classpath: + run { + fun getClasspath(taskPath: String): Iterable { + val argsPrefix = " $taskPath Kotlin compiler args:" + return output.lines().single { argsPrefix in it } + .substringAfter("-classpath ").substringBefore(" -").split(File.pathSeparator) + } + + fun classpathHasKNStdlib(classpath: Iterable) = classpath.any { "klib/common/stdlib" in it.replace("\\", "/") } + + assertFalse(classpathHasKNStdlib(getClasspath(":compileKotlinMetadata"))) + assertFalse(classpathHasKNStdlib(getClasspath(":compileJvmAndJsMainKotlinMetadata"))) + } } } } \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerWork.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerWork.kt index 4ebf85b37cd..0395a092896 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerWork.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerWork.kt @@ -126,7 +126,7 @@ internal class GradleKotlinCompilerWork @Inject constructor( with(log) { kotlinDebug { "Kotlin compiler class: ${compilerClassName}" } kotlinDebug { "Kotlin compiler classpath: ${compilerFullClasspath.joinToString { it.canonicalPath }}" } - kotlinDebug { "Kotlin compiler args: ${compilerArgs.joinToString(" ")}" } + kotlinDebug { "$taskPath Kotlin compiler args: ${compilerArgs.joinToString(" ")}" } } val executionStrategy = kotlinCompilerExecutionStrategy() diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/native/KotlinNativeTargetPreset.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/native/KotlinNativeTargetPreset.kt index ef358813e13..b54d93ecb79 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/native/KotlinNativeTargetPreset.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/native/KotlinNativeTargetPreset.kt @@ -142,7 +142,7 @@ abstract class AbstractKotlinNativeTargetPreset( project.kotlinExtension.sourceSets .filter { it.isIntermediateNativeSourceSet() } .forEach { - it.dependencies { implementation(stdlib) } + project.dependencies.add(it.implementationMetadataConfigurationName, stdlib) } } }