jbangdev / jbang

Unleash the power of Java - JBang Lets Students, Educators and Professional Developers create, edit and run self-contained source-only Java programs with unprecedented ease.
https://jbang.dev
MIT License
1.42k stars 157 forks source link

Can't run Kotlin files in Windows with more than one dependency in the classpath #1447

Closed garodriguezlp closed 2 years ago

garodriguezlp commented 2 years ago

Describe the bug Can't run Kotlin files in Windows with more than one dependency in the classpath

To Reproduce Steps to reproduce the behavior:

  1. Create a KotlinBug.kt file with the following content:
    
    ///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS com.squareup.okhttp3:okhttp:4.10.0 //DEPS org.json:json:20220320

import okhttp3.OkHttpClient import org.json.JSONObject

fun main() { val jokeRequest = okhttp3.Request.Builder() .url("https://api.chucknorris.io/jokes/random?category=dev") .build() val jokeResponse = OkHttpClient().newCall(jokeRequest).execute() val joke = JSONObject(jokeResponse.body!!.string()) println(joke["value"]) }


2. Run it in a `Windows 10` host with `Powershell`, like this:
```ps1
jbang .\KotlinBug.kt
  1. See the following error:
    [jbang] Building jar...
    error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\com\squareup\okio\okio-jvm\3.0.0\okio-jvm-3.0.0.jar
    error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk8\1.5.31\kotlin-stdlib-jdk8-1.5.31.jar
    error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk7\1.5.31\kotlin-stdlib-jdk7-1.5.31.jar
    error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-common\1.5.31\kotlin-stdlib-common-1.5.31.jar
    error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib\1.6.20\kotlin-stdlib-1.6.20.jar
    error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\annotations\13.0\annotations-13.0.jar
    error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\json\json\20220320\json-20220320.jar
    [jbang] [ERROR] Error during compile
    [jbang] Run with --verbose for more details

Expected behavior

JBang version

[jbang] jbang version 0.98.0
Cache: C:\Users\gustavo.rodriguezl\.jbang\cache
Config: C:\Users\gustavo.rodriguezl\.jbang
Repository:C:\Users\gustavo.rodriguezl\.m2\repository
0.98.0

Additional context

πŸ‘€πŸ‘‡πŸ‘‡

This is how the execution looks if verbose mode is enabled ```ps1 gustavo.rodriguezl ~\src\garodriguezlp\jbang-kotlin-bug ❯ jbang --verbose run .\KotlinBug.kt [jbang] jbang version 0.98.0 [jbang] Build required as C:\Users\gustavo.rodriguezl\.jbang\cache\jars\KotlinBug.kt.b1dcd226c361b16405f16afa01abf35b4bc837922d85cd9439255613c8efff4d.jar not readable or not found. [jbang] Downloading Kotlin 1.6.10. Be patient, this can take several minutes... [jbang] Downloading https://github.com/JetBrains/kotlin/releases/download/v1.6.10/kotlin-compiler-1.6.10.zip [jbang] Redirected to: https://objects.githubusercontent.com/github-production-release-asset-2e65be/3432266/321575a3-fab9-4e5a-9a47-dda6e8303e47?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220902%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220902T191331Z&X-Amz-Expires=300&X-Amz-Signature=429d8a730640798c6f4318335b48b4313757750623969938dc0e41eec48ac37c&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=3432266&response-content-disposition=attachment%3B%20filename%3Dkotlin-compiler-1.6.10.zip&response-content-type=application%2Foctet-stream [jbang] Downloaded file https://objects.githubusercontent.com/github-production-release-asset-2e65be/3432266/321575a3-fab9-4e5a-9a47-dda6e8303e47?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220902%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220902T191331Z&X-Amz-Expires=300&X-Amz-Signature=429d8a730640798c6f4318335b48b4313757750623969938dc0e41eec48ac37c&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=3432266&response-content-disposition=attachment%3B%20filename%3Dkotlin-compiler-1.6.10.zip&response-content-type=application%2Foctet-stream [jbang] Installing Kotlin 1.6.10... [jbang] Unpacking to C:\Users\gustavo.rodriguezl\.jbang\cache\kotlincs\1.6.10 [jbang] Repositories: mavencentral=https://repo1.maven.org/maven2/ [jbang] Resolving dependencies... [jbang] com.squareup.okhttp3:okhttp:jar:4.10.0 org.json:json:jar:20220320 Done [jbang] Dependencies resolved [jbang] Building jar... [jbang] Compile: C:\Users\gustavo.rodriguezl\.jbang\cache\kotlincs\1.6.10\kotlinc\bin\kotlinc.bat -classpath C:\Users\gustavo.rodriguezl\.m2\repository\com\squareup\okhttp3\okhttp\4.10.0\okhttp-4.10.0.jar;C:\Users\gustavo.rodriguezl\.m2\repository\com\squareup\okio\okio-jvm\3.0.0\okio-jvm-3.0.0.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk8\1.5.31\kotlin-stdlib-jdk8-1.5.31.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk7\1.5.31\kotlin-stdlib-jdk7-1.5.31.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-common\1.5.31\kotlin-stdlib-common-1.5.31.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib\1.6.20\kotlin-stdlib-1.6.20.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\annotations\13.0\annotations-13.0.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\json\json\20220320\json-20220320.jar -d C:\Users\gustavo.rodriguezl\.jbang\cache\jars\KotlinBug.kt.b1dcd226c361b16405f16afa01abf35b4bc837922d85cd9439255613c8efff4d.jar.tmp C:\Users\gustavo.rodriguezl\src\garodriguezlp\jbang-kotlin-bug\KotlinBug.kt error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\com\squareup\okio\okio-jvm\3.0.0\okio-jvm-3.0.0.jar error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk8\1.5.31\kotlin-stdlib-jdk8-1.5.31.jar error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk7\1.5.31\kotlin-stdlib-jdk7-1.5.31.jar error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-common\1.5.31\kotlin-stdlib-common-1.5.31.jar error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib\1.6.20\kotlin-stdlib-1.6.20.jar error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\annotations\13.0\annotations-13.0.jar error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\json\json\20220320\json-20220320.jar [jbang] Deleting folder C:\Users\gustavo.rodriguezl\.jbang\cache\jars\KotlinBug.kt.b1dcd226c361b16405f16afa01abf35b4bc837922d85cd9439255613c8efff4d.jar.tmp [jbang] [ERROR] Error during compile dev.jbang.cli.ExitException: Error during compile at dev.jbang.source.builders.BaseBuilder.runCompiler(BaseBuilder.java:191) at dev.jbang.source.builders.BaseBuilder.runCompiler(BaseBuilder.java:179) at dev.jbang.source.builders.BaseBuilder.compile(BaseBuilder.java:152) at dev.jbang.source.builders.BaseBuilder.build(BaseBuilder.java:105) at dev.jbang.source.builders.BaseBuilder.build(BaseBuilder.java:30) at dev.jbang.cli.Run.prepareArtifacts(Run.java:133) at dev.jbang.cli.Run.doCall(Run.java:104) at dev.jbang.cli.BaseCommand.call(BaseCommand.java:136) at dev.jbang.cli.BaseCommand.call(BaseCommand.java:20) at picocli.CommandLine.executeUserObject(CommandLine.java:1953) at picocli.CommandLine.access$1300(CommandLine.java:145) at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2358) at picocli.CommandLine$RunLast.handle(CommandLine.java:2352) at dev.jbang.cli.JBang$3.handle(JBang.java:151) at dev.jbang.cli.JBang$3.handle(JBang.java:146) at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2179) at picocli.CommandLine$RunLast.execute(CommandLine.java:2316) at picocli.CommandLine.execute(CommandLine.java:2078) at dev.jbang.Main.main(Main.java:14) [jbang] If you believe this a bug in jbang, open an issue at https://github.com/jbangdev/jbang/issues ```
And this is what I see if I dig into and tweek the `kotlinc.bat` file - Actually, I'm talking about modifying the [`kotlinc.bat` file line 69](https://github.com/JetBrains/kotlin/blob/master/compiler/cli/bin/kotlinc.bat#L69) by puttig before and `echo` command to see what is the value of the `KOTLIN_OPTS` variable. - Like this: ```bat echo "KOTLIN_OPTS=%KOTLIN_OPTS%" ``` ```ps1 ❯ jbang --verbose run .\KotlinBug.kt [jbang] jbang version 0.98.0 [jbang] Build required as C:\Users\gustavo.rodriguezl\.jbang\cache\jars\KotlinBug.kt.b1dcd226c361b16405f16afa01abf35b4bc837922d85cd9439255613c8efff4d.jar not readable or not found. [jbang] Repositories: mavencentral=https://repo1.maven.org/maven2/ [jbang] Building jar... [jbang] Compile: C:\Users\gustavo.rodriguezl\.jbang\cache\kotlincs\1.6.10\kotlinc\bin\kotlinc.bat -classpath C:\Users\gustavo.rodriguezl\.m2\repository\com\squareup\okhttp3\okhttp\4.10.0\okhttp-4.10.0.jar;C:\Users\gustavo.rodriguezl\.m2\repository\com\squareup\okio\okio-jvm\3.0.0\okio-jvm-3.0.0.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk8\1.5.31\kotlin-stdlib-jdk8-1.5.31.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk7\1.5.31\kotlin-stdlib-jdk7-1.5.31.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-common\1.5.31\kotlin-stdlib-common-1.5.31.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib\1.6.20\kotlin-stdlib-1.6.20.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\annotations\13.0\annotations-13.0.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\json\json\20220320\json-20220320.jar -d C:\Users\gustavo.rodriguezl\.jbang\cache\jars\KotlinBug.kt.b1dcd226c361b16405f16afa01abf35b4bc837922d85cd9439255613c8efff4d.jar.tmp C:\Users\gustavo.rodriguezl\src\garodriguezlp\jbang-kotlin-bug\KotlinBug.kt error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\com\squareup\okio\okio-jvm\3.0.0\okio-jvm-3.0.0.jar error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk8\1.5.31\kotlin-stdlib-jdk8-1.5.31.jar error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk7\1.5.31\kotlin-stdlib-jdk7-1.5.31.jar error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-common\1.5.31\kotlin-stdlib-common-1.5.31.jar error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib\1.6.20\kotlin-stdlib-1.6.20.jar error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\annotations\13.0\annotations-13.0.jar error: source entry is not a Kotlin file: C:\Users\gustavo.rodriguezl\.m2\repository\org\json\json\20220320\json-20220320.jar [jbang] Deleting folder C:\Users\gustavo.rodriguezl\.jbang\cache\jars\KotlinBug.kt.b1dcd226c361b16405f16afa01abf35b4bc837922d85cd9439255613c8efff4d.jar.tmp [jbang] [ERROR] Error during compile dev.jbang.cli.ExitException: Error during compile at dev.jbang.source.builders.BaseBuilder.runCompiler(BaseBuilder.java:191) at dev.jbang.source.builders.BaseBuilder.runCompiler(BaseBuilder.java:179) at dev.jbang.source.builders.BaseBuilder.compile(BaseBuilder.java:152) at dev.jbang.source.builders.BaseBuilder.build(BaseBuilder.java:105) at dev.jbang.source.builders.BaseBuilder.build(BaseBuilder.java:30) at dev.jbang.cli.Run.prepareArtifacts(Run.java:133) at dev.jbang.cli.Run.doCall(Run.java:104) at dev.jbang.cli.BaseCommand.call(BaseCommand.java:136) at dev.jbang.cli.BaseCommand.call(BaseCommand.java:20) at picocli.CommandLine.executeUserObject(CommandLine.java:1953) at picocli.CommandLine.access$1300(CommandLine.java:145) at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2358) at picocli.CommandLine$RunLast.handle(CommandLine.java:2352) at dev.jbang.cli.JBang$3.handle(JBang.java:151) at dev.jbang.cli.JBang$3.handle(JBang.java:146) at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2179) at picocli.CommandLine$RunLast.execute(CommandLine.java:2316) at picocli.CommandLine.execute(CommandLine.java:2078) at dev.jbang.Main.main(Main.java:14) [jbang] If you believe this a bug in jbang, open an issue at https://github.com/jbangdev/jbang/issues "KOTLIN_OPTS= "-classpath" "C:\Users\gustavo.rodriguezl\.m2\repository\com\squareup\okhttp3\okhttp\4.10.0\okhttp-4.10.0.jar" "C:\Users\gustavo.rodriguezl\.m2\repository\com\squareup\okio\okio-jvm\3.0.0\okio-jvm-3.0.0.jar" "C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk8\1.5.31\kotlin-stdlib-jdk8-1.5.31.jar" "C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk7\1.5.31\kotlin-stdlib-jdk7-1.5.31.jar" "C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-common\1.5.31\kotlin-stdlib-common-1.5.31.jar" "C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib\1.6.20\kotlin-stdlib-1.6.20.jar" "C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\annotations\13.0\annotations-13.0.jar" "C:\Users\gustavo.rodriguezl\.m2\repository\org\json\json\20220320\json-20220320.jar" "-d" "C:\Users\gustavo.rodriguezl\.jbang\cache\jars\KotlinBug.kt.b1dcd226c361b16405f16afa01abf35b4bc837922d85cd9439255613c8efff4d.jar.tmp" "C:\Users\gustavo.rodriguezl\src\garodriguezlp\jbang-kotlin-bug\KotlinBug.kt"" ``` - As you can see (in thisπŸ‘†last debug line), the classpath is not being passed correctly to the Kotlin compiler. The classpath is being passed as multiple arguments instead of a single argument separated by `;` semicolon.
garodriguezlp commented 2 years ago

