From 91b1ead5805eb903be41d1fa1c9a49b2b3743e3b Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Wed, 24 Jul 2024 17:01:35 +0500 Subject: [PATCH] Temporary fix: Override Kotlin version in the plugin module Fixes issue #236. Eventually, the plugin should use the version of Kotlin embedded into Gradle. --- plugin/build.gradle | 30 +++++++++++++++++++++++++----- plugin/settings.gradle | 8 ++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/plugin/build.gradle b/plugin/build.gradle index ce285299..59fd4738 100644 --- a/plugin/build.gradle +++ b/plugin/build.gradle @@ -1,4 +1,5 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinVersion +import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapperKt import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask buildscript { @@ -15,10 +16,16 @@ buildscript { dependencies { classpath(libs.kotlinx.teamInfraGradlePlugin) - // Note: unlike the root project, don't override KGP version in this project. - // Gradle plugins should only use the embedded-kotlin version. - // kotlinx-benchmark uses an external KGP the moment... but that should be fixed - // https://github.com/Kotlin/kotlinx-benchmark/issues/244 + + String kotlinVersion = providers.gradleProperty("kotlin_version").orNull + if (kotlinVersion != null && !kotlinVersion.isBlank()) { + // In addition to overriding the Kotlin version in the Version Catalog, + // also enforce the KGP version using a dependency constraint. + // Constraints are stricter than Version Catalog. + constraints { + classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion") + } + } } } @@ -43,7 +50,20 @@ infra { } } -logger.info("Using Kotlin ${libs.versions.kotlin.get()} for project ${project.name}") +String currentKgpVersion = KotlinPluginWrapperKt.getKotlinPluginVersion(project) +logger.info("Using Kotlin Gradle Plugin ${currentKgpVersion} for project ${project.name}") + +String kotlinVersionOverride = providers.gradleProperty("kotlin_version").getOrNull() + +if (kotlinVersionOverride != null) { + String versionCatalogKotlinVersion = libs.versions.kotlin.get() + if (kotlinVersionOverride != versionCatalogKotlinVersion) { + throw new IllegalStateException("Kotlin version in Version Catalog was not overridden. Expected:$kotlinVersionOverride, actual:$versionCatalogKotlinVersion.") + } + if (kotlinVersionOverride != currentKgpVersion) { + throw new IllegalStateException("Kotlin Gradle Plugin version was not overridden. Expected:$kotlinVersionOverride, actual:$currentKgpVersion.") + } +} repositories { mavenCentral() diff --git a/plugin/settings.gradle b/plugin/settings.gradle index 0214a337..13ed37b9 100644 --- a/plugin/settings.gradle +++ b/plugin/settings.gradle @@ -10,6 +10,14 @@ dependencyResolutionManagement { versionCatalogs { create("libs") { from(files("../gradle/libs.versions.toml")) + + String kotlinVersion = providers.gradleProperty("kotlin_version").orNull + if (kotlinVersion != null && !kotlinVersion.isBlank()) { + // Override the default Kotlin version. + // The only intended use-case is for testing dev Kotlin builds using kotlinx-benchmark. + // The Kotlin version should not be overridden during regular development. + version("kotlin", kotlinVersion) + } } } }