ktlint: 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 @76628072
Expected Behavior
The task should tell me if there is a problem with the code style or not.
Observed Behavior
Crash:
> Task :ktlintFormat FAILED
17:02:10.989 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "baseline" id.
17:02:10.989 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "checkstyle" id.
17:02:10.990 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "json" id.
17:02:10.990 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "html" id.
17:02:10.990 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "plain" id.
17:02:10.990 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "sarif" id.
17:02:10.990 [main] DEBUG com.pinterest.ktlint.Main - Initializing "plain" reporter with {verbose=false, color=false, color_name=DARK_GRAY}
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:606)
at com.pinterest.ktlint.KtlintCommandLine.parallel$default(Main.kt:578)
at com.pinterest.ktlint.KtlintCommandLine.lintFiles(Main.kt:321)
at com.pinterest.ktlint.KtlintCommandLine.run(Main.kt:282)
at com.pinterest.ktlint.Main.main(Main.kt:76)
Caused by: java.lang.ExceptionInInitializerError
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)
at org.jetbrains.kotlin.com.intellij.psi.impl.DebugUtil.currentInvalidationTrace(DebugUtil.java:620)
at org.jetbrains.kotlin.com.intellij.psi.impl.DebugUtil.calcInvalidationTrace(DebugUtil.java:614)
at org.jetbrains.kotlin.com.intellij.psi.impl.DebugUtil.onInvalidated(DebugUtil.java:585)
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:147)
at org.jetbrains.kotlin.com.intellij.pom.impl.PomTransactionBase.run(PomTransactionBase.java:28)
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:142)
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.removeRange(CompositeElement.java:611)
at com.pinterest.ktlint.ruleset.standard.ImportOrderingRule.visit(ImportOrderingRule.kt:235)
at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:351)
at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:344)
at com.pinterest.ktlint.core.VisitorProvider$sequentialVisitor$1$1$1$1.invoke(VisitorProvider.kt:102)
at com.pinterest.ktlint.core.VisitorProvider$sequentialVisitor$1$1$1$1.invoke(VisitorProvider.kt:102)
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.VisitorProvider$sequentialVisitor$1.invoke(VisitorProvider.kt:102)
at com.pinterest.ktlint.core.VisitorProvider$sequentialVisitor$1.invoke(VisitorProvider.kt:94)
at com.pinterest.ktlint.core.KtLint.format(KtLint.kt:344)
at com.pinterest.ktlint.internal.FileUtilsKt.formatFile(FileUtils.kt:221)
at com.pinterest.ktlint.KtlintCommandLine.process(Main.kt:396)
at com.pinterest.ktlint.KtlintCommandLine.access$process(Main.kt:95)
at com.pinterest.ktlint.KtlintCommandLine$lintFiles$3.invoke$lambda-1(Main.kt:311)
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 @76628072
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:154)
at org.jetbrains.kotlin.com.intellij.util.ReflectionUtil.getDeclaredField(ReflectionUtil.java:279)
at org.jetbrains.kotlin.com.intellij.openapi.util.objectTree.ThrowableInterner.<clinit>(ThrowableInterner.java:88)
... 35 more
FAILURE: Build failed with an exception.
Steps to Reproduce
ktlint.gradle:
apply plugin: 'java'
repositories {
mavenCentral()
}
configurations {
ktlint
}
dependencies {
ktlint("com.pinterest:ktlint:0.44.0") {
attributes {
attribute(Bundling.BUNDLING_ATTRIBUTE, getObjects().named(Bundling, Bundling.EXTERNAL))
}
}
// additional 3rd party ruleset(s) can be specified here
// just add them to the classpath (e.g. ktlint 'groupId:artifactId:version') and
// ktlint will pick them up
}
task ktlint(type: JavaExec, group: "verification") {
description = "Check Kotlin code style."
classpath = configurations.ktlint
mainClass.set("com.pinterest.ktlint.Main")
args "-F", "**/src/**/*.kt", "!**/genMain/**", "!**/genTest/**"
// to generate report in checkstyle format prepend following args:
// "--reporter=plain", "--reporter=checkstyle,output=${buildDir}/ktlint.xml"
// to add a baseline to check against prepend following args:
// "--baseline=ktlint-baseline.xml"
// see https://github.com/pinterest/ktlint#usage for more
}
check.dependsOn ktlint
task ktlintFormat(type: JavaExec, group: "formatting") {
description = "Fix Kotlin code style deviations."
classpath = configurations.ktlint
mainClass.set("com.pinterest.ktlint.Main")
args "-F", "**/src/**/*.kt", "!**/genMain/**", "!**/genTest/**"
}
Your Environment
- Version of ktlint used: 0.44.0
- Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task):
- Version of Gradle used (if applicable): 7.4
- Operating System and version: Latest MacOS Monterey
- Link to your project (if it’s a public repository): https://github.com/ScottPierce/kotlin-html/tree/scott/Kotlin_1_6
- Java Version:
java --version
openjdk 17.0.2 2022-01-18 LTS
OpenJDK Runtime Environment Corretto-17.0.2.8.1 (build 17.0.2+8-LTS)
OpenJDK 64-Bit Server VM Corretto-17.0.2.8.1 (build 17.0.2+8-LTS, mixed mode, sharing)
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 8
- Comments: 28 (3 by maintainers)
Commits related to this issue
- Fix for ktlintFormat Upstream dependencies of ktlint need to be updated, in the meantime this is a workaround. See https://github.com/pinterest/ktlint/issues/1391 — committed to ministryofjustice/hmpps-approved-premises-api by brownbearsoftware a year ago
- ktlint --add-opens=java.base/java.lang=ALL-UNNAMED The recommendation from the corresponding ktlint issue: https://github.com/pinterest/ktlint/issues/1391#issuecomment-1336221448 Bug: 273619707 Tes... — committed to androidx/androidx by mathjeff a year ago
- Add workaround for KtLint incompatibility with JDK 17 https://github.com/pinterest/ktlint/issues/1943 https://github.com/pinterest/ktlint/issues/1391 — committed to gtk-kn/gtk-kn by leinardi a year ago
- Simpify arguments to Java/Kotlin formatters. pre-commit should already auto-find the right files and pass them to these hooks, and not passing them should I *think* mean it's harder for new implement... — committed to bowtie-json-schema/bowtie by Julian 10 months ago
Thanks @paul-dingemans, @cwilper Temporarily fixed by adding the following line to my ktlinFormat definition
Sorry, I was just running the ktlint CLI version which worked fine. Tnx for pointing once more to
./gradlew ktlintFormat
which does reproduce the problem. Based on @cwilper’s comment, I got it working by changing the definition of thektlintFormat
task as follows:For maven users it is possible to get around this by adding a file
.mvn/jvm.config
with content--add-opens java.base/java.lang=ALL-UNNAMED
, e.g.:Credit: https://github.com/gantsign/ktlint-maven-plugin/issues/392#issuecomment-1114657552
I am not sure whether OP’s original problem is solved, but I am loosing track on the status of this issue.
The problem is caused by newer Java version restricting access to certain libraries. We have to wait until libraries we use, do solve this problem so that it does not propagate via KtLint. Providing the
--add-opens=java.base/java.lang=ALL-UNNAMED
in one of ways suggested before is the way to go for now.@paul-dingemans I have created a sample repository with the same issue here.
I am facing this issue with
ktlintFormat works when using with java 11 (though gives a warning as mentioned here: https://github.com/pinterest/ktlint/issues/1618), but if there is some code formatting issue, then ktlintFormat fails with the above version, and may be other java versions as well.
Gradle 7.4
Build time: 2022-02-08 09:58:38 UTC Revision: f0d9291c04b90b59445041eaa75b2ee744162586
Kotlin: 1.5.31 Groovy: 3.0.9 Ant: Apache Ant™ version 1.10.11 compiled on July 10 2021 JVM: 17.0.4 (Amazon.com Inc. 17.0.4+8-LTS) OS: Mac OS X 12.5.1 x86_64
try this, this work well for me. I alse encounter this problem in jdk 17.
- JVM_OPTS= --add-exports=java.naming/com.sun.jndi.ldap=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.security=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.management/javax.management=ALL-UNNAMED --add-opens=java.naming/javax.naming=ALL-UNNAMED
Hello, here’s another data point on this: I got the same error when trying to run ktlint 0.45.2 from Java 17 in gradle. I eventually got around it by setting these jvmArgs in my JavaExec task.
Fwiw, I also did not run into the issue originally unless I passed the
-F
argument.@ScottPierce Tnx for detailed description. I will have a look at it again.