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

ClassCastException when loading trailing comma settings from editorconfig #1288

Closed
tailrecur opened this issue Nov 18, 2021 · 10 comments
Closed
Labels

Comments

@tailrecur
Copy link

Expected Behavior

ktlint loads the trailing comma settings from .editorconfig successfully

Observed Behavior

ktlint crashes with the following stacktrace:

Caused by: com.pinterest.ktlint.core.RuleExecutionException: java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Boolean (java.lang.String and java.lang.Boolean are in module java.base of loader 'bootstrap')
        at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:395)
        at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:374)
        at com.pinterest.ktlint.core.KtLint$visitor$2$4.invoke(KtLint.kt:323)
        at com.pinterest.ktlint.core.KtLint$visitor$2$4.invoke(KtLint.kt:322)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:229)
        at com.pinterest.ktlint.core.KtLint$visitor$2.invoke(KtLint.kt:322)
        at com.pinterest.ktlint.core.KtLint$visitor$2.invoke(KtLint.kt:293)
        at com.pinterest.ktlint.core.KtLint.format(KtLint.kt:374)
        at com.pinterest.ktlint.core.KtLint.format(KtLint.kt:352)
        at org.jlleitschuh.gradle.ktlint.worker.KtLintWorkAction.execute(KtLintWorkAction.kt:72)
        at org.gradle.workers.internal.DefaultWorkerServer.execute(DefaultWorkerServer.java:63)
        at org.gradle.workers.internal.AbstractClassLoaderWorker$1.create(AbstractClassLoaderWorker.java:49)
        at org.gradle.workers.internal.AbstractClassLoaderWorker$1.create(AbstractClassLoaderWorker.java:43)
        at org.gradle.internal.classloader.ClassLoaderUtils.executeInClassloader(ClassLoaderUtils.java:97)
        at org.gradle.workers.internal.AbstractClassLoaderWorker.executeInClassLoader(AbstractClassLoaderWorker.java:43)
        at org.gradle.workers.internal.IsolatedClassloaderWorker.run(IsolatedClassloaderWorker.java:49)
        at org.gradle.workers.internal.IsolatedClassloaderWorker.run(IsolatedClassloaderWorker.java:30)
        at org.gradle.workers.internal.WorkerDaemonServer.run(WorkerDaemonServer.java:85)
        at org.gradle.workers.internal.WorkerDaemonServer.run(WorkerDaemonServer.java:55)
        at org.gradle.process.internal.worker.request.WorkerAction$1.call(WorkerAction.java:138)
        at org.gradle.process.internal.worker.child.WorkerLogEventListener.withWorkerLoggingProtocol(WorkerLogEventListener.java:41)
        at org.gradle.process.internal.worker.request.WorkerAction.run(WorkerAction.java:135)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:567)
        at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
        at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
        at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
        at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
        at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:414)
        at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
        at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
        at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
        ... 1 more
Caused by: java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Boolean (java.lang.String and java.lang.Boolean are in module java.base of loader 'bootstrap')
        at com.pinterest.ktlint.ruleset.experimental.trailingcomma.TrailingCommaRule.getEditorConfigValues(TrailingCommaRule.kt:76)
        at com.pinterest.ktlint.ruleset.experimental.trailingcomma.TrailingCommaRule.visit(TrailingCommaRule.kt:51)
        at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:381)

Steps to Reproduce

build.gradle.kts contents:

import org.jlleitschuh.gradle.ktlint.reporter.ReporterType

plugins {
    id("org.jlleitschuh.gradle.ktlint") version "10.2.0"
}

repositories {
    mavenCentral()
}

subprojects {
    apply {
        plugin("org.jlleitschuh.gradle.ktlint")
    }

    repositories {
        mavenCentral()
    }

    configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
        version.set("0.43.0")
        debug.set(false)
        verbose.set(true)
        android.set(false)
        outputToConsole.set(true)
        outputColorName.set("RED")
        ignoreFailures.set(false)
        enableExperimentalRules.set(true) 
        reporters {
            reporter(ReporterType.PLAIN_GROUP_BY_FILE)
        }
    }
}

.editorconfig file contents:

[*]
indent_style = space
indent_size=4
insert_final_newline=true
max_line_length=120
ij_kotlin_allow_trailing_comma=true
ij_kotlin_allow_trailing_comma_on_call_site=true

Running the following command causes the above-mentioned stacktrace:

./gradlew ktlintFormat --stacktrace

Your Environment

  • Version of ktlint used: 0.43.0
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): Gradle plugin: org.jlleitschuh.gradle.ktlint:10.2.0
  • Version of Gradle used (if applicable):
------------------------------------------------------------
Gradle 7.1.1
------------------------------------------------------------

Build time:   2021-07-02 12:16:43 UTC
Revision:     774525a055494e0ece39f522ac7ad17498ce032c

