Hi, I am trying to make a gradle plugin which conf...
# plugin-development
s
Hi, I am trying to make a gradle plugin which configures the kotlin compiler options for a project. in my plugin's code, I have something which looks roughly like this:
Copy code
fun configureProject() {
    project.configure<KotlinProjectExtension> {
        if (this is HasConfigurableKotlinCompilerOptions<*>)
            configureCommonCompilerOptions()

        when (this) {
            is KotlinJvmProjectExtension    -> {
                configureJvmCompilerOptions()
            }

            is KotlinMultiplatformExtension -> {
                targets.withType<KotlinJvmTarget>().configureEach {
                    configureJvmCompilerOptions()
                }

                targets.withType<KotlinJsIrTarget>().configureEach {
                    configureJsCompilerOptions()
                }
            }
        }
    }
}

private fun HasConfigurableKotlinCompilerOptions<*>.configureCommonCompilerOptions() {
    val nyx = this@NyxKotlinExtension
    compilerOptions {
        // ...
    }
}
however, the problem I'm facing is that when I add this plugin to another project and this code gets executed, then the following exception will be thrown:
Copy code
Caused by: java.lang.ClassCastException: class org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension_Decorated cannot be cast to class org.jetbrains.kotlin.gradle.dsl.HasConfigurableKotlinCompilerOptions (org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension_Decorated and org.jetbrains.kotlin.gradle.dsl.HasConfigurableKotlinCompilerOptions are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader$InstrumentingVisitableURLClassLoader @47f6e669)
this makes absolutely no sense as to why it's occurring, as
KotlinJvmProjectExtension
definitely inherits from
HasConfigurableKotlinCompilerOptions
. if you would like to see exactly what I'm doing, then that can be found here: https://github.com/solo-studios/nyx/blob/ae325df6221244572757ff661767621c8de01262/src/main/kotlin/ca/solostudios/nyx/plugin/compile/NyxKotlinExtension.kt#L370-L439 you can test this behaviour by doing the following in any gradle project: first, add the following to your `settings.gradle.kts`:
Copy code
pluginManagement {
    repositories {
        maven("<https://maven.solo-studios.ca/snapshots/>")
        mavenCentral()
        gradlePluginPortal()
    }
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "ca.solo-studios.nyx") {
                useModule("ca.solo-studios:nyx:0.3.0-20250308.223428-37")
            }
        }
    }
}
then, add the following to your `build.gradle.kts`:
Copy code
plugins {
    id("ca.solo-studios.nyx") version "0.3.0-SNAPSHOT"
}
re-import the project, and it will fail. the source code for the entire project can be found on github. edit: I have published a workaround, so you need to do funny resolution stuff to get the broken version.
a
does this error look like the same issue? https://github.com/gradle/gradle/issues/27218
s
no, the issue is a
ClassCastException
rather than a
NoClassDefFoundError
. I've never experienced that error, and even handle it in a few cases where both plugins might not be loaded: https://github.com/solo-studios/nyx/blob/master/src/main/kotlin/ca/solostudios/nyx/plugin/compile/NyxCompilePlugin.kt#L57-L67
Copy code
try {
    project.plugins.withType<KotlinBasePlugin> {
        val kotlinExtension = compileExtension.create<NyxKotlinExtension>(NyxKotlinExtension.NAME, project, compileExtension)

        project.afterEvaluate {
            kotlinExtension.configureProject()
        }
    }
} catch (_: NoClassDefFoundError) {
    // ignore
}
v
To consolidate the split discussions, the same thread is open here: https://slack-chats.kotlinlang.org/t/27060192/hi-i-am-trying-to-make-a-gradle-plugin-which-configures-the-#d5c82bd7-8817-4fa2-a66c-6f17925ce87d It is most likely not a class loader problem but a simple backwards incompatibility of the plugin regarding KGP versions <2.1.0