https://kotlinlang.org logo
Join SlackCommunities
Powered by
# scripting
  • o

    Olaf Gottschalk

    08/06/2024, 6:22 AM
    I seem to have found this! In my ScriptCompilationConfiguration, I added one line
    Copy code
    object TralaScriptConfiguration : ScriptCompilationConfiguration(
        {
            defaultImports(tralaDefaultImports)
    
            jvm {
                jvmTarget("11")    
                compilerOptions("-Xcontext-receivers")
                   
                updateClasspath(...)
            }
        }
    )
    This seems to do the trick... am I right? I was not immediately on this track as I was looking for a
    freeCompilerArgs
    function...
  • d

    Dylan

    08/06/2024, 4:15 PM
    Hey there. Not sure where to ask this. Is ki shell dead? The last commit is from 9 months ago, and the last update is from 2 years ago. It's still using Kotlin 1.7.0. ki remains useful, and there's no replacement at the time with the same amount of features.
    👀 1
    e
    z
    i
    • 4
    • 5
  • o

    Olaf Gottschalk

    08/09/2024, 11:22 AM
    I ask myself why some of my checks I do in the
    ScriptCompilationConfiguration
    are working properly when calling the
    eval
    on the scripting host, but are ignored when IntelliJ handles the same script file type in its editor... specifically, I added some checks and declare failures using
    markFailureResult
    in
    refineConfiguration { beforeParsing { ... }}
    - but even if I just return an error, the editor does not show it at all...
  • v

    Vampire

    08/14/2024, 2:30 PM
    How again do I use Maven Local from a main.kts script again? I'm pretty sure I did it before, but don't remember how.
    m
    p
    • 3
    • 22
  • t

    Tom Mulcahy

    09/12/2024, 10:24 PM
    I see that
    import Foo.Companion.bar
    works, but Android Studio seems to expect
    import Foo_main.Foo.Companion.bar
    . What IDE are you all using to write kts? Is there a way to use Android Studio (without a sea of red and no auto-complete)?
    i
    • 2
    • 2
  • z

    zt

    09/19/2024, 2:50 AM
    Has anyone been able to get the scripting compiler to run in an android app? I've tried myself but I just ran into thousands of errors about duplicate classes. The same exact project and dependencies works fine on desktop. Iti s just not possible at this point to run it on Android?
  • d

    Daniel Souza Bertoldi

    09/26/2024, 1:03 PM
    My script can't resolve references for
    io
    dependencies, no ideia why. I'll post my code + the error in the thread.
    e
    • 2
    • 5
  • j

    Jintin

    10/16/2024, 11:19 AM
    Hi, sharing a command line tool I built recently to analyze aar/jar info - Jentry Can execute to check exposed api is correctly. Hope you'll like it! https://github.com/Jintin/Jentry
    👍 4
  • o

    Olaf Gottschalk

    11/04/2024, 10:56 AM
    Hi! Now that my custom script language runs like I want it, new problems arise. As the scripts are run on remote servers and can be triggered by remote calls using web endpoints and other things, I want to restrict the things my scripts have access to. Namely: If I want to NOT ALLOW my scripting language to have access to the system environment or file system - how can such things be disabled, hopefully using the
    ScriptCompilationConfiguration
    ? Does anybody know how to do it? Thanks!
    p
    m
    +2
    • 5
    • 37
  • e

    Eugen Martynov

    11/04/2024, 11:53 AM
    Hey hey, I'm tying to run kotlin script on CI and it is corporate envirnement that requires some JDK certificate patching. I see that it fails with 502 when trying to download the dependency. How can I troubleshot it? What could be the reason? How kotlin script resolves the maven dependency?
    p
    • 2
    • 1
  • w

    WayZer

    11/10/2024, 1:43 PM
    Is it bug or by design: The script can't access
    protected
    in base class with new scripting version(KT 2.0+ library) Workaround `@Suppress("INVISIBLE_REFERENCE")`: works when compile and runtime.
    p
    • 2
    • 5
  • j

    Jason

    11/14/2024, 11:31 PM
    Has anyone ran into issues with the Repository annotation, MavenDependencyResolver, Aether and dependencies declaring a greater than version (1.2.+)? I noticed that when the underlying Aether parts I modify the collected dependencies I can fix my issue by using the debugger to fix the version (i.e. 1.2.0). Curious if Aether can be configured to match this semantic versioning pattern?
    p
    • 2
    • 1
  • m

    mbonnin

    11/20/2024, 3:30 PM
    My 2 cents but I believe https://blog.jetbrains.com/kotlin/2024/11/state-of-kotlin-scripting-2024/ may hurt Kotlin scripting more than it's helping. In this tldr; era, the post sounds like a deprecation notice while it's actually quite the opposite? It's about focusing the scripting effort where it matters most?
    💯 3
    p
    e
    +4
    • 7
    • 24
  • s

    spierce7

    11/21/2024, 4:01 PM
    I saw the blog post about kotlin scripting, and was curious what it's impacts are. It's future seems far more vague than before. i.e. I was planning on using Kotlin-playground for an internal project, which I know uses kotlin scripting. Is the kotlin-playground use case of Kotlin scripting dead, or alive?
    e
    • 2
    • 2
  • y

    yostane

    12/05/2024, 4:20 PM
    Hello. I've been using JBang (a tool that got inspiration from kscript) to do the advent of code. You can take a look here to see what it looks like: https://github.com/yostane/adventofcode/tree/main/2024
    👍 1
  • i

    István Mészáros

    12/06/2024, 9:21 AM
    Should IDEA with K2 mode enabled load custom script definitions from imported Gradle projects? It used to work in the past but doesn't work now, not sure when it broke. 🙂 I have a Gradle project with the definition class and the appropriate marker file in META-INF. The IDEA log only loads the Gradle DSL definitions, no error or any mention of my custom script type.
    👀 1
    e
    • 2
    • 1
  • n

    Nino

    12/16/2024, 12:32 PM
    Hello, today I wanted to write a script in Kotlin, but I'm completely lost. Maybe you could give me some guidance. • I'm an Android dev using Android Studio • I want to write a script to move / rename files in a folder. Nothing fancy but I would like to avoid using bash and use Kotlin instead. • The resulting "script" should be usable easily by the other team members or the CI machine, in Windows or Mac. • The resulting "script" should be readable and modifiable by other devs (and not the compiled .jar) So I tried: • https://umang91.medium.com/scripting-in-kotlin-eaf01750bbee which was soooo promising but I see the
    kotlin
    CLI is already dead ? Such a same it seemed such a beautiful and powerful replacement for bash files 😞 • Then I tried Kotlin Notebook but they never mentionned that it needed Intellij Ultimate. I lost half a day trying to download the plugin until I understood. Greedy fuckers 😞 • Then I tried
    ki shell
    but I don't quite understand what is it for. Looks like Kotlin Playground locally, which doesn't fit my need. • Then I'm trying to use
    kotlinc -script
    but there's no documentation (https://kotlinlang.org/docs/command-line.html#run-scripts), or I don't understand the relation between the overly complex documentation linked there (https://kotlinlang.org/docs/custom-script-deps-tutorial.html) and a simple CLI-invoked script in Kotlin. Please guide me in this maze I'm already on the verge to going back to bash scripting. What tool would you use to produce the specified script ?
    p
    v
    • 3
    • 44
  • p

    Piotr Krzemiński

    01/24/2025, 6:00 PM
    I want to compile Kotlin scripts within a Kotlin script 😄 just compile, to validate if they compile fine or with a particular error message. So far I'm getting:
    Copy code
    e: java.lang.ExceptionInInitializerError
    ...
    Caused by: java.lang.ClassCastException: class org.jetbrains.kotlin.idea.KotlinFileType cannot be cast to class org.jetbrains.kotlin.com.intellij.openapi.fileTypes.LanguageFileType (org.jetbrains.kotlin.idea.KotlinFileType and org.jetbrains.kotlin.com.intellij.openapi.fileTypes.LanguageFileType are in unnamed module of loader java.net.URLClassLoader @5d5c04f9
    ...
    my code in the thread
    ♾️ 1
    v
    • 2
    • 9
  • p

    Piotr Krzemiński

    01/27/2025, 3:28 PM
    a pleasant surprise - to consume a KMP library from a script, I didn't have to add
    -jvm
    - just
    @file:DependsOn("it.krzeminski:snakeyaml-engine-kmp:3.1.0")
    works now 🙂
    mind blown 2
    nono 2
    🤔 1
    m
    j
    +2
    • 5
    • 42
  • r

    ribesg

    02/19/2025, 2:11 PM
    How do you make IntelliJ recognize a
    .main.kts
    file as a Kotlin Script and provide highlighting, completion and everything else? I just have a folder containing my scripts for now and it just treats them as plain text
    p
    m
    +2
    • 5
    • 36
  • o

    Olaf Gottschalk

    02/20/2025, 9:29 AM
    Hello Scripters! With moving my own DSL project to Kotlin 2.1 (coming from 1.9), I experience some problems with the tooling setup, specifically with IntelliJ offering syntax highlight and code completion on my own script files with their own extension. It used to work like this: In my DSL library project, there is a class with a
    @KotlinScript
    annotation like this:
    Copy code
    const val TRALA_FILE_EXTENSION = "trala.kts"
    
    @KotlinScript(
        displayName = "TraLa Script",
        fileExtension = TRALA_FILE_EXTENSION,
        compilationConfiguration = TraLaScriptConfiguration::class
    )
    abstract class TraLaScript
    plus, there is a resource file at this location:
    resources/META-INF/kotlin/script/templates/com.bmw.transformation.scripting.TraLaScript
    (which is empty) The compilation configuration looks like this:
    Copy code
    @Suppress("JavaIoSerializableObjectMustHaveReadResolve")
    object TraLaScriptConfiguration : ScriptCompilationConfiguration(
        {
            defaultImports(traLaDefaultImports)
    
            ide {
                acceptedLocations(ScriptAcceptedLocation.Everywhere)
            }
    
            jvm {
                jvmTarget("17")
                compilerOptions("-Xcontext-receivers")
            }
        }
    )
    The idea is that any project that uses my DSL library can have files anywhere (see
    acceptedLocations
    with the extension
    .trala.kts
    and the editor will offer syntax highlight and code completion / inspection. This used to work. Sometimes I had to restart IntelliJ... it was never straight forward. But it worked. Now, I cannot see any way to make the editor behave like I want. And I do not know what changed. In projects that DEPEND on my lib (with this setup), nothing works any more. In my library project itself, the default imports don't seem to work anymore! When I add a new file
    foo.trala.kts
    and start typing code, it inserts import statements that it should not, because they are part of the default imports... What am I doing wrong here? Is there any good documentation on these matters? Thanks! Olaf
    👀 1
    i
    v
    • 3
    • 6
  • p

    Piotr Krzemiński

    02/20/2025, 1:00 PM
    is there a remote chance to use Gradle Version Catalog with
    @file:DependsOn("...")
    ? 🙈 scripting would have to know where to look for the TOML file, and it's non-trivial to infer. It could traverse starting from the script's location, up the file tree, until it finds
    gradle/libs.versions.toml
    m
    v
    a
    • 4
    • 38
  • p

    Piotr Krzemiński

    02/20/2025, 3:06 PM
    I need help untangling what's wrong with fix(deps): update ktor monorepo to v3.1.0. Calling
    .github/workflows/end-to-end-tests.main.kts
    here: https://github.com/typesafegithub/github-workflows-kt/actions/runs/13436465865/job/37545104505?pr=1830 causes:
    java.lang.NoClassDefFoundError: kotlinx/io/unsafe/UnsafeBufferOperations
    . So far I checked that: • I cannot reproduce it locally (purged all caches, used Java 11) - reproduced it by setting a GitHub token, only then the problematic code path is executed • if I remove
    @file:DependsOn("io.github.typesafegithub:action-updates-checker:3.2.1-SNAPSHOT")
    and any pieces that depend on functions from it, it works - so this package is to blame •
    UnsafeBufferOperations
    was introduced in kotlinx-io 0.5.0 • ktor started using kotlinx-io starting 3.1.0 in this commit Something must still depend on kotlinx-io older than 0.5.0, but I cannot pin it down...
    ✅ 1
    v
    • 2
    • 20
  • r

    Rob Elliot

    02/28/2025, 1:20 PM
    Hi - is it possible in
    ScriptCompilationConfiguration
    to require a specific return type for the script? So that the script will fail to compile if the last expression is not of that type?
    i
    • 2
    • 1
  • w

    WayZer

    03/29/2025, 2:50 PM
    Just try scripting K2 with 2.1.20, still some bug. Script like this
    Copy code
    object TableVersion : IdTable<String>("TableVersion") {
        // can't use `text` as h2db don't support for primaryKey
        override val id: Column<EntityID<String>> = varchar("table", 64).entityId()
        override val primaryKey: PrimaryKey = PrimaryKey(id) // h2database#2191
        //...
    }
    will complain
    PrimaryKey
    class (inner class of Table) is not found. This example works with
    -language-version=1.9
    i
    • 2
    • 1
  • z

    zt

    04/04/2025, 4:12 AM
    I'm trying to get IntelliJ IDEA to recognize my custom script but it just refuses to do it. I've created an empty file at
    META-INF/kotlin/script/templates/dev.zt64.composables.ComposableScript
    but when I select the script nothing is resolved and the classpath is missing dependencies. I've tried adding
    isStandalone(false)
    to my compilation configuration as well here is the repository https://github.com/zt64/compose-scripts
    👀 1
  • o

    Olaf Gottschalk

    04/25/2025, 5:41 AM
    I found another very interesting quirk that I cannot explain to myself... How can it be that there is a difference in what the Kotlin compiler does when comparing internal IntelliJ / Gradle compilation with script compilation? I had this function definition in my internal scripting language:
    Copy code
    @TransformationDsl
        infix fun <D : Any, VO> SetOperation<S, D>.by(valueMapper: ValueMapper<Any?, VO>) =
            apply { source = source.map(valueMapper) }
    When this function is used in "normal IntelliJ" contexts, a call like this compiles and works just fine:
    Copy code
    set optional "overallWeightTransportation" from "GrossWeightTransportationQuantity" by unitCodeMapper
    If you look very closely to my generics, the generic type
    VO
    is not really relevant to this function, it has not bounds, the usage of the parameter
    valueMapper
    when passed to the
    map
    function does not care about this generic type, as
    map
    accepts everything. I did not realize this was over-engineered until when compiled with the script compiler I got this error:
    Copy code
    set optional "overallWeightTransportation" from "GrossWeightTransportationQuantity" by unitCodeMapper //later mandatory
                                                                                        ^^ Cannot infer type for this parameter. Specify it explicitly.
                                                                                        ^^ Not enough information to infer type argument for 'VO'.
    The key question is now: why does it work just fine in IntelliJ, but not in scripting compilation? Solution for my immediate problem was to replace
    VO
    with
    *
    of course... but the question remains... ;-)
    c
    • 2
    • 2
  • l

    louiscad

    05/05/2025, 12:34 AM
    Showing the intermediate values in scratch files is a wonderful idea!
    💯 4
    g
    v
    • 3
    • 4
  • r

    ritesh

    05/09/2025, 3:11 PM
    Wanted to get a sense of people if they are using k-script in CI and if there had been any gotchas or pain-points as compared to shell scripting or python-scripting, when to comes to debugging and other factors.
    v
    c
    • 3
    • 8
  • d

    Daniel Pitts

    05/10/2025, 4:41 PM
    Is it possible to use Kotlin Scripting as a way to allow extending a C++ program? For example, if I were to create a game engine in C++, and wanted to enable Kotlin scripting, would there be an easy way to do that?
    e
    m
    • 3
    • 6