-
Notifications
You must be signed in to change notification settings - Fork 371
Kotlin Scripting Support #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This looks really promising! Would this allow me to distribute a single self contained script template which could then be opened in Intellij without any other configuration being required? Ideally I could distribute a script template with a reference to the script definition and Intellij would automatically download this definition from Maven and provide rich code completion. |
@DaanDeMeyer this is not yet in this KEEP, but it could be an interesting addition. I'll think about it. Thanks! |
@ligee 2 questions:
Thanks! |
Great that you guys are planning to push scripting to a new level. It's an amazing KEEP and I especially liked the bits about declarative bootstrapping of an IDE dependency and runtime environment. It's also great to read how you envision to cover the whole range from build-files to shell scripts with a unified framework addition. Though, it reads like a very ambitious plan to me including lots of sub-projects. So I'd be curious about the indented time-frame.
Looking forward to kontlinconf so that we can chat more about it! |
@artem-zinnatullin: |
@holgerbrandl, thank you for the feedback. We will try to keep the size of the work here under control. Significant parts of the infrastructure changes described here are, in fact, reshapings and unifyings of the existing scripting infrastructure. But there should be, of course, some particular implementations on top of that, and implementing them will take time, but with clearly defined interfaces we may be able to outsource it partly to the community. |
Yep, I saw Spek there, that's why I asked :)
Spek is a pretty complex test framework that requires test engine/runner to work (currently Spek uses JUnit5 infrastructure). Also, usually you run tests through build system, not sure how scripting will handle that. This can save one nesting level by removing top-level class (which doesn't look as a pain point atm), but on the other hand running tests seems to become much more complicated. Technically everything is possible, but I don't think this is direction we want to move Spek into at the moment. cc @raniejade, @hhariri PTAL |
@ligee for script support, it looks like you have something roughed in for whitelist/blacklisting of functions/classes that can be accessed. This will be important for us to embed Kotlin scripting in Elasticsearch or other engines like Arango, Neo4j, etc. Ensuring what can be called at any given moment, or even what classes can be loaded. Lastly, time limiting of the script running, or loop iteration limiting protections as well (endless loops kill these type of systems). The more integrated this protection can be, the better. |
Is there anywhere to follow the progress on this? |
I'm working now on the updated proposal, along with the series of the prototypes. As soon as it will be ready, I'll publish the updated version. Hopefully, it will happen within a few weeks from now. |
I am interested to leverage these improvements in Spring and Riff. |
@ilya-g please add the requirement: Between compilation and class loading, there should be the option for a verifier that can approve or reject a script. This allows secure scripting to be tied into anything that executes those steps concurrently without needing to re-write every permutation of script runners that might be created. Allowing something like Cuerentena integration https://github.com/kohesive/cuarentena ... There should never be a hard-coded "compile and load class" without the ability to interject between those steps, before class loading. |
@apatrida Thanks. In cases where the script content came from outside of development, e.g. some simple formula for calculating P&L etc. It is critical to validate before loading for the feature to be usable in production. |
The 1.2.50 is out with the new experimental scripting support. The KEEP is updated too, reflects now the approach taken with 1.2.50 and actual implementation status. |
@apatrida, the current approach allows to write a scripting host that will get a compiled script before instantiating/evaluating it and can perform any checks at that point. Could it be an acceptable solution for your case, or you rather believe that we should introduce another entity - a Checker - with a predefined interface, that could be used with standard hosts? |
@ligee it should be after bytecode is generated but before any class is loaded to prevent attacks during static initialization. And would be nice if many implementations that might exist of scripting hosts don't all need rewritten to add verification. So some plugin point that can be exposed in the common hosts and hopefully whatever wrapping people do around those. I'll look at what is in 1.2.50 and see how it is currently set up. |
@apatrida it could be done now exactly at the moment you described, but having an extension point for it in the standard host interface looks like a good idea, I'll think about it. |
@ligee As noted by @rgoldberg in #124, |
It would be great if this optionally supported continuations via an implicit surrounding runBlocking call. Avoid nasty boilerplate and allow suspending functions to be called from top level (without nesting). |
@yschimke You can already have top level |
@LouisCAD to clarify, I really meant calling them at the toplevel e.g. without runBlocking here |
That sounds great, would love to see that advance and be incorporated in some kind of API in order for it be much easier for users of scripting so that they don't have to worry about which stuff to block, what to allow and which compiler options to add in order to be safe (I'll probably try but as someone who's relatively familiar with the language, but not a developer it's probably quite hard to find all the "exploits") |
To be fair, I gave up expecting progress on this. I can have more success if I sit down and code some version of lisp in kotlin over the weekend, as a solution. |
Hi, could anyone give me some context on how can I connect I the documentation for Thanks. If the description is not clear I am happy to provide more context. |
@Snipx, it seems that your scenario is not covered, and it makes sense to introduce a link from evaluation configuration to the compilation one. Could you please file the issue in YouTrack, basically with the description you provided. |
Leaving the link to the ticket for introducing the link from evaluation configuration to the compilation configuration: https://youtrack.jetbrains.com/issue/KT-48758 |
Hi, I am the current maintainer of 'kscript' and would like to align kscript with Kotlin Scripting implementation. I also wonder if Kotlin Scripting can be used in kscript as backing implementation. Can you elaborate on that? |
Hi, @aartiPl! The status of the relevant API is still "experimental", but we are planning to "deexperimentalize" it in the nearest future, maybe even in 1.8.x. This will mean that we'll move the API to the new packages without "experimental" part, and agree to support it in the foreseeable future. There are no significant changes planned in the API itself so far. So I guess you can freely proceed with "porting" or aligning now, just take into account the future renaming. There is very little documentation so far, unfortunately. The annotations and other part of the API could be extracted for the documented source code in the Kotlin repo. All relevant subprojects are located at https://github.com/JetBrains/kotlin/tree/master/libraries/scripting. |
In regards to annotations - I can find only 3 in the source code: But kscript is having much more: In the new kscript (4.1) I have deprecated all comment-based annotations. Also, I have changed existing annotations to be aligned with Kotlin-scripting annotations:
Will you support also additional annotations? What about Import/Include? Is there any artifact with annotations definition which can be imported into the project, so that annotations are recognized (like: https://github.com/holgerbrandl/kscript-annotations)? |
@aartiPl, there are a couple of other annotations, defined in https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-main-kts/src/org/jetbrains/kotlin/mainKts/annotations.kt, but they are not a part of the "official API" yet, although the underlying mechanisms are part of the API. We may make it official in the future, but it is not decided yet. For us as the API providers, it is important to have the underlying semantics to be common and reused, which annotations are used to trigger it is less important at this point. So, to the list of annotations. We have:
We do not have anything analogous to About username/password - you can find more e.g. in this issue - https://youtrack.jetbrains.com/issue/KT-27701#focus=Comments-27-5659673.0-0 and the follow-up. As of using these things, the best way to my mind to have a look at the |
I have made a framework using kts as modules. But I encounter a problem, how to use other compiler plugin like |
@ligee - I have an initial implementation of kotlin scripting for kscript. Can you have a look here: This implementation works for more than 50% of test cases, so not bad for the starter. However, some cases won't apply anymore, e.g., KotlinOptions, and kt files support (I am not sure here: it seems that Kotlin Scripting supports just a single extension for scripts). But some cases fail, although they shouldn't. One notable example is passing the source file's location - currently, the compiler got the wrong path to the script file and can not resolve imports correctly. I tried manually providing the root script's location but did not succeed. Here is the ticket in KScript for implementation of the Kotlin Scripting engine: One more remark from my side, after initial implementation: the need to pass CompilationConfiguration and EvaluationConfiguration to @KotlinScript annotation IMHO makes the whole implementation much more complex than needed. From the KScript implementation perspective, it should be enough to create configurations once and then pass them to compile and evaluate methods. |
@way-zer, plugins are supported in scripting, but you need to configure it with CLI arguments either via regular CLI compiler or via the |
How to use annotation to add plugin. I try CompilerOption: using Is there any way to add compiler plugin using scripting host? |
@way-zer, sorry for the delay. You're right, the usage via The only way to do it now is to use CLI compiler as in my test. |
Update: |
In terms of use cases, the proposed model doesn't feel very handy for templating. By templating, I mean having a file in some alien language with several From the compiler and renderer point of view, each kotlin block can be given an arbitrary filename, but I get the feeling that the IDE will be left aside. I don't see any easy way of defining an "uber format" that would contain kotlin code inside specific parsed constructs. |
I think it's totally possible using your own tools which parses any format and extracts Kotlin snippets (for example see the existing kotlinx-knit). So, how it can be universal for all possible formats and what is use case for it? One can create a utility that parses those blocks and executes them as programs Maybe you could clarify your idea, just genuinely interested what do you mean |
@gildor I'm not speaking about a universal format. Most template engines are agnostic in regard to the generated format. It can be HTML or Cobol, but the template language will have the same syntax. It's possible under Intellij Idea to edit Velocity HTML files, for instance, and have an appropriate syntax coloring in both code (HTML tags and Velocity references and directives). Of course there is a specific tool implied, the Velocity plugin, but it's not specific to the generated language. I'm just dreaming aloud of a Kotlin-powered Velocity-like engine... |
|
@rocketraman I took a look at jte. But it's not the subject here, jte can't incorporate code sections (and as a side note, a template engine which doesn't rely on a proper parser looks a bit frightful to me). |
The I am heavily using this feature in project, in which on |
@way-zer the importing of If you're using |
I'm thinking how to using scripting config instead gradle to config project dependencies. But find it may be impossible. To be specific, in my project, I'm using Another problem, I'd like Or May provide another api to get metadata like annotations (just parse without compile)? |
@way-zer, excuse me, but I cannot grasp the whole problem by your description. Do you have an example code of scripts compilation and refinements, that fails to achieve the results you like? The refinements are designed with the gradle case in mind, in particular so you should be able to get a callback before compiling of a script, extract a subscript from it, that may affect the classpath and other properties of the main script (e.g. like gradle's I do not think that we will provide a separate API for annotations extraction any time soon. This is quite controversial topic and require a lot of discussion and design. |
Thank you, I will try the Compare
|
Sorry for disturbing, I'm trying to implement an IDEA plugin to get better support for my framework Two main feature in general: Dependency between ktsWhen Import source(also TODO for LivePlugin)When I am trying to implement IDEA plugin to support these features, getting rid of the dependence on unofficial and controversial features. But it seems hard to implement, I have tried So I'd like to know is there any way to resolve the problem, or would you like to give some support? |
@way-zer, could you, please, ask your questions in the Kotlin Slack? There is a dedicated channel for scripting discussions - https://slack-chats.kotlinlang.org/c/scripting. Alternatively, please create an YouTrack issue, and we can discuss it there. Using this discussion for the particular questions like this is inconvenient. |
Hi all, I'm catching up to speed here. We are evaluating the use of kts on the server side and security is the biggest question mark. Wanted to follow up on the progress of the "restrictions" configuration. Looking at this previous comment...: #75 (comment) Just wanted to see if there is any progress there? I'm navigating the YouTrack issues and will ask in the Kotlin Slack if I'm granted access. Thank you! |
@jjgp, no, unfortunately there is no progress on that. We may return to this after completely switching to K2. |
Uh oh!
There was an error while loading. Please reload this page.
https://github.com/Kotlin/KEEP/blob/master/proposals/scripting-support.md
replaces https://github.com/Kotlin/KEEP/blob/master/proposals/script-definition-template.md
The text was updated successfully, but these errors were encountered: