Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 #1391

Closed
ScottPierce opened this issue Mar 2, 2022 · 28 comments

Comments

@ScottPierce
Copy link

ScottPierce commented Mar 2, 2022

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)
@ScottPierce ScottPierce changed the title Build 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 Mar 2, 2022
@ScottPierce ScottPierce changed the title 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 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 Mar 2, 2022
@paul-dingemans
Copy link
Collaborator

I can not reproduce your problem when running with CLI on java 17.0.2:

$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
$ java17
openjdk version "17.0.2" 2022-01-18 LTS
OpenJDK Runtime Environment Zulu17.32+13-CA (build 17.0.2+8-LTS)
OpenJDK 64-Bit Server VM Zulu17.32+13-CA (build 17.0.2+8-LTS, mixed mode, sharing)
$ ktlint -F "**/src/**/*.kt" "!**/genMain/**" "!**/genTest/**"
ktlint -F "**/src/**/*.kt" "*/genMain/**" "*/genTest/**"
20:11:41.174 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "baseline" id.
20:11:41.175 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "checkstyle" id.
20:11:41.175 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "json" id.
20:11:41.175 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "html" id.
20:11:41.175 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "plain" id.
20:11:41.175 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "sarif" id.
20:11:41.176 [main] DEBUG com.pinterest.ktlint.Main - Initializing "plain" reporter with {verbose=false, color=false, color_name=DARK_GRAY}
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.jetbrains.kotlin.com.intellij.util.ReflectionUtil (file:/usr/local/Cellar/ktlint/0.44.0/libexec/ktlint) to field java.lang.Throwable.backtrace
WARNING: Please consider reporting this to the maintainers of org.jetbrains.kotlin.com.intellij.util.ReflectionUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
./ktlint-test-projects-external/kotlin-html/kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/element/ColgroupDsl.kt:28:11: Trailing space(s)
./ktlint-test-projects-external/kotlin-html/kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/element/ColgroupDsl.kt:42:9: Trailing space(s)
./ktlint-test-projects-external/kotlin-html/kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/element/ColgroupDsl.kt:64:16: Trailing space(s)
./ktlint-test-projects-external/kotlin-html/kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/element/ColgroupDsl.kt:75:26: Trailing space(s)
./ktlint-test-projects-external/kotlin-html/kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/element/StrongDsl.kt:27:9: Trailing space(s)
./ktlint-test-projects-external/kotlin-html/kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/element/StrongDsl.kt:41:9: Trailing space(s)
./ktlint-test-projects-external/kotlin-html/kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/element/StrongDsl.kt:63:23: Trailing space(s)
./ktlint-test-projects-external/kotlin-html/kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/element/StrongDsl.kt:75:7: Trailing space(s)
./ktlint-test-projects-external/kotlin-html/kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/TextDecorationLineStyles.kt:19:9: Trailing space(s)
./ktlint-test-projects-external/kotlin-html/kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/TextDecorationLineStyles.kt:37:6: Trailing space(s)
./ktlint-test-projects-external/kotlin-html/kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/BackgroundAttachmentStyles.kt:23:9: Trailing space(s)
./ktlint-test-projects-external/kotlin-html/kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/TransitionTimingFunctionStyles.kt:15:9: Trailing space(s)
./ktlint-test-projects-external/kotlin-html/kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/TransitionTimingFunctionStyles.kt:21:6: Trailing space(s)
./ktlint-test-projects-external/kotlin-html/kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/TransitionTimingFunctionStyles.kt:26:20: Trailing space(s)
20:11:43.407 [main] DEBUG com.pinterest.ktlint.Main - 2415ms / 331 file(s) / 14 error(s)
$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   kotlin-html-generator/src/main/kotlin/dev/scottpierce/html/generate/GenerateMain.kt
	modified:   kotlin-html-generator/src/main/kotlin/dev/scottpierce/html/generate/model/Constants.kt
	modified:   kotlin-html-generator/src/main/kotlin/dev/scottpierce/html/generate/model/GeneratedStyleProperty.kt
	modified:   kotlin-html-generator/src/main/kotlin/dev/scottpierce/html/generate/task/GenerateElementDslTask.kt
	modified:   kotlin-html-generator/src/main/kotlin/dev/scottpierce/html/generate/task/GenerateElementTestsTask.kt
	modified:   kotlin-html-generator/src/main/kotlin/dev/scottpierce/html/generate/task/GenerateStyleBuilderDslTask.kt
	modified:   kotlin-html-generator/src/main/kotlin/dev/scottpierce/html/generate/task/GenerateStylePropertiesTask.kt
	modified:   kotlin-html-style-builder/src/genMain/kotlin/dev/scottpierce/html/writer/element/AStyleBuilderDsl.kt
	modified:   kotlin-html-style-builder/src/genMain/kotlin/dev/scottpierce/html/writer/element/ButtonStyleBuilderDsl.kt
	modified:   kotlin-html-style-builder/src/genMain/kotlin/dev/scottpierce/html/writer/element/InputStyleBuilderDsl.kt
	modified:   kotlin-html-style-builder/src/genMain/kotlin/dev/scottpierce/html/writer/element/TdStyleBuilderDsl.kt
	modified:   kotlin-html-style-builder/src/genMain/kotlin/dev/scottpierce/html/writer/element/ThStyleBuilderDsl.kt
	modified:   kotlin-html-style-builder/src/genMain/kotlin/dev/scottpierce/html/writer/element/VideoStyleBuilderDsl.kt
	modified:   kotlin-html-writer/src/commonMain/kotlin/dev/scottpierce/html/writer/element/ElementUtil.kt
	modified:   kotlin-html-writer/src/commonMain/kotlin/dev/scottpierce/html/writer/style/BoxShadow.kt
	modified:   kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/element/ColgroupDsl.kt
	modified:   kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/element/InputDsl.kt
	modified:   kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/element/StrongDsl.kt
	modified:   kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/BackgroundAttachmentStyles.kt
	modified:   kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/BackgroundImageStyles.kt
	modified:   kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/BorderRadiusStyles.kt
	modified:   kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/BoxShadowStyles.kt
	modified:   kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/ListStyleStyles.kt
	modified:   kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/OutlineStyles.kt
	modified:   kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/TextDecorationLineStyles.kt
	modified:   kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/TextDecorationStyles.kt
	modified:   kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/TransitionPropertyStyles.kt
	modified:   kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/TransitionStyles.kt
	modified:   kotlin-html-writer/src/genMain/kotlin/dev/scottpierce/html/writer/style/TransitionTimingFunctionStyles.kt
	modified:   kotlin-html-writer/src/jvmMain/kotlin/dev/scottpierce/html/FileCache.kt
	modified:   kotlin-html-writer/src/jvmTest/kotlin/dev/scottpierce/html/writer/element/InlineFileTest.kt

