From 808d4353e5bef8831e5d08f34fa634e390112eba Mon Sep 17 00:00:00 2001 From: Dmitrii Krasnov Date: Wed, 21 Feb 2024 15:49:35 +0100 Subject: [PATCH] [Gradle] Fixed race condition during k/n bundle downloading Extended lock bounds. Moved removing an old bundle back to configuration phase. ^KT-65985 Fixed --- .../KotlinNativeBundleBuildService.kt | 58 +++++++++++++------ 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/toolchain/KotlinNativeBundleBuildService.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/toolchain/KotlinNativeBundleBuildService.kt index 68ebeb6a0af..75eaf92a4bf 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/toolchain/KotlinNativeBundleBuildService.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/toolchain/KotlinNativeBundleBuildService.kt @@ -38,6 +38,8 @@ internal abstract class KotlinNativeBundleBuildService : BuildService = project.gradle.sharedServices.registerIfAbsent( @@ -62,37 +64,57 @@ internal abstract class KotlinNativeBundleBuildService : BuildService, ) { - val lock = NativeDistributionCommonizerLock(bundleDir) { message -> project.logger.info("Kotlin Native Bundle: $message") } + val lock = + NativeDistributionCommonizerLock(bundleDir) { message -> project.logger.info("Kotlin Native Bundle: $message") } - if (reinstallFlag) { - lock.withLock { - bundleDir.deleteRecursively() - } - } + lock.withLock { - if (!bundleDir.resolve(KONAN_DIRECTORY_NAME_TO_CHECK_EXISTENCE).exists()) { - val gradleCachesKotlinNativeDir = - kotlinNativeCompilerConfiguration - .singleOrNull() - ?.resolve(kotlinNativeVersion) - ?: error( - "Kotlin Native dependency has not been properly resolved. " + - "Please, make sure that you've declared the repository, which contains $kotlinNativeVersion." - ) + removeBundleIfNeeded(reinstallFlag, bundleDir) - project.logger.info("Moving Kotlin/Native bundle from tmp directory $gradleCachesKotlinNativeDir to ${bundleDir.absolutePath}") - lock.withLock { + if (!bundleDir.resolve(KONAN_DIRECTORY_NAME_TO_CHECK_EXISTENCE).exists()) { + val gradleCachesKotlinNativeDir = + resolveKotlinNativeConfiguration(kotlinNativeVersion, kotlinNativeCompilerConfiguration) + + project.logger.info("Moving Kotlin/Native bundle from tmp directory $gradleCachesKotlinNativeDir to ${bundleDir.absolutePath}") fso.copy { it.from(gradleCachesKotlinNativeDir) it.into(bundleDir) } + project.logger.info("Moved Kotlin/Native bundle from $gradleCachesKotlinNativeDir to ${bundleDir.absolutePath}") } - project.logger.info("Moved Kotlin/Native bundle from $gradleCachesKotlinNativeDir to ${bundleDir.absolutePath}") } project.setupKotlinNativeDependencies(konanTargets) } + private fun removeBundleIfNeeded( + reinstallFlag: Boolean, + bundleDir: File, + ) { + if (reinstallFlag && canBeReinstalled) { + bundleDir.deleteRecursively() + canBeReinstalled = false // we don't need to reinstall k/n if it was reinstalled once during the same build + } + } + + private fun resolveKotlinNativeConfiguration( + kotlinNativeVersion: String, + kotlinNativeCompilerConfiguration: ConfigurableFileCollection, + ): File { + val resolutionErrorMessage = "Kotlin Native dependency has not been properly resolved. " + + "Please, make sure that you've declared the repository, which contains $kotlinNativeVersion." + + val gradleCachesKotlinNativeDir = kotlinNativeCompilerConfiguration + .singleOrNull() + ?.resolve(kotlinNativeVersion) + ?: error(resolutionErrorMessage) + + if (!gradleCachesKotlinNativeDir.exists()) { + throw IllegalArgumentException(resolutionErrorMessage) + } + return gradleCachesKotlinNativeDir + } + private fun Project.setupKotlinNativeDependencies(konanTargets: Set) { val distributionType = NativeDistributionTypeProvider(this).getDistributionType() if (distributionType.mustGeneratePlatformLibs) {