```@file:Repository("<https://maven.pkg.jetbrains....
# scripting
z
Copy code
@file:Repository("<https://maven.pkg.jetbrains.space/public/p/compose/dev/>")
@file:DependsOn("org.jetbrains.compose.runtime:runtime:1.2.1")
@file:DependsOn("org.jetbrains.compose.desktop:desktop-jvm-linux-x64:1.2.1")

import androidx.compose.runtime.*
import androidx.compose.material.*
import androidx.compose.ui.window.*

application {
    Window(
        onCloseRequest = ::exitApplication
    ) {
        Text("Hello, world!")
    }
}
It's saying
artifactResolutionException: Could not find artifact androidx.compose.runtime:runtime:jar:1.2.1 in https___maven.pkg.jetbrains.space_public_p_compose_dev_ (<https://maven.pkg.jetbrains.space/public/p/compose/dev/>)
The same repository and dependency in a regular gradle config file works so I don't see what I'm doing wrong
h
I guess you have to use
runtime-jvm
z
Same error as well
h
z
That seems to have helped some, now I get this error with the other dependency
Copy code
test.main.kts:8:1: warning: file 'org.jetbrains.compose.desktop:desktop-jvm-linux-x64:1.2.1' not found
test.main.kts:8:1: warning: artifactResolutionException: The following artifacts could not be resolved: org.jetbrains.compose.desktop:desktop-jvm-linux-x64:jar:1.2.1, org.jetbrains.compose.foundation:foundation:aar:1.2.1, androidx.compose.foundation:foundation:jar:1.2.1, org.jetbrains.compose.animation:animation:aar:1.2.1, androidx.compose.animation:animation:jar:1.2.1, org.jetbrains.compose.foundation:foundation-layout:aar:1.2.1, androidx.compose.foundation:foundation-layout:jar:1.2.1, org.jetbrains.compose.ui:ui-text:aar:1.2.1, androidx.compose.ui:ui-text:jar:1.2.1, org.jetbrains.compose.ui:ui-util:aar:1.2.1, androidx.compose.ui:ui-util:jar:1.2.1, org.jetbrains.compose.material:material:aar:1.2.1, androidx.compose.material:material:jar:1.2.1, org.jetbrains.compose.animation:animation-core:aar:1.2.1, androidx.compose.animation:animation-core:jar:1.2.1, org.jetbrains.compose.material:material-icons-core:aar:1.2.1, androidx.compose.material:material-icons-core:jar:1.2.1, org.jetbrains.compose.material:material-ripple:aar:1.2.1, androidx.compose.material:material-ripple:jar:1.2.1, org.jetbrains.compose.runtime:runtime:aar:1.2.1, androidx.compose.runtime:runtime:jar:1.2.1, org.jetbrains.compose.ui:ui:aar:1.2.1, androidx.compose.ui:ui:jar:1.2.1, org.jetbrains.compose.runtime:runtime-saveable:aar:1.2.1, androidx.compose.runtime:runtime-saveable:jar:1.2.1, org.jetbrains.compose.ui:ui-geometry:aar:1.2.1, androidx.compose.ui:ui-geometry:jar:1.2.1, org.jetbrains.compose.ui:ui-graphics:aar:1.2.1, androidx.compose.ui:ui-graphics:jar:1.2.1, org.jetbrains.compose.ui:ui-unit:aar:1.2.1, androidx.compose.ui:ui-unit:jar:1.2.1, org.jetbrains.compose.ui:ui-tooling-preview:aar:1.2.1, androidx.compose.ui:ui-tooling-preview:jar:1.2.1: Could not find artifact org.jetbrains.compose.desktop:desktop-jvm-linux-x64:jar:1.2.1 in https___maven.pkg.jetbrains.space_public_p_compose_dev_ (<https://maven.pkg.jetbrains.space/public/p/compose/dev/>)
h
Yes, the compose gradle plugin replaces the androidx artifacts with JetBrains ones. And because you (the script engine) don't use Gradle to resolve the dependencies, you have to do it manually. Maybe adding the google repo is enough, but I expect this wont work (besides missing compose plugin support at all)
z
well I found this option to add the plugin, which might work
Copy code
@file:CompilerOptions("-Xplugin=/home/nick/.gradle/caches/modules-2/files-2.1/org.jetbrains.compose/compose-gradle-plugin/1.2.1/8f05014e323db9a7c32d051962aa06b77fe2f003/compose-gradle-plugin-1.2.1.jar")
now im getting this error
Copy code
java.lang.NoSuchMethodError: 'void androidx.compose.ui.window.Application_desktopKt.application$default(boolean, kotlin.jvm.functions.Function1, int, java.lang.Object)'
        at Test_main.<init>(test.main.kts:15)
Copy code
@file:Repository("<https://maven.pkg.jetbrains.space/public/p/compose/dev/>")
@file:Repository("<https://dl.google.com/dl/android/maven2/>")
@file:DependsOn("org.jetbrains.compose.runtime:runtime-desktop:1.2.1")
@file:DependsOn("org.jetbrains.compose.desktop:desktop-jvm:1.2.1")
@file:DependsOn("org.jetbrains.compose.ui:ui-desktop:1.2.1")
@file:DependsOn("org.jetbrains.compose.material:material-desktop:1.2.1")

import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.window.*

application {
    Window(
        onCloseRequest = ::exitApplication
    ) {
        Text("Hello, world!")
    }
}
h
I don't know you are able to parse the CompilerOptions annotation, but this error message means, the compose plugin didn't run and didn't create the compose overloads
z
I tried passing it to the kotlinc command but its the same
h
Hm, wait, the compose plugin did run, because it replaced the normal application function call with the compose generated overload.
z
I'm not sure cause it seems to show that error with or without compiler options
Yea so in a regular kotlin project, that error gets thrown when the compiler plugin isnt applied
Could the issue lie in the compose plugin replacing org.jetbrains with androidx? https://github.com/JetBrains/compose-jb/blob/382ad5b78f2ccbde41e4f774db4a5f3ef3c6b[…]/compose/src/main/kotlin/org/jetbrains/compose/ComposePlugin.kt Maybe this isn't supported in kts?
It says
java.lang.NoSuchMethodError: 'void androidx.compose.ui.window.Application_desktopKt.application$default(boolean, kotlin.jvm.functions.Function1, int, java.lang.Object)'
but that doesnt match the function signature I'm calling, and that function doesn't even seem to exist in the library source code
c
@zt I know this thread is a bit old, but I am experimenting with Compose in scripting right now and is running into the exact same issue. Did you manage to figure out what the problem was?
z
I also haven't been able to get it to work. The error is a result of the compose compiler not running
c
Hmm, yeah, that has also been my theory. The weird thing is that I have the exact same setup running with the compiler plugin from Serialization, which does work.
I think I tracked down the problem and reported it to Google: https://issuetracker.google.com/u/1/issues/329677662. They have already fixed it. So with a little luck, it should be possible to do this from the next compose release. Note, it looks like they do not have testing on their end and I haven't run extensive tests. So maybe there are other edge cases where the compose compiler needs to be updated.
z
Hopefully that's what makes it work, would love to get this working
Hmm. I'm running into a Stackoverflow exception now when the compose compiler runs on my script @chrmelchior
Copy code
java.lang.StackOverflowError
	at kotlin.script.experimental.api.ErrorHandlingKt.valueOrThrow(errorHandling.kt:305)
	at ComposableSingletons$MainKt$lambda-3$1$2$1$1$2$1.invokeSuspend(Main.kt:63)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:111)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:99)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:585)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:802)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:706)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:693)
Caused by: java.lang.StackOverflowError
	at org.jetbrains.kotlin.ir.descriptors.IrBasedDescriptorsKt.toIrBasedDescriptor(IrBasedDescriptors.kt)
	at org.jetbrains.kotlin.ir.symbols.impl.IrSymbolBase.getDescriptor(IrPrivateSymbolBase.kt:31)
	at org.jetbrains.kotlin.ir.symbols.impl.IrScriptSymbolImpl.getDescriptor(IrPrivateSymbolBase.kt:151)
	at org.jetbrains.kotlin.ir.declarations.impl.IrScriptImpl.getDescriptor(IrScriptImpl.kt:63)
	at org.jetbrains.kotlin.ir.declarations.impl.IrScriptImpl.getDescriptor(IrScriptImpl.kt:24)
	at org.jetbrains.kotlin.ir.descriptors.IrBasedDescriptorsKt.toIrBasedDescriptor(IrBasedDescriptors.kt:1271)
	at org.jetbrains.kotlin.ir.descriptors.IrBasedDescriptorsKt.toIrBasedDescriptor(IrBasedDescriptors.kt:135)
	at org.jetbrains.kotlin.ir.symbols.impl.IrSymbolBase.getDescriptor(IrPrivateSymbolBase.kt:31)
I structured my project to have a basic compose UI that can load scripts containing composable functions and hopefully be able to display them in the same window
c
Yeah, it sounds like there might still be problems. Can you share the script you are trying to compile?
z
Just this
Copy code
@Composable
fun Content() {
  Text("Hello")
}
c
I'll test it later today to see if I see the same. Note, this is how I define dependencies:
Copy code
@file:Repository("<https://repo.maven.apache.org/maven2/>")
@file:Repository("<https://maven.google.com/>")
@file:DependsOn("org.jetbrains.compose:compose-full:1.6.0")
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.8.0")
@file:DependsOn("androidx.collection:collection-jvm:1.4.0")
@file:DependsOn("org.jetbrains.skiko:skiko-awt:0.7.93")
@file:DependsOn("org.jetbrains.skiko:skiko-awt-runtime-macos-arm64:0.7.93")
Especially
compose-full
is nice, since it contains all compose dependencies in one jar
z
Have you gotten a chance to test? I can maybe upload my project to a repository to share
c
Running this script from the CLI with the latest compiler from source
Copy code
@file:Repository("<https://repo.maven.apache.org/maven2/>")
@file:Repository("<https://maven.google.com/>")
@file:DependsOn("org.jetbrains.compose:compose-full:1.6.1")
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.8.0")
@file:DependsOn("androidx.collection:collection-jvm:1.4.0")
@file:DependsOn("org.jetbrains.skiko:skiko-awt:0.7.97")
@file:DependsOn("org.jetbrains.skiko:skiko-awt-runtime-macos-arm64:0.7.97")

import androidx.compose.material.Text
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application

application {
    Window(onCloseRequest = ::exitApplication) {
        Text("Hello, world!")
    }
}
Throws
Copy code
java.awt.HeadlessException
	at java.desktop/sun.java2d.HeadlessGraphicsEnvironment.getDefaultScreenDevice(HeadlessGraphicsEnvironment.java:58)
	at androidx.compose.ui.window.LayoutConfiguration_desktopKt.getGlobalDensity(LayoutConfiguration.desktop.kt:41)
	at androidx.compose.ui.window.Application_desktopKt$awaitApplication$2$1$2$1.invoke(Application.desktop.kt:224)
	at androidx.compose.ui.window.Application_desktopKt$awaitApplication$2$1$2$1.invoke(Application.desktop.kt:219)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jb.kt:107)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jb.kt:33)
	at androidx.compose.runtime.ActualJvm_jvmKt.invokeComposable(ActualJvm.jvm.kt:33)
	at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3303)
	at androidx.compose.runtime.ComposerImpl.composeContent$runtime(Composer.kt:3236)
	at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:725)
	at androidx.compose.runtime.Recomposer.composeInitial$runtime(Recomposer.kt:1071)
	at androidx.compose.runtime.CompositionImpl.composeInitial(Composition.kt:633)
	at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:619)
	at androidx.compose.ui.window.Application_desktopKt$awaitApplication$2$1$2.invokeSuspend(Application.desktop.kt:219)
Which I actually think is expected, since it would make sense for the script engine to not support displays. That said, it does work for Swing UI components. At least I can show a
JFrame
, so my guess is that there is a way around it, maybe by using a
ComposePanel
and putting content there: https://github.com/JetBrains/compose-multiplatform/blob/master/tutorials/Swing_Integration/README.md At least if you want to run things from the CLI.
z
@chrmelchior Hi, I still haven't really gotten much progress, been busy with other projects. Still stuck on that StackOverflowException even after upgrading to kotlin 2.0.0 and the compiler migration stuff If you are able to could you check out my test project and see if theres something you might know?
c
I found a bug in the Kotlin compiler that also needed to be fixed. It was fixed here: https://youtrack.jetbrains.com/issue/KT-67066 but it looks like it isn't available until 2.0.20. I can see it should be part of the dev build
2.0.20-dev-1805
so it would be worth testing that
z
Do you think that's the cause of the stack overflow exception? I'm not too familiar with how the compiler works. Really wish there was a detailed explanation for someone unfamiliar with it like me
@chrmelchior Trying again with Kotlin 2.0.20-Beta2 and it's getting farther than before, I think If I use
kotlin-compose-compiler-plugin-embeddable-2.0.20-Beta2.jar
then its the StackOverflow nonsense If I use
kotlin-compose-compiler-plugin-embeddable-2.0.0.jar
then I get the error below
Copy code
java.lang.NoSuchFieldError: VALIDATE_IR
	at kotlin.script.experimental.api.ErrorHandlingKt.valueOrThrow(errorHandling.kt:305)
	at ComposableSingletons$MainKt$lambda-3$1$2$1$1$2$1.invokeSuspend(Main.kt:62)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:111)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:99)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:585)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:802)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:706)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:693)
Caused by: java.lang.NoSuchFieldError: VALIDATE_IR
	at androidx.compose.compiler.plugins.kotlin.ComposePluginRegistrar$Companion.createComposeIrExtension(ComposePlugin.kt:426)
	at androidx.compose.compiler.plugins.kotlin.ComposePluginRegistrar$Companion.createComposeIrExtension$default(ComposePlugin.kt:389)
	at androidx.compose.compiler.plugins.kotlin.ComposePluginRegistrar.registerProjectComponents(ComposePlugin.kt:311)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.registerExtensionsFromPlugins$cli_base(KotlinCoreEnvironment.kt:740)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$ProjectEnvironment.registerExtensionsFromPlugins(KotlinCoreEnvironment.kt:179)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.configureProjectEnvironment(KotlinCoreEnvironment.kt:637)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt:209)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.createForProduction(KotlinCoreEnvironment.kt:453)
	at org.jetbrains.kotlin.scripting.compiler.plugin.impl.CompilationContextKt.createIsolatedCompilationContext(compilationContext.kt:77)
	at org.jetbrains.kotlin.scripting.compiler.plugin.impl.ScriptJvmCompilerIsolated.compile$lambda$2$lambda$1(ScriptJvmCompilerImpls.kt:65)
	at org.jetbrains.kotlin.scripting.compiler.plugin.impl.ScriptJvmCompilerImplsKt.withScriptCompilationCache(ScriptJvmCompilerImpls.kt:114)
	at org.jetbrains.kotlin.scripting.compiler.plugin.impl.ScriptJvmCompilerImplsKt.access$withScriptCompilationCache(ScriptJvmCompilerImpls.kt:1)
	at org.jetbrains.kotlin.scripting.compiler.plugin.impl.ScriptJvmCompilerIsolated.compile(ScriptJvmCompilerImpls.kt:60)
c
Interesting. I was also about to resume testing this. In my test setup using
2.0.20-Beta2
I am still seeing other errors in
org.jetbrains.kotlin.backend.jvm.ir.JvmIrTypeUtilsKt.getErasedUpperBound(JvmIrTypeUtils.kt:98)
. Will need to dig into why that is happening. On the positive side, with the compose compiler now part of the Kotlin release https://github.com/JetBrains/kotlin/tree/50a6b1d125d7dc3267a9264d7231842e90af585b/plugins/compose it should be easier to debug.
z
The StackOverflow error from what I can tell seems to be because it gets stuck in a loop trying to get the descriptor from IrScript. Where it keeps returning IrScript down the stack until it runs out of memory Maybe a compiler bug?
c
Yes, it is a compiler bug. I attached a patch that should fix it. I will see if I can also write a test for it and get it upstreamed. But with the compose compiler part of the Kotlin repo, everything can now be built locally. I also did run into a StackOverFlow like the one you described. Setting the language version to 1.9 seems to fix it. I modified the https://github.com/Kotlin/kotlin-script-examples to include a Compose example that runs on my machine. I also attached a patch file for that.
z
Interesting. I'm now seeing the IR lowering failed exception after setting the language version
c
Yes, that was the exception I was also getting, the root cause of that is fixed by my patch above. I created a PR against Compose to fix it. So when that is merged, it should work.
z
I'm actually testing out that patch right now and it does work. Just need to work out evaluating the script now
c
I pushed my test sample here: https://github.com/cmelchior/kotlin-script-examples/tree/cm/compose-build-error ... maybe you can use that as a template
z
Actually I was already looking at that. I just got it to display an application window containing some text so compilation is working. I'm gonna now try to get the composable content embedded in the running application. I appreciate everything so far
👍 1
I got it working!
🎉 1
I'm curious about how the language version workaround fixes that stackoverflow exception and how it can be fixed
c
Scripting doesn't support K2 yet, which is why the language version needs to be below 2.0, but work is about to start on making it compatible, so I would expect the support to be there some in the few few months.
z
@chrmelchior Hi, is there a YouTrack issue open for the compose compiler bug with the patch to fix it?
c
Sorry, been on vacation, so I am first seeing this now. There is this PR here: https://kotlin.jetbrains.space/p/kotlin/reviews/1077/timeline (which I think is public) which is referencing this issue: https://youtrack.jetbrains.com/issue/KT-69856
z
I'm unable to access the jb space website. Any estimate on when the fixes will be merged?
c
We are just waiting for an okay from the Compose team. I pinged the relevant parties but are still waiting for a response....so "soon"
z
Okay. Thanks for checking