Kotlin:       1.4.31
Groovy:       3.0.7
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          16 (AdoptOpenJDK 16+36)
OS:           Mac OS X 10.15.7 x86_64
  • Operating System and version:
  • Link to your project (if it's a public repository):
@paul-dingemans
Copy link
Collaborator

The rule still works for me. Can you please add a code sample which causes the error?

Other things that you might want to try as it could give additional insights:

  • Disabling ij_kotlin_allow_trailing_comma only
  • Disabling ij_kotlin_allow_trailing_comma_on_call_site only
  • Run with another java version (preferably less than 16 as we have some know issues with Java 16+)

@romtsn
Copy link
Collaborator

romtsn commented Dec 13, 2021

I feel like I've seen this issue before, it's most likely because you can't use mixed (spaces vs no-spaces) editorconfig values. If you remove the spaces around = here, I think it should work.

indent_style = space

@gotson
Copy link

gotson commented Dec 22, 2021

I feel like I've seen this issue before, it's most likely because you can't use mixed (spaces vs no-spaces) editorconfig values. If you remove the spaces around = here, I think it should work.

indent_style = space

I have the same problem as Op, but removing the space around the = doesn't help.

  • Run with another java version (preferably less than 16 as we have some know issues with Java 16+)

I confirm that i have the problem with Java 17, but not with Java 8

@paul-dingemans
Copy link
Collaborator

@tailrecur @gotson Can you please check whether this problem is solved with the 0.44.0 release?

@paul-dingemans
Copy link
Collaborator

Closing issue as no response was received in last 29 days.

@YSakhno
Copy link

YSakhno commented Aug 2, 2022

I can confirm that similar exception still (Ktlint v0.46.1) happens for me, albeit it happens for property ktlint_code_style, not for trailing-commas related one. I'm attaching stack traces if that helps.

Gradle version:

------------------------------------------------------------
Gradle 7.5
------------------------------------------------------------

Build time:   2022-07-14 12:48:15 UTC
Revision:     c7db7b958189ad2b0c1472b6fe663e6d654a5103

Kotlin:       1.6.21
Groovy:       3.0.10
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          11.0.12 (Eclipse Foundation 11.0.12+7)
OS:           Windows 10 10.0 amd64

Running the CLI version of Ktlint has the same problem.

Simply commenting out the ktlint_code_style = official line in .editorconfig solves the problem with the exception. Well, it solves the problem in CLI checker instantly, with Gradle it sometimes requires stopping the daemon first.

stacktrace-gradle.txt
stacktrace-cmd-line.txt

@paul-dingemans
Copy link
Collaborator

Tnx for reporting. Issue #1559 has been reported for ktlint_code_style. I expect it to be the same kind of problem.

@paul-dingemans
Copy link
Collaborator

Tnx for reporting. Issue #1559 has been reported for ktlint_code_style. I expect it to be the same kind of problem.

Should be resolved with #1564

@CokieForever
Copy link

Hi there, I have the same issue as the OP. I am using ktlint 0.47.0 and Java 11 (OpenJDK 11.0.12) on Windows 10 (Win10 Pro 21H2), although I can reproduce the issue with Java 8 and Java 17 as well.

Removing whitespaces does not seem to make any difference. It seems, however, that the issue arises only when the --editorconfig parameter is used and the .editorconfig file is not in the current directory.

Here is a little demonstration: ktlint_test.zip

> ktlint *.kt --editorconfig config

Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Boolean (java.lang.String and java.lang.Boolean are in module java.base of loader 'bootstrap')
        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.ClassCastException: class java.lang.String cannot be cast to class java.lang.Boolean (java.lang.String and java.lang.Boolean are in module java.base of loader 'bootstrap')
        at com.pinterest.ktlint.ruleset.standard.TrailingCommaOnCallSiteRule.beforeFirstNode(TrailingCommaOnCallSiteRule.kt:56)
        at com.pinterest.ktlint.core.KtLint.executeRule(KtLint.kt:194)
        at com.pinterest.ktlint.core.KtLint.access$executeRule(KtLint.kt:30)
        at com.pinterest.ktlint.core.KtLint$lint$1.invoke(KtLint.kt:176)
        at com.pinterest.ktlint.core.KtLint$lint$1.invoke(KtLint.kt:175)
        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.lint(KtLint.kt:175)
        at com.pinterest.ktlint.internal.FileUtilsKt.lintFile(FileUtils.kt:224)
        at com.pinterest.ktlint.internal.KtlintCommandLine.process(KtlintCommandLine.kt:409)
        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:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:829)

@CokieForever
Copy link

Works as from 0.47.1 for me. Thanks!

ktlint_test>ktlint *.kt --editorconfig config
C:\ktlint_test\Test.kt:2:16: Missing trailing comma before ")" (trailing-comma-on-declaration-site)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants