Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Gradle plugin ignores Java sources in post-compile transformations #525

Open
TheMrMilchmann opened this issue Mar 1, 2025 · 0 comments

Comments

@TheMrMilchmann
Copy link

I have a multiplatform project that contains a JVM target with additional Java sources (via withJava()). When applying the atomicfu plugin, the Java sources are no longer present in the jar. This can be mitigated by disabling this transformation (i.e. disabling the JVM transformation or using IR transforms).

For reproducing, the mpp-sample can be adjusted accordingly:

Image

The bug is most likely caused by the rewiring of the compilations classes here:

val classesDirs = compilation.output.classesDirs
// make copy of original classes directory
@Suppress("UNCHECKED_CAST")
val compilationTask = compilation.compileTaskProvider as TaskProvider<KotlinCompileTool>
val originalDestinationDirectory = project.layout.buildDirectory
.dir("classes/atomicfu-orig/${target.name}/${compilation.name}")
compilationTask.configure {
if (it is Kotlin2JsCompile) {
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "EXPOSED_PARAMETER_TYPE")
it.defaultDestinationDirectory.value(originalDestinationDirectory)
} else {
it.destinationDirectory.value(originalDestinationDirectory)
}
}
val originalClassesDirs: FileCollection = project.objects.fileCollection()
.from(compilationTask.flatMap { it.destinationDirectory })
.from({ project.files(classesDirs.from).filter { it.exists() } })
originalDirsByCompilation[compilation] = originalClassesDirs
val transformedClassesDir = project.layout.buildDirectory
.dir("classes/atomicfu/${target.name}/${compilation.name}")
val transformTask = when (target.platformType) {
KotlinPlatformType.jvm, KotlinPlatformType.androidJvm -> {
// create transformation task only if transformation is required and JVM IR compiler transformation is not enabled
if (config.transformJvm) {
project.registerJvmTransformTask(compilation)
.configureJvmTask(
compilation.compileDependencyFiles,
compilation.compileAllTaskName,
transformedClassesDir,
originalClassesDirs,
config
)
.also {
compilation.defaultSourceSet.kotlin.compiledBy(it, AtomicFUTransformTask::destinationDirectory)
}
} else null
}
else -> error("Unsupported transformation platform '${target.platformType}'")
}
if (transformTask != null) {
//now transformTask is responsible for compiling this source set into the classes directory
compilation.defaultSourceSet.kotlin.destinationDirectory.value(transformedClassesDir)
classesDirs.setFrom(transformedClassesDir)

At the very end, setFrom(...) is called to overwrite the classes, but this never considers the output of the Java compile task for KotlinJvmCompilations. A naive fix looks as follows. However, I'm unsure if this is a proper fix for this, as I'm unfamiliar with how exactly the transform functions. Maybe the Java sources should also be considered by the transformation?

diff --git a/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt b/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt
index 052550f..de08900 100644
--- a/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt
+++ b/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt
@@ -17,6 +17,7 @@ import org.gradle.util.*
 import org.jetbrains.kotlin.gradle.dsl.*
 import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
 import org.jetbrains.kotlin.gradle.plugin.*
+import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmCompilation
 import org.jetbrains.kotlin.gradle.tasks.*
 import java.io.*
 import java.util.*
@@ -362,6 +363,9 @@ private fun Project.configureTransformationForTarget(target: KotlinTarget) {
             //now transformTask is responsible for compiling this source set into the classes directory
             compilation.defaultSourceSet.kotlin.destinationDirectory.value(transformedClassesDir)
             classesDirs.setFrom(transformedClassesDir)
+            if (compilation is KotlinJvmCompilation) {
+                classesDirs.from(compilation.compileJavaTaskProvider)
+            }
             classesDirs.setBuiltBy(listOf(transformTask))
             tasks.withType(Jar::class.java).configureEach {
                 if (name == target.artifactsTaskName) {
@TheMrMilchmann TheMrMilchmann changed the title The Gradle plugin discards Java sources in post-compile transformations The Gradle plugin ignores Java sources in post-compile transformations Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

1 participant