Given a Gradle 8.6 build that applies `kotlin-dsl`...
# getting-started
v
Given a Gradle 8.6 build that applies
kotlin-dsl
plugin and contains a pre-compiled script plugin (also had this with plain-old
.kt
file under same circumstances). If I have simplified this code in the plugin:
Copy code
val artifactsGetter: Provider<Configuration>.(Action<AttributeContainer>) -> Provider<ArtifactView> =
    { attributesAction -> map { it.incoming.artifactView { attributes(attributesAction) } } }

configurations
    .runtimeClasspath
    .artifactsGetter {
        attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.JAR_TYPE)
    }
It compiles perfectly fine. But if I try to apply that plugin, I get
Copy code
class Bar_gradle$$$result$1 cannot be cast to class org.gradle.api.Action (Bar_gradle$$$result$1 is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader$InstrumentingVisitableURLClassLoader @649f4a9; org.gradle.api.Action is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @5f9d02cb)
If I look at the bytecode with
javap
what is generated is
Copy code
final class Bar_gradle$$$result$1 extends kotlin.jvm.internal.Lambda implements kotlin.jvm.functions.Function1<org.gradle.api.attributes.AttributeContainer, kotlin.Unit> {
Does anyone have an idea why this is happening and whose "fault" it is? Should I report this to Kotlin or Gradle? Is there a generic work-around besides changing every case that fails at runtime to
.artifactsGetter(Action { ... })
instead of
.artifactsGetter { ... }
? (With Gradle 7.6.4 and thus KGP 1.7.10 it worked fine as long as the language version stayed at 1.4, but started to fail when I set it to 1.7, with Gradle 8.6 and thus KGP 1.9.20 it even fails with language version 1.4)
e
Looks like a Kotlin compilation issue. The lambda should have been compiled to a class that implements
Action
, shouldn’t it? At least is that how I thought SAM conversions worked.
v
Me too, just wanted some reassurance before I create an issue 🙂
🙂 1