Wyatt Kennedy
07/23/2023, 4:02 AMkotlin("jvm") version "1.9.0"
, I get errors like /kotlin-stdlib-common-1.9.0.jar!/META-INF/kotlin-stdlib-common.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.5.1
when compiling the host. When I explicitly set the plugin version in the definition project to "1.5.10"
and down the version of kotlinx-coroutines-core
to something that plays nice, it does kind've load and I can apply my gradle plugin in a project, but then I get errors like Unable to construct script definition: Unable to load base class kotlin.script.experimental.api.KotlinType@1f76d9c0
when trying to run a task that runs a script. The script definition code is an exact copy of the kotlin scripting example for now while I debug this, except the @KotlinScript
is named ShaderDef
. The host does this in a gradle task currently:
val configuration = createJvmCompilationConfigurationFromTemplate<ShaderDef> { }
val results = BasicJvmScriptingHost().eval(extension.file.get().toScriptSource(), configuration, null)
results.reports.forEach {
if (it.severity > ScriptDiagnostic.Severity.DEBUG) {
println(" : ${it.message}" + if (it.exception == null) "" else ": ${it.exception}")
}
}
I'll keep tinkering, but I'm really lost here. Has anyone else tried to use a custom gradle plugin as a scripting host?Wyatt Kennedy
07/23/2023, 4:08 AMWyatt Kennedy
07/23/2023, 4:15 AMjvm {
dependenciesFromCurrentContext(wholeClasspath = true)
}
isn't sufficient when the plugin is applied as a gradle plugin for some reason?mbonnin
07/23/2023, 9:40 AMmbonnin
07/23/2023, 9:40 AMmbonnin
07/23/2023, 9:42 AMWyatt Kennedy
07/23/2023, 6:09 PMmbonnin
07/23/2023, 6:11 PMmbonnin
07/23/2023, 6:11 PMWyatt Kennedy
07/23/2023, 6:23 PMUnable to load base class kotlin.script.experimental.api.KotlinType@1f776f8b
when trying to apply my plugin to another project so I'm guessing those compat issues were unrelated to the root cause here. Is there something funky about plugin classpaths where they don't automatically include their dependencies like org.jetbrains.kotlin:kotlin-scripting-jvm
or something?mbonnin
07/23/2023, 6:24 PManother projectIs that other project also using 8.2?
mbonnin
07/23/2023, 6:24 PMIs there something funky about plugin classpathsThere so many funky things with Gradle classpaths I don't even know where to begin
mbonnin
07/23/2023, 6:28 PMmbonnin
07/23/2023, 6:30 PM// build-logic/build.gradle.kts
dependencies {
implementation("com.example:com.example-gradle-plugin:$version")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.20")
// etc...
}
mbonnin
07/23/2023, 6:30 PMIs there anything more to this error ? (stacktrace or so?)Unable to load base class kotlin.script.experimental.api.KotlinType@1f776f8b
mbonnin
07/23/2023, 6:31 PMNoSuchMethod
or ClassNotFound
or something like thisWyatt Kennedy
07/23/2023, 6:31 PMmbonnin
07/23/2023, 6:32 PM./gradlew --stop
before that too just in caseWyatt Kennedy
07/23/2023, 6:37 PMCaused by: java.lang.ClassNotFoundException: org.ksentiment.ShaderDef
at kotlin.script.experimental.jvm.JvmGetScriptingClass.invoke(jvmScriptingHostConfiguration.kt:117)
that class ShaderDef is my script defintion class annotated with @KotlinScript.
My plugin project does includueBuild("../shader-script-definition) and then appears to find the dependency fine and compile it.mbonnin
07/23/2023, 6:38 PMWyatt Kennedy
07/23/2023, 6:38 PM_implementation_("org.ksentiment:shader-script-definition")
mbonnin
07/23/2023, 6:39 PMmbonnin
07/23/2023, 6:40 PMincludueBuild("../shader-script-definition)
• shader-script-definition
• consumer build
?mbonnin
07/23/2023, 6:41 PMconsumer build
includes the plugin? Do you publish it somewhere or is it a composite build too?Wyatt Kennedy
07/23/2023, 6:42 PMinclude("client")
includeBuild("shader-script-definition")
pluginManagement {
includebuild("shader-plugin")
}
client is the consumer and is normal included in the root project.Wyatt Kennedy
07/23/2023, 6:44 PMplugins {
kotlin("plugin.serialization") version "1.6.10"
kotlin("multiplatform")
id("org.ksentiment.shader-plugin") version "1.0-SNAPSHOT"
`maven-publish`
}
mbonnin
07/23/2023, 6:44 PMWyatt Kennedy
07/23/2023, 6:44 PMmbonnin
07/23/2023, 6:45 PM"1.0-SNAPSHOT"
version (but that's probably not the cause of the issue)mbonnin
07/23/2023, 6:45 PMinclude("client")
pluginManagement {
includeBuild("shader-script-definition")
includebuild("shader-plugin")
}
mbonnin
07/23/2023, 6:46 PMorg.ksentiment.ShaderDef
is part of shader-script-definition
, I would expect it next to the pluginmbonnin
07/23/2023, 6:46 PMmbonnin
07/23/2023, 6:46 PMWyatt Kennedy
07/23/2023, 6:49 PMWyatt Kennedy
07/23/2023, 8:05 PMmbonnin
07/23/2023, 8:13 PMmbonnin
07/23/2023, 8:13 PMTask clientbuildEnvironment------------------------------------------------------------ Project ':client' ------------------------------------------------------------ classpath +--- project :plugin | \--- github.fatalcatharsis:script-definition -> project :script-definition | +--- org.jetbrains.kotlinkotlin stdlib jdk81.8.20 (*) | +--- org.jetbrains.kotlinkotlin scripting common1.8.20 (*)
mbonnin
07/23/2023, 8:13 PMbuildEnvironment
finds itmbonnin
07/23/2023, 8:14 PMmbonnin
07/23/2023, 8:22 PMmbonnin
07/23/2023, 8:29 PMapply()
method, it finds a class:
val clazz = Class.forName("github.fatalcatharsis.ScriptDefinition")
println("clasName '${clazz}'")
mbonnin
07/23/2023, 8:58 PMmbonnin
07/23/2023, 9:10 PMthis.javaClass.classLoader = {VisitableURLClassLoader$InstrumentingVisitableURLClassLoader@13732} "InstrumentingVisitableURLClassLoader(ClassLoaderScopeIdentifier.Id{coreAndPlugins:settings[:]:buildSrc[:]:root-project[:]:project-client(export)})"
ScriptCompilationConfiguration::class.java.classLoader = {VisitableURLClassLoader@13733} "VisitableURLClassLoader(ant-and-gradle-loader)"
Wyatt Kennedy
07/23/2023, 9:10 PMmbonnin
07/23/2023, 9:11 PMmbonnin
07/23/2023, 9:11 PMScriptDefinition
https://github.com/JetBrains/kotlin/blob/5424c54fae7b4836506ec711edc0135392b445d6/[…]kotlin/script/experimental/jvm/jvmScriptingHostConfiguration.ktmbonnin
07/23/2023, 9:12 PMScriptDefinition
is not in that specific classloadermbonnin
07/23/2023, 9:12 PMmbonnin
07/23/2023, 9:13 PMWyatt Kennedy
07/23/2023, 9:17 PMmbonnin
07/23/2023, 9:19 PMWe're well outside my realm of understanding100% same here, sorry 😅
mbonnin
07/23/2023, 9:20 PMmbonnin
07/23/2023, 9:24 PMmbonnin
07/23/2023, 9:24 PMmbonnin
07/23/2023, 9:25 PMmbonnin
07/23/2023, 9:25 PMkotlin-scripting-jvm
on the classpath for their own build.gradle.kts
mbonnin
07/23/2023, 9:26 PMkotlin-stdlib
before any other dependency has any chance to loadmbonnin
07/23/2023, 9:28 PMkotlin-scripting-jvm
and ScriptCompilationConfiguration
is in Gradle "initial" classloader (whatever that is)
• ScriptDefinition
is in Gradle "dependencies" classloader (the classloader that loads the jars from buildEnvironment)mbonnin
07/23/2023, 9:28 PMScriptDefinition
mbonnin
07/23/2023, 9:29 PMmbonnin
07/23/2023, 9:30 PMWyatt Kennedy
07/23/2023, 9:38 PM> Could not resolve all files for configuration ':client:classpath'.
> Could not find github.fatalcatharsis:script-definition:.
Required by:
project :client > github.fatalcatharsis.test-plugin:github.fatalcatharsis.test-plugin.gradle.plugin:1.0-SNAPSHOT > github.fatalcatharsis:plugin:1.0-SNAPSHOT
it recognizes that script-definition is a dependency of the plugin applied to client, but doesn't know where to find it even though I have mavenLocal in both pluginManagement repositories and dependencyResolutionManagement repositories. Something about it being a transitive classpath dependency of client makes it fail resolution. This is definitely a question for the gradle fellas now. @mbonnin you've been incredibly helpful sir. If you have a tip jar, I'd like to buy you a beer :).mbonnin
07/23/2023, 9:43 PMmbonnin
07/23/2023, 9:44 PMmbonnin
07/23/2023, 9:45 PMkotlin-scripting-jvm
that allows to pass a class that's going to be used for the context classloaderWyatt Kennedy
07/23/2023, 9:47 PMmbonnin
07/23/2023, 9:47 PMmbonnin
07/23/2023, 9:48 PM$ ./gradlew :client:test-kts-script
> Task :client:test-kts-script
: Using new faster version of JAR FS: it should make your build faster, but the new implementation is experimental
: /Users/mbonnin/git/gradle-plugin-kotlin-script-test-mre/client/example.test.kts (No such file or directory): java.io.FileNotFoundException: /Users/mbonnin/git/gradle-plugin-kotlin-script-test-mre/client/example.test.kts (No such file or directory)
BUILD SUCCESSFUL in 1s
Wyatt Kennedy
07/23/2023, 9:57 PMmbonnin
07/23/2023, 10:01 PMdo I just have to specify these classes I need in the script context because of me using it in a gradle plugin for some reason?Yes. I'm not 100% certain of the reason but my mental model is along the lines of this: by default, the Kotlin scripting host uses the classloader of
ScriptCompilationConfiguration
(which is something from kotlin-scripting-jvm-host-1.8.20.jar
). But since kotlin-scripting-jvm-host-1.8.20.jar
is loaded super early by Gradle (because it also needs it for build.gradle.kts
files), then this classloader doesn't know about things added later during the build evaluation, like your own ScriptDefinition
herembonnin
07/23/2023, 10:02 PMScriptDefinition::class.java.classLoader
, this classloader obviously knows about ScriptDefinition
mbonnin
07/23/2023, 10:03 PMWyatt Kennedy
07/23/2023, 10:05 PM