Kotlin / kotlinx-atomicfu

The idiomatic way to use atomic operations in Kotlin
Other
827 stars 56 forks source link

Move the logic from koltin repo atomicfu gradle plugin to the gradle plugin in the library. #406

Closed mvicsokolova closed 2 months ago

mvicsokolova commented 3 months ago

This is a reproducer for the problem #399

According to our last solution (https://github.com/Kotlin/kotlinx-atomicfu/pull/386), atomicfu-gradle-plugin provides a transitive atomicfu compiler plugin dependency and relies on the gradle version resolution. So, we provide compiler plugin with the lowest version and expect it to be resolved into the highest version present on the user's classpath.

runtimeOnly "org.jetbrains.kotlin:atomicfu:1.6.21"

But this does not work for the project configuration presented in this PR. This is a minimized version of an MPP project created by a MPP Wizard, which uses libs.versions.toml file as a version catalog.

How to reproduce the failure:

cd integration-testing/examples/compiler-plugin-resolution-bug
gradle clean build

The build will fail with the error:

* Where:
Build file '/Users/Maria.Sokolova/IdeaProjects/kotlinx-atomicfu-new/integration-testing/examples/compiler-plugin-resolution-bug/shared/build.gradle.kts' line: 16

* What went wrong:
org/jetbrains/kotlinx/atomicfu/gradle/AtomicfuKotlinGradleSubplugin$AtomicfuKotlinGradleExtension
> org.jetbrains.kotlinx.atomicfu.gradle.AtomicfuKotlinGradleSubplugin$AtomicfuKotlinGradleExtension

NOTE: if contents of compiler-plugin-resolution-bug/build.gradle.kts are removed, the build completes successfully:

plugins {
    // this is necessary to avoid the plugins to be loaded multiple times
    // in each subproject's classloader
    alias(libs.plugins.kotlinMultiplatform) apply false
}
mvicsokolova commented 2 months ago

The last commit suggests an alternative solution to the problem of 2 atomicfu gradle plugins (#370): the logic of the gradle plugin from the Kotlin repo (org.jetbrains.kotlinx.atomicfu.gradle.AtomicfuKotlinGradleSubplugin) that applies atomicfu compiler plugin transformations may be moved to the library starting from Kotlin 1.9.0. It's possible because the sources of the compiler plugin are being published since Kotlin 1.9.0: kotlin-atomicfu-compiler-plugin-embeddable

This will solve the problem of Kotlin version resolution "before" kotlinx-atomicfu plugin application.

The current solution works in overall, but still has several issues to be solved:

  1. As the kotlin.native.version may be overriden by a user and differ from the KGP version, the Native part of the compiler plugin should be applied with kotlin version equal tokotlin.native.version. For now, the Native part of the compiler plugin is separated from the JVM and JS parts but kotlin.native.version is not extracted.
  2. Till this moment, we didn't pay attention to the fact that KGP version may differ from the version of Kotlin compiler. In atomicfu-gradle-plugin we extract KGP version. Though there is no public API for that yet (I'll attach the issue here).
  3. A test should be added for the case if kotlinx-atomicfu is applied to the project that uses Kotlin version older than 1.9.0
mvicsokolova commented 2 months ago

As the problems 1 and 2 are not critical now, and there were no actual user cases of version difference, I think it's reasonable to postpone their solution (#408) until we have public API to retrieve Kotlin compiler and Kotlin native compiler versions. Here is the corresponding ticket: KT-66384.

I'll remove the separation of JVM/JS and Native compiler plugins and use KGP version to get the compiler plugin artifact, as it was done before.