jbang icon indicating copy to clipboard operation
jbang copied to clipboard

Add default dependencies for Kotlin source

Open linux-china opened this issue 3 years ago • 3 comments

IntelliJ IDEA can setup KotlinJavaRuntime for Kotlin source file, and following dependencies are included by default.

Screen Shot 2021-12-26 at 7 17 06 PM

Please consider to include koltin-stdlib, kotlin-stdlib-jdk7, kotlin-stdlib-jdk8 and kotlin-reflect as default dependencies for Kotlin script.

For example, following Kotlin code is normal:

///usr/bin/env jbang "$0" "$@" ; exit $?

fun main() {
    "First\nSecond".lines().forEach {
        println(it)
    }
}

But if you use jbang run hello.kt and you will get following error because no kotlin stdlib included to run the script.

Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/text/StringsKt
	at HelloKt.main(hello.kt:5)
	at HelloKt.main(hello.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.text.StringsKt
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	... 2 more

Another option is to include -include-runtime for kotlinc when compiling and include Kotlin runtime into the resulting jar.

Of course developers can add //DEPS org.jetbrains.kotlin:kotlin-stdlib:1.6.10 in script, but I think it will bring better developer experience with stdlib dependencies included.

linux-china avatar Dec 27 '21 05:12 linux-china

I think it will bring better developer experience with stdlib dependencies included.

I know it feels like it will but if there is one feature in IDE's I consider a complete antipattern and not helpful its how they have these "auto-added-runtime-classpath-container" features. Eclipse has it - but they stopped doing it, Intellij picked it up and continued expanding it - and they haven't stopped....and its just not right. I'll tell you why:

  • makes the project dependent on one specific IDE (great for IDE vendors, bad for users - especially students)
  • it builds in an expectation everything is available by default where as in real life you really don't want everything there by default (beyond maybe the first initial getting started experience)
  • it uses unnecessary diskspace and does not allow reusing already downloaded dependencies/docs/sources etc.

...now how does that related to JBang - jbang mission is to make java (kotlin and groovy included) accessible to users while still being pushing for good practices. Adding a lot of dependencies by default is not a good practice.

Also jbang does operate under the limitation it has to (for now) "trick" IDE's into grokking a set of .java files are a project on their own thus relying on gradle etc. to be able to represent a project - adding too much magic here will make that harder.

Furthermore, if jbang was to automagitcally add a set of dependencies the user cannot remove them again. Also, we can't just upgrade to the latest greatest - we just did it for the default kotlin runtime which I approved as no other mechanism available at the moment; but that change potentially breaks every kotlin jbang user as now the default behavior changed and the user has no way of making scripts that they know will continue to work.

If we ever did something like this it would be something that has to be encoded as part of the source file or at least have to be overridable/changable by the user - jbang simply have to push for deterministic and reproducible scripts.

It is not easy; but one thing is for sure is that adding stdlibs blindly will make it worse.

I'm not saying no to finding a way to add (minimal) default depencies but it cannot be done blindly. It (probably) needs to be explicit and at least overridable (including possible to turn off)

maxandersen avatar Dec 27 '21 06:12 maxandersen

Agree. A junior developer sent me this message: Why my "hello world" Kotlin can not work with JBang. :). Nobody wants to read the document, and of course I don't want to explain the reason again and again. Maybe kotlin-stdlib-jdk8 is minimal default dependency for Java 8+ and most developers.

linux-china avatar Dec 27 '21 08:12 linux-china

Adding those libs to hello.quote.kotlin template could make sense.

maxandersen avatar Dec 27 '21 09:12 maxandersen