no changes added to commit (use "git add" and/or "git commit -a")

I do have problems when trying to run ./gradlew ktlintformat but they seem different then yours:

$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
$ java17
openjdk version "17.0.2" 2022-01-18 LTS
OpenJDK Runtime Environment Zulu17.32+13-CA (build 17.0.2+8-LTS)
OpenJDK 64-Bit Server VM Zulu17.32+13-CA (build 17.0.2+8-LTS, mixed mode, sharing)
$ ./gradlew ktlintformat
Starting a Gradle Daemon, 2 incompatible Daemons could not be reused, use --status for details

FAILURE: Build failed with an exception.

* What went wrong:
Unable to start the daemon process.
This problem might be caused by incorrect configuration of the daemon.
For example, an unrecognized jvm option is used.
Please refer to the User Manual chapter on the daemon at https://docs.gradle.org/6.2.2/userguide/gradle_daemon.html
Process command line: /Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home/bin/java -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.invoke=ALL-UNNAMED --add-opens java.prefs/java.util.prefs=ALL-UNNAMED -Xms1g -Xmx3g -Dfile.encoding=UTF-8 -Duser.country=NL -Duser.language=en -Duser.variant -cp /Users/pdingemans/.gradle/wrapper/dists/gradle-6.2.2-all/2k4hz1yffuhb6ob9prdg10tx/gradle-6.2.2/lib/gradle-launcher-6.2.2.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 6.2.2
Please read the following process output to find out more:
-----------------------
Unrecognized VM option 'MaxPermSize=512m'
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Can you please try to run CLI in same way as I did and report back? Also try to narrow down the problem to a specific file.