@yeung66 I'm curious to know if you were able to run your Kotlin files after #1433, and what are your thoughts about this?

garodriguezlp commented 2 years ago

Hi @quintesse and @maxandersen, Any thoughts on this issue?

I even wonder if it's a jbang issue or a kotlin one. πŸ˜•

Thanks!

garodriguezlp commented 2 years ago

I'll leave here my research/workaround notes here (I hope it does not bother you guys):

I know this might be the worst workaround ever, but it probably could point us in some direction (let's see it as an inspiration πŸ˜…)

        if (!Util.isBlankString(path)) {
            optionList.addAll(Arrays.asList("-classpath", "\"" + path + "\""));
        }
[jbang] jbang version 0.98.0
[jbang] Build required as C:\Users\gustavo.rodriguezl\.jbang\cache\jars\KotlinBug.kt.b1dcd226c361b16405f16afa01abf35b4bc837922d85cd9439255613c8efff4d.jar not readable or not found.
[jbang] Downloading Kotlin 1.6.10. Be patient, this can take several minutes...
[jbang] Downloading https://github.com/JetBrains/kotlin/releases/download/v1.6.10/kotlin-compiler-1.6.10.zip
[jbang] Redirected to: https://objects.githubusercontent.com/github-production-release-asset-2e65be/3432266/321575a3-fab9-4e5a-9a47-dda6e8303e47?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220902%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220902T200716Z&X-Amz-Expires=300&X-Amz-Signature=d304d7881c7fb737da67e85a087ae5b916c980a8ee27dc007b65641568f9fe99&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=3432266&response-content-disposition=attachment%3B%20filename%3Dkotlin-compiler-1.6.10.zip&response-content-type=application%2Foctet-stream
[jbang] Downloaded file https://objects.githubusercontent.com/github-production-release-asset-2e65be/3432266/321575a3-fab9-4e5a-9a47-dda6e8303e47?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220902%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220902T200716Z&X-Amz-Expires=300&X-Amz-Signature=d304d7881c7fb737da67e85a087ae5b916c980a8ee27dc007b65641568f9fe99&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=3432266&response-content-disposition=attachment%3B%20filename%3Dkotlin-compiler-1.6.10.zip&response-content-type=application%2Foctet-stream
[jbang] Installing Kotlin 1.6.10...
[jbang] Unpacking to C:\Users\gustavo.rodriguezl\.jbang\cache\kotlincs\1.6.10
[jbang] Repositories: mavencentral=https://repo1.maven.org/maven2/
[jbang] Resolving dependencies...
[jbang] com.squareup.okhttp3:okhttp:jar:4.10.0
         org.json:json:jar:20220320
Done
[jbang] Dependencies resolved
[jbang] Building jar...
[jbang] Compile: C:\Users\gustavo.rodriguezl\.jbang\cache\kotlincs\1.6.10\kotlinc\bin\kotlinc.bat -classpath "C:\Users\gustavo.rodriguezl\.m2\repository\com\squareup\okhttp3\okhttp\4.10.0\okhttp-4.10.0.jar;C:\Users\gustavo.rodriguezl\.m2\repository\com\squareup\okio\okio-jvm\3.0.0\okio-jvm-3.0.0.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk8\1.5.31\kotlin-stdlib-jdk8-1.5.31.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk7\1.5.31\kotlin-stdlib-jdk7-1.5.31.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-common\1.5.31\kotlin-stdlib-common-1.5.31.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib\1.6.20\kotlin-stdlib-1.6.20.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\jetbrains\annotations\13.0\annotations-13.0.jar;C:\Users\gustavo.rodriguezl\.m2\repository\org\json\json\20220320\json-20220320.jar" -d C:\Users\gustavo.rodriguezl\.jbang\cache\jars\KotlinBug.kt.b1dcd226c361b16405f16afa01abf35b4bc837922d85cd9439255613c8efff4d.jar.tmp C:\Users\gustavo.rodriguezl\src\garodriguezlp\jbang-kotlin-bug\KotlinBug.kt
[jbang] System Java version detected as 11
[jbang] Deleting folder C:\Users\gustavo.rodriguezl\.jbang\cache\jars\KotlinBug.kt.b1dcd226c361b16405f16afa01abf35b4bc837922d85cd9439255613c8efff4d.jar.tmp
[jbang] Repositories: mavencentral=https://repo1.maven.org/maven2/
[jbang] System Java version matches requested version 11
maxandersen commented 2 years ago

Please run with jbang --verbose - then we should get a hint on what goes wrong.

garodriguezlp commented 2 years ago

@maxandersen I already did. πŸ˜…

See in the Additional context section

image

I'm pretty confident I've identified the root cause 🀞. In a few words: The classpath arg for the batch script needs to be wrapped in double quotes; otherwise, it'd be split by the ; semicolons

Also, please see my last https://github.com/jbangdev/jbang/issues/1447#issuecomment-1235864717

yeung66 commented 2 years ago
Arrays.asList("-classpath", "\"" + path + "\"")

I think you are right. Adding double quotes will solve the problem

maxandersen commented 2 years ago

This feels like a kotlinc.bat issue imo; its not ensuring parameters passed to it keeps quotes.

if kotlinc was natively parsing the input it would be fine.

maxandersen commented 2 years ago

Can you try out https://github.com/jbangdev/jbang/pull/1448 and see if things work well for kotlin as well as java still ?

maxandersen commented 2 years ago

note, I made the same fix just using escapeOSArgument which should do the right thing dependent on the OS we run on.

quintesse commented 2 years ago

This feels like a kotlinc.bat issue imo; its not ensuring parameters passed to it keeps quotes.

I have the same feeling, although in this case the argument doesn't even need quotes, there are no spaces and in Windows ; is not a special character, it's not a command separator like on Linux/Mac. So I'm actually a bit surprised that adding the quotes helps at all.

quintesse commented 2 years ago

Well it turns out I was wrong, basically any "special" character is treated as a separator, see for example: https://stackoverflow.com/a/53346096/2889926

garodriguezlp commented 2 years ago

Yeap! That's also what I understand from this source πŸ‘‡ image

quintesse commented 2 years ago

Can you try out #1448 and see if things work well for kotlin as well as java still ?

Wait, this is weird, it might very well work, but that command gets passed to Java's ProcessBuilder! That's why we're not doing any quoting because that code should supposedly take care of quoting itself!! So how come it doesn't do so?

maxandersen commented 2 years ago

It does but the .bat strips it.

quintesse commented 2 years ago

So just ran some tests with ProcessBuilder that runs a script that enumerates it's arguments. When run with:

{ "foo", "bar;baz" }

You get the following result:

* Windows Linux
0 foo bar;baz foo bar;baz
1 foo foo
2 bar bar;baz
3 baz

But when quoting the second argument like this:

{ "foo", "\"bar;baz\"" }

You get:

* Windows Linux
0 foo "bar;baz" foo "bar;baz"
1 foo foo
2 bar;baz "bar;baz"

As you can see the quotiung helps for Windows but on Linux the quotes have become part of the argument being passed. Which most likely messes up most commands on Linux (and Mac).

quintesse commented 2 years ago

Putting everything together, using:

{ "foo", "bar;baz", "bar baz", "\"bar;baz\"", "\"bar baz\"" }

Gives:

* Windows Linux
0 foo bar;baz "bar baz" "bar;baz" "bar baz" foo bar;baz bar baz "bar;baz" "bar baz"
1 foo foo
2 bar bar;baz
3 baz bar baz
4 bar baz "bar;baz"
5 bar;baz "bar baz"
6 bar baz

As you can see ProcessBuilder handles spaces in arguments just fine on all platforms, meaning it must be quoting them correctly. It almost seems like a bug in ProcessBuilder that it doesn't know how to handle non-space separators on Windows.

quintesse commented 2 years ago

I think the concept of a CommandBuffer might be nice solution, even if it's simply to take a list of arguments and turn them into

1) another list for consumption by ProcessBuilder 2) a string to be executed by the system's shell

Eg. something like:


class CommandBuffer {
   public CommandBuffer(List<String> args) {}
   public ProcessBuilder asProcessBuilder() {}
   public String asCommandLine() { /* returns result of one of the methods below depending on current system */ }
   public String asBashCommandLine() {}
   public String asCmdCommandLine() {}
   public String asPowershellCommandLine() {}
}
maxandersen commented 2 years ago

Great examination. No wonder windows users are hard to support with this level of inconsistency.

Do you wanna make a pr for commandBuffer ?

maxandersen commented 2 years ago

btw. are we in the situation that .bat/.cmd should be treated differently than executables on windows or is it the same?

quintesse commented 2 years ago

btw. are we in the situation that .bat/.cmd should be treated differently than executables on windows or is it the same?

I'm not sure, I wanted to test it but I didn't know how. I'd need something like an ECHO.EXE or anything where I can check exactly what arguments were passed (according to the command).

maxandersen commented 2 years ago

jbang --native echo.java or go run echo.go ? :)

quintesse commented 2 years ago

Dunno about Go, but I wouldn't trust a native echo.java to be using standard Windows ways of parsing command lines.

quintesse commented 2 years ago

With PR #1449 the KotlinBug.kt now compiles correctly and it doesn't seem to be causing any others problems.