ScoopInstaller / Extras

📦 The Extras bucket for Scoop.
https://scoop.sh
The Unlicense
1.8k stars 1.37k forks source link

[Bug]: ktlint fails to start with newer java versions (eg openjdk17) #10313

Open ghost opened 1 year ago

ghost commented 1 year ago

Prerequisites

Package Name

ktlint

Expected/Current Behaviour

I have the following relevant scoop packages installed:

openjdk17: 17.0.2-8 (latest version) ktlint: 0.48.2 (latest version)

When running ktlint (eg ktlint -F), I see the following stack trace:

$ ktlint -F
09:55:52.219 [main] INFO com.pinterest.ktlint.internal.KtlintCommandLine - Enable default patterns [**/*.kt, **/*.kts]
Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.NoClassDefFoundError: Could not initialize class org.jetbrains.kotlin.com.intellij.openapi.util.objectTree.ThrowableInterner
        at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:122)
        at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:191)
        at com.pinterest.ktlint.internal.KtlintCommandLine.parallel(KtlintCommandLine.kt:684)
        at com.pinterest.ktlint.internal.KtlintCommandLine.parallel$default(KtlintCommandLine.kt:642)
        at com.pinterest.ktlint.internal.KtlintCommandLine.lintFiles(KtlintCommandLine.kt:407)
        at com.pinterest.ktlint.internal.KtlintCommandLine.run(KtlintCommandLine.kt:332)
        at com.pinterest.ktlint.Main.main(Main.kt:33)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.jetbrains.kotlin.com.intellij.openapi.util.objectTree.ThrowableInterner
        at org.jetbrains.kotlin.com.intellij.util.exception.FrequentErrorLogger.report(FrequentErrorLogger.java:47)
        at org.jetbrains.kotlin.com.intellij.util.exception.FrequentErrorLogger.info(FrequentErrorLogger.java:43)
        at org.jetbrains.kotlin.com.intellij.psi.impl.DebugUtil.handleUnspecifiedTrace(DebugUtil.java:628)
[...]

This error is mentioned at https://github.com/pinterest/ktlint/issues/1195 and was fixed last year. It seems that all Java 16+ version cause this problem. The fix was done by calling java using --add-opens java.base/java.lang=ALL-UNNAMED. Unfortunately, scoop writes its own start script in /c/Users/me/scoop/shims/ktlint which doesn't contain such a fix:

$ cat /c/Users/me/scoop/shims/ktlint
#!/bin/sh
# C:\Users\me\scoop\apps\ktlint\current\ktlint.jar
java.exe -jar "C:\Users\me\scoop\apps\ktlint\current\ktlint.jar"  "$@"

Steps to Reproduce

$ scoop install openjdk17
$ java --version    # make sure java version is >= 16
$ scoop install ktlint
$ ktlint -F   # <- will directly show this error

Possible Solution

The following patch fixes the issue:

$ diff -u /c/Users/me/scoop/shims/ktlint_0.48.2orig /c/Users/me/scoop/shims/ktlint
--- /c/Users/me/scoop/shims/ktlint_0.48.2orig       2023-01-24 09:54:50.425983100 +0100
+++ /c/Users/me/scoop/shims/ktlint  2023-01-24 09:57:49.994818600 +0100
@@ -1,3 +1,3 @@
 #!/bin/sh
 # C:\Users\me\scoop\apps\ktlint\current\ktlint.jar
-java.exe -jar "C:\Users\me\scoop\apps\ktlint\current\ktlint.jar"  "$@"
\ No newline at end of file
+java.exe --add-opens java.base/java.lang=ALL-UNNAMED -jar "C:\Users\me\scoop\apps\ktlint\current\ktlint.jar"  "$@"
\ No newline at end of file

Same should be done with /c/Users/me/scoop/shims/ktlint.cmd.

This is the same solution as described in https://github.com/pinterest/ktlint/issues/1195.

Scoop and Buckets Version

$ scoop --version
Current Scoop version:
v0.3.1 - Released at 2022-11-15

'extras' bucket:
ad1b98ad1 (HEAD -> master, origin/master, origin/HEAD) nexttrace: Update to version 1.0.5

'java' bucket:
b1ef59e5 (HEAD -> master, origin/master, origin/HEAD) intellij-jbr17: Update to version 17.0.6-b779.1

'main' bucket:
ef02489fc (HEAD -> master) terragrunt: Update to version 0.42.6

Scoop Config

$ scoop config

last_update                       scoop_branch scoop_repo
-----------                       ------------ ----------
2023-01-24T09:54:05.4529253+01:00 master       https://github.com/ScoopInstaller/Scoop

PowerShell Version

using git bash

Additional Softwares

No response

Elkiwa commented 1 year ago
$ scoop install openjdk17
$ java --version    # make sure java version is >= 16
$ scoop install ktlint
$ ktlint -F   # <- will directly show this error

Followed the steps above though didn't find any error. Maybe the fixes are already upstream?

My config:

PS C:\dev> java --version
openjdk 17.0.2 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-86)
OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)
PS C:\dev> ktlint --version
1.0.0