@ScottPierce
Copy link
Author

ScottPierce commented Mar 3, 2022

@paul-dingemans Looks like you might be running it on the main branch. Can you try the scott/Kotlin_1_6 branch, where I removed the gradle property that's causing your error.

@ScottPierce
Copy link
Author

ScottPierce commented Mar 3, 2022

So the above error is what I get on my above m1 laptop.

I started getting other issues on JDK 17 on circleci:
https://app.circleci.com/pipelines/github/ScottPierce/kotlin-html/509/workflows/bce512b5-1d86-4440-a939-0146e87ac5f4/jobs/1538

I was able to resolve this by moving to JDK 11:
https://app.circleci.com/pipelines/github/ScottPierce/kotlin-html/511/workflows/07169170-bd22-40e3-b9a0-d2ac462f2db0/jobs/1545

@paul-dingemans
Copy link
Collaborator

Indeed I was running on main instead of Kotlin_1_6 branch. I can still not reproduce your problem:

.../kotlin-html (Kotlin_1_6) $ git  status
On branch Kotlin_1_6
Your branch is up to date with 'origin/scott/Kotlin_1_6'.

nothing to commit, working tree clean
.../kotlin-html (Kotlin_1_6) $ java17
openjdk version "17.0.2" 2022-01-18 LTS
OpenJDK Runtime Environment Zulu17.32+13-CA (build 17.0.2+8-LTS)
OpenJDK 64-Bit Server VM Zulu17.32+13-CA (build 17.0.2+8-LTS, mixed mode, sharing)
.../kotlin-html (Kotlin_1_6) $ ./gradlew ktlint

> Configure project :benchmark
Kotlin Multiplatform Projects are an Alpha feature. See: https://kotlinlang.org/docs/reference/evolution/components-stability.html. To hide this message, add 'kotlin.mpp.stability.nowarn=true' to the Gradle properties.

Project property 'kotlin.parallel.tasks.in.project' is deprecated.
By default it depends on Gradle parallel project execution option value.

> Configure project :kotlin-html-writer
Warning: Kotlin language settings function 'useExperimentalAnnotation' is deprecated and will be removed in next major releases. Please, use 'optIn' instead.


> Task :ktlint
21:16:38.104 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "baseline" id.
21:16:38.105 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "checkstyle" id.
21:16:38.105 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "json" id.
21:16:38.105 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "html" id.
21:16:38.105 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "plain" id.
21:16:38.105 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "sarif" id.
21:16:38.106 [main] DEBUG com.pinterest.ktlint.Main - Initializing "plain" reporter with {verbose=false, color=false, color_name=DARK_GRAY}
21:16:39.098 [main] DEBUG com.pinterest.ktlint.Main - 1154ms / 87 file(s) / 0 error(s)

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.3.3/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 2s
7 actionable tasks: 1 executed, 6 up-to-date
.../kotlin-html (Kotlin_1_6) $

Can you repeat steps above on your CLI?

@ScottPierce
Copy link
Author

@paul-dingemans You aren't getting any errors because I fixed all the style issues on the branch (It's an active PR). Try going back to this specific commit and trying again - https://app.circleci.com/pipelines/github/ScottPierce/kotlin-html/509/workflows/bce512b5-1d86-4440-a939-0146e87ac5f4/jobs/1538

@paul-dingemans
Copy link
Collaborator

I hope that you understand that it is hard to investigate problems against a "living" branch. If you want me to dig in this problem, please provide a reproducable example in this issue. Until then, closing it without further investigation.

@ScottPierce
Copy link
Author

@paul-dingemans Sorry, I thought I was providing everything you needed with the above link. It gives you the git commit hash, with a fully reproducible log from a docker run on CircleCI. I'll make you a branch from that commit hash for convenience.

Link to CI Run
Git Commit Hash: 0b54a30635c7c7896fb789e9226e0bf4ac0dae67
Branch that I made for you from the commit hash: scott/ReproKtlintBug

If you can't reproduce the log locally for some reason, you should be able to definitely reproduce it in docker. My CI is being built using the following docker image: cimg/openjdk:17.0-browsers

Please let me know if you need anything else.

@eygraber
Copy link
Contributor

eygraber commented Mar 15, 2022

I'm using ktlint's KotlinPsiFileFactory code in one of my scripts and I started seeing this issue as well. I had it working recently with Java 17, and I'm not sure what's changed since then that would cause this issue.

Running the code with Java 11 prints:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.jetbrains.kotlin.com.intellij.util.ReflectionUtil (file:/home/eli/.m2/repository/org/jetbrains/kotlin/kotlin-compiler-embeddable/1.6.10/kotlin-compiler-embeddable-1.6.10.jar) to field java.lang.Throwable.backtrace
WARNING: Please consider reporting this to the maintainers of org.jetbrains.kotlin.com.intellij.util.ReflectionUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

so maybe this is a Kotlin issue?

EDIT: Filed https://youtrack.jetbrains.com/issue/KT-51619

@paul-dingemans
Copy link
Collaborator

@paul-dingemans Sorry, I thought I was providing everything you needed with the above link. It gives you the git commit hash, with a fully reproducible log from a docker run on CircleCI. I'll make you a branch from that commit hash for convenience.

Link to CI Run Git Commit Hash: 0b54a30635c7c7896fb789e9226e0bf4ac0dae67 Branch that I made for you from the commit hash: scott/ReproKtlintBug

If you can't reproduce the log locally for some reason, you should be able to definitely reproduce it in docker. My CI is being built using the following docker image: cimg/openjdk:17.0-browsers

Please let me know if you need anything else.

@ScottPierce Sorry that I was not clear. The problem that I have regaing this issue, is that you expect me to check out a entire repo in which I have to get digging for a problem. Normally that is not always a problem. But this was with a living branch. I have looked multiple times at this issue and failed for various reasons (including own mistakes). This all was not needed if a "code sample" that reproduces the problem was added to the issue. So I am happy to assist, but I do ask more preparations from your side to narrow the problem down to a specific file or preferably just a code section.

@paul-dingemans
Copy link
Collaborator

@eygraber

I'm using ktlint's KotlinPsiFileFactory code in one of my scripts and I started seeing this issue as well. I had it working recently with Java 17, and I'm not sure what's changed since then that would cause this issue.

Running the code with Java 11 prints:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.jetbrains.kotlin.com.intellij.util.ReflectionUtil (file:/home/eli/.m2/repository/org/jetbrains/kotlin/kotlin-compiler-embeddable/1.6.10/kotlin-compiler-embeddable-1.6.10.jar) to field java.lang.Throwable.backtrace
WARNING: Please consider reporting this to the maintainers of org.jetbrains.kotlin.com.intellij.util.ReflectionUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

so maybe this is a Kotlin issue?

EDIT: Filed https://youtrack.jetbrains.com/issue/KT-51619

KotlinPsiFileFactory

Ktlint just provides access to org.jetbrains.kotlin.com.intellij.psi.PsiFileFactory. This just looks like a java 11+ issue to me. Please try to reproduce with Java 11 and/or provide a code sample (in a new issue!) which reproduces the problem.

@eygraber
Copy link
Contributor

@paul-dingemans my point was that I'm experiencing the same issue that @ScottPierce is experiencing, but I'm not actually using ktlint (I just copied the KotlinPsiFileFactory source into my project).

When running with Java 17 I get the same crash that Scott posted, and when running with Java 11, I get the same warning Scott gets when running with Java 11 (https://app.circleci.com/pipelines/github/ScottPierce/kotlin-html/511/workflows/07169170-bd22-40e3-b9a0-d2ac462f2db0/jobs/1545).

@ScottPierce
Copy link
Author

ScottPierce commented Mar 15, 2022

This all was not needed if a "code sample" that reproduces the problem was added to the issue.

So as things stand, any time there is a ktlint error, I reproduce the issue. If the style for the project .

This all was not needed if a "code sample" that reproduces the problem was added to the issue.

Sure - I'll do my best! Sorry, I think this is a case of I knew something that I didn't communicate very well, which is that this seems to be reproducible with my ktlint configuration (a single file for my whole project that I pasted in the root issue, located in gradle/ktlint.gradle), and then Java 17. If there are no style issues then everything is fine, however if there is a style issue then I get some variant of java.util.concurrent.ExecutionException.

i.e. I did the following:

  1. Create a default Android sample application
  2. Paste the above given ktlint file. I had to remove the repositories block because of the new way of Gradle managing repositories
apply plugin: 'java'

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/**"
}
  1. in the root build.gradle, add the line apply from: rootProject.file("gradle/ktlint.gradle"). You will also have to delete the clean function the sample adds by default.
  2. Run ./gradlew ktlint or ./gradlew ktlintFormat
  3. Get the following exception:
./gradlew ktlint

> Task :ktlint FAILED
17:39:01.027 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "baseline" id.
17:39:01.028 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "checkstyle" id.
17:39:01.028 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "json" id.
17:39:01.028 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "html" id.
17:39:01.028 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "plain" id.
17:39:01.028 [main] DEBUG com.pinterest.ktlint.Main - Discovered reporter with "sarif" id.
17:39:01.028 [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.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.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.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)
        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)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':ktlint'.
> Process 'command '/Users/scottpierce/.sdkman/candidates/java/17.0.2.8.1-amzn/bin/java'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s
1 actionable task: 1 executed

Here is my java version:

java -version
openjdk version "17.0.2" 2022-01-18 LTS
OpenJDK Runtime Environment Corretto-17.0.2.8.1 (build 17.0.2+8-LTS)

If you can't reproduce it from this here are my thoughts:

  1. You have the wrong version of Java installed
  2. This issue is related to Rosetta translation for M1 macs. I'm running an M1 Max 16" MBP, and I'm using an x86 JVM (I think? It's really hard to tell, but I think amazon only provides x86 JVMs)

Attached is my project
MyApplication.zip
:

@paul-dingemans
Copy link
Collaborator

@ScottPierce Tnx for detailed description. I will have a look at it again.

@ZacSweers
Copy link

This is a configuration cache bug where gradle tries to serialize an exception during caching. Disable it and rerun to show the original issue, gradle should just not try to serialize exceptions thrown like this

@PMacho
Copy link

PMacho commented Apr 4, 2022

This is a configuration cache bug where gradle tries to serialize an exception during caching. Disable it and rerun to show the original issue, gradle should just not try to serialize exceptions thrown like this

Would you tell us, which option you mean, and how we disable it?

@cwilper
Copy link

cwilper commented Apr 7, 2022

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.

  jvmArgs '--add-opens' 'java.base/java.lang=ALL-UNNAMED'

Fwiw, I also did not run into the issue originally unless I passed the -F argument.

@buttercannfly
Copy link

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

@fzyzcjy
Copy link

fzyzcjy commented Aug 4, 2022

Same issues here. Any updates?

@josephlbarnett
Copy link

for what its worth we've worked around this by using the antrun maven plugin instead of the ktlint maven plugin: https://github.com/trib3/leakycauldron/blob/cabaf522652be90550e4d0f8d7ccde50335116d6/parent-pom/pom.xml#L1472-L1498 -- that way the rest of the build still runs without any --add-opens, but ktlint runs with the --add-opens workaround.

We also appear to see this on 0.46.1, but things appear to work without any workarounds on 0.45.2, at least in our codebase (even with changing source code to force ktlint to reformat on execution)?

@ashish12
Copy link

@paul-dingemans I have created a sample repository with the same issue here.

I am facing this issue with

java --version
openjdk 17.0.4 2022-07-19 LTS
OpenJDK Runtime Environment Corretto-17.0.4.8.1 (build 17.0.4+8-LTS)
OpenJDK 64-Bit Server VM Corretto-17.0.4.8.1 (build 17.0.4+8-LTS, mixed mode, sharing)

ktlintFormat works when using with java 11 (though gives a warning as mentioned here: #1618), but if there is some code formatting issue, then ktlintFormat fails with the above version, and may be other java versions as well.

./gradlew --version


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(TM) 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

./gradlew ktlintFormat

Task :ktlintFormat FAILED
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.internal.KtlintCommandLine.parallel(KtlintCommandLine.kt:605)
at com.pinterest.ktlint.internal.KtlintCommandLine.parallel$default(KtlintCommandLine.kt:563)
at com.pinterest.ktlint.internal.KtlintCommandLine.lintFiles(KtlintCommandLine.kt:321)
at com.pinterest.ktlint.internal.KtlintCommandLine.run(KtlintCommandLine.kt:268)
at com.pinterest.ktlint.Main.main(Main.kt:27)
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.invalidate(TreeElement.java:328)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.TreeElement.rawRemove(TreeElement.java:312)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.TreeElement.rawReplaceWithList(TreeElement.java:319)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafElement.rawReplaceWithText(LeafElement.java:146)
at com.pinterest.ktlint.ruleset.standard.IndentationRule.visitWhiteSpace(IndentationRule.kt:851)
at com.pinterest.ktlint.ruleset.standard.IndentationRule.beforeVisitChildNodes(IndentationRule.kt:343)
at com.pinterest.ktlint.core.KtLint.executeRuleOnNodeRecursively(KtLint.kt:208)
at com.pinterest.ktlint.core.KtLint.executeRuleOnNodeRecursively(KtLint.kt:217)
at com.pinterest.ktlint.core.KtLint.executeRuleOnNodeRecursively(KtLint.kt:217)
at com.pinterest.ktlint.core.KtLint.executeRuleOnNodeRecursively(KtLint.kt:217)
at com.pinterest.ktlint.core.KtLint.executeRuleOnNodeRecursively(KtLint.kt:217)
at com.pinterest.ktlint.core.KtLint.executeRuleOnNodeRecursively(KtLint.kt:217)
at com.pinterest.ktlint.core.KtLint.executeRuleOnNodeRecursively(KtLint.kt:217)
at com.pinterest.ktlint.core.KtLint.executeRuleOnNodeRecursively(KtLint.kt:217)
at com.pinterest.ktlint.core.KtLint.executeRuleOnNodeRecursively(KtLint.kt:217)
at com.pinterest.ktlint.core.KtLint.executeRuleOnNodeRecursively(KtLint.kt:217)
at com.pinterest.ktlint.core.KtLint.executeRuleOnNodeRecursively(KtLint.kt:217)
at com.pinterest.ktlint.core.KtLint.executeRuleOnNodeRecursively(KtLint.kt:217)
at com.pinterest.ktlint.core.KtLint.executeRule(KtLint.kt:195)
at com.pinterest.ktlint.core.KtLint.access$executeRule(KtLint.kt:30)
at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:252)
at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:251)
at com.pinterest.ktlint.core.internal.VisitorProvider$visitor$4.invoke(VisitorProvider.kt:82)
at com.pinterest.ktlint.core.internal.VisitorProvider$visitor$4.invoke(VisitorProvider.kt:80)
at com.pinterest.ktlint.core.KtLint.format(KtLint.kt:251)
at com.pinterest.ktlint.internal.FileUtilsKt.formatFile(FileUtils.kt:254)
at com.pinterest.ktlint.internal.KtlintCommandLine.process(KtlintCommandLine.kt:379)
at com.pinterest.ktlint.internal.KtlintCommandLine.access$process(KtlintCommandLine.kt:52)
at com.pinterest.ktlint.internal.KtlintCommandLine$lintFiles$3.invoke$lambda-0(KtlintCommandLine.kt:312)
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 @43393097
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.(ThrowableInterner.java:88)
... 40 more

@paul-dingemans
Copy link
Collaborator

@ashish12 Tnx for the example repo. I can not reproduce the problem with the example repo with the Java Corretto 17.0.4.1 version. I have tried with ktlint version 0.45.2, 0.46.1 and 0.47.1. I am running on MacOs. What is your OS?

@ashish12
Copy link

./gradlew --version included in my previous comment, gave the details about OS: OS: Mac OS X 12.5.1 x86_64.

Now with updated OS Mac OS X 12.6 x86_64, as well, I am getting the same issue with the example repo.
JVM: 17.0.4 (Amazon.com Inc. 17.0.4+8-LTS)
OS: Mac OS X 12.6 x86_64

All I am doing is running, ./gradlew ktlintFormat.

@paul-dingemans
Copy link
Collaborator

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 the ktlintFormat task as follows:

val ktlintFormat by tasks.creating(JavaExec::class) {
    inputs.files(inputFiles)
    outputs.dir(outputDir)

    description = "Fix Kotlin code style deviations."
    classpath = ktlint
    jvmArgs = listOf("--add-opens=java.base/java.lang=ALL-UNNAMED")
    setProperty("mainClass", "com.pinterest.ktlint.Main")
    args = listOf("-F", "src/**/*.kt")
}

@ch4rl3x
Copy link

ch4rl3x commented Nov 30, 2022

Thanks @paul-dingemans, @cwilper
Temporarily fixed by adding the following line to my ktlinFormat definition

task ktlintFormat(type: JavaExec, group: "formatting") {
    ...
    jvmArgs "--add-opens=java.base/java.lang=ALL-UNNAMED"
}

@paul-dingemans
Copy link
Collaborator

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.

brownbearsoftware added a commit to ministryofjustice/hmpps-approved-premises-api that referenced this issue Mar 14, 2023
Upstream dependencies of ktlint need to be updated, in the meantime this is a workaround.  See pinterest/ktlint#1391
copybara-service bot pushed a commit to androidx/androidx that referenced this issue Mar 23, 2023
The recommendation from the corresponding ktlint issue: pinterest/ktlint#1391 (comment)

Bug: 273619707

Test: echo >> appactions/interaction/interaction-service/src/main/java/androidx/appactions/interaction/service/AppInteractionServiceFactory.kt \
       && ./gradlew :appactions:interaction:interaction-service:ktlintFormat

Change-Id: Ie4d8d95ce873d8c068ebc200191a1fc8c7ea8d1f
@leinardi
Copy link
Contributor

Hello @paul-dingemans, would it be possible to re-open this issue until a proper fix is available?
Also, could you please link here the related issues for the upstream dependencies so that we can subscribe and put some pressure on them for a fix?

@paul-dingemans
Copy link
Collaborator

Hello @paul-dingemans, would it be possible to re-open this issue until a proper fix is available? Also, could you please link here the related issues for the upstream dependencies so that we can subscribe and put some pressure on them for a fix?

See #1618

@mikaello
Copy link

mikaello commented May 22, 2023

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.:

$ mkdir -p .mvn
$ echo "--add-opens java.base/java.lang=ALL-UNNAMED" > .mvn/jvm.config

Credit: gantsign/ktlint-maven-plugin#392 (comment)

Julian added a commit to bowtie-json-schema/bowtie that referenced this issue Sep 5, 2023
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 implementers to forget adding explicitly to this list.

Manually testing seems to confirm this is the case (by breaking a file
and seeing ktlint complain).

Though we're running into pinterest/ktlint#1618 (AKA
pinterest/ktlint#1391) and I can't seem to figure out how to pass
through JVM args...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests