ktLintFormat fails with an error: Process 'command '/Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home/bin/java'' (macOS w/ Apple M1 chip)
Installed ktLint
on my Android Studio project but when running I get the following error:
Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.ExceptionInInitializerError
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.KtlintCommandLine.parallel(Main.kt:576)
at com.pinterest.ktlint.KtlintCommandLine.parallel$default(Main.kt:548)
at com.pinterest.ktlint.KtlintCommandLine.lintFiles(Main.kt:298)
at com.pinterest.ktlint.KtlintCommandLine.run(Main.kt:262)
at com.pinterest.ktlint.Main.main(Main.kt:70)
Caused by: java.lang.ExceptionInInitializerError
at org.jetbrains.kotlin.com.intellij.util.exception.FrequentErrorLogger.report(FrequentErrorLogger.java:46)
at org.jetbrains.kotlin.com.intellij.util.exception.FrequentErrorLogger.info(FrequentErrorLogger.java:42)
at org.jetbrains.kotlin.com.intellij.psi.impl.DebugUtil.handleUnspecifiedTrace(DebugUtil.java:626)
at org.jetbrains.kotlin.com.intellij.psi.impl.DebugUtil.currentInvalidationTrace(DebugUtil.java:618)
at org.jetbrains.kotlin.com.intellij.psi.impl.DebugUtil.calcInvalidationTrace(DebugUtil.java:612)
at org.jetbrains.kotlin.com.intellij.psi.impl.DebugUtil.onInvalidated(DebugUtil.java:583)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.TreeElement.onInvalidated(TreeElement.java:226)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.TreeElement.rawRemoveUpToWithoutNotifications(TreeElement.java:388)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.TreeElement.rawRemoveUpTo(TreeElement.java:342)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement.remove(CompositeElement.java:775)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement.lambda$removeChildrenInner$4(CompositeElement.java:797)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.ChangeUtil$1.runInner(ChangeUtil.java:149)
at org.jetbrains.kotlin.com.intellij.pom.impl.PomTransactionBase.run(PomTransactionBase.java:46)
at com.pinterest.ktlint.core.internal.FormatPomModel.runTransaction(KotlinPsiFileFactory.kt:121)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.ChangeUtil.prepareAndRunChangeAction(ChangeUtil.java:143)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement.removeChildrenInner(CompositeElement.java:796)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement.replaceChild(CompositeElement.java:623)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.codeStyle.CodeEditUtil.replaceChild(CodeEditUtil.java:162)
at org.jetbrains.kotlin.com.intellij.extapi.psi.ASTDelegatePsiElement.replaceChildInternal(ASTDelegatePsiElement.java:385)
at org.jetbrains.kotlin.com.intellij.extapi.psi.ASTDelegatePsiElement.replace(ASTDelegatePsiElement.java:375)
at org.jetbrains.kotlin.psi.KtExpressionImplStub.rawReplace(KtExpressionImplStub.java:48)
at org.jetbrains.kotlin.psi.KtExpressionImpl$Companion.replaceExpression(KtExpressionImpl.kt:53)
at org.jetbrains.kotlin.psi.KtExpressionImplStub.replace(KtExpressionImplStub.java:43)
at com.pinterest.ktlint.ruleset.standard.StringTemplateRule.visit(StringTemplateRule.kt:49)
at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:389)
at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:382)
at com.pinterest.ktlint.core.KtLint$visitor$2$2.invoke(KtLint.kt:313)
at com.pinterest.ktlint.core.KtLint$visitor$2$2.invoke(KtLint.kt:312)
at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:229)
at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:230)
at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:230)
at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:230)
at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:230)
at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:230)
at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:230)
at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:230)
at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:230)
at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:230)
at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:230)
at com.pinterest.ktlint.core.KtLint$visitor$2.invoke(KtLint.kt:312)
at com.pinterest.ktlint.core.KtLint$visitor$2.invoke(KtLint.kt:300)
at com.pinterest.ktlint.core.KtLint.format(KtLint.kt:382)
at com.pinterest.ktlint.internal.FileUtilsKt.formatFile(FileUtils.kt:202)
at com.pinterest.ktlint.KtlintCommandLine.process(Main.kt:366)
at com.pinterest.ktlint.KtlintCommandLine.access$process(Main.kt:89)
at com.pinterest.ktlint.KtlintCommandLine$lintFiles$3.invoke$lambda-0(Main.kt:289)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private transient java.lang.Object java.lang.Throwable.backtrace accessible: module java.base does not "opens java.lang" to unnamed module @fe48b45
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
at org.jetbrains.kotlin.com.intellij.util.ReflectionUtil.findFieldInHierarchy(ReflectionUtil.java:153)
at org.jetbrains.kotlin.com.intellij.util.ReflectionUtil.getDeclaredField(ReflectionUtil.java:278)
at org.jetbrains.kotlin.com.intellij.openapi.util.objectTree.ThrowableInterner.<clinit>(ThrowableInterner.java:81)
... 50 more
> Task :app:ktlintFormat FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:ktlintFormat'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
I'v found some places talking about a wrong version of Java installed to satisfy ktLint, but it's from last year and I'mm already using a newer version of Java the what was suggested. F.eks. this thread: https://githubmemory.com/repo/pinterest/ktlint/issues/1227
Here is my ktLint.gradle
file which is quite standard:
configurations {
ktlint
}
dependencies {
ktlint "com.pinterest:ktlint:0.43.2"
}
task ktlint(type: JavaExec, group: "verification") {
description = "Check Kotlin code style."
classpath = configurations.ktlint
main = "com.pinterest.ktlint.Main"
ignoreExitValue = true
// create html report for CI and plain report for terminal report
args "--android", "--color", "--reporter=plain", "--reporter=checkstyle,output=${buildDir}/reports/ktlint-results.xml", "src/**/*.kt"
}
task ktlintFormat(type: JavaExec, group: "formatting") {
description = "Fix Kotlin code style deviations."
classpath = configurations.ktlint
main = "com.pinterest.ktlint.Main"
args "-F", "src/**/*.kt"
jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
}
And the Java version:
java version "17.0.1" 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)
Hardware:
macOS Monterey 12.0.1
Apple M1 chip
Gradle 7.3.3
Configure your ktlintFormat
task to include jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
, works for me with with JDK 17.0.1.
Source: https://github.com/pinterest/ktlint/issues/1195