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

Internal error: NoSuchElementException in TrailingCommaRule when using --experimental #1280

Closed
valentjn opened this issue Nov 7, 2021 · 0 comments · Fixed by #1283
Closed

Comments

@valentjn
Copy link

valentjn commented Nov 7, 2021

Expected Behavior

ktlint doesn't crash with an internal error.

Observed Behavior

ktlint crashes with an internal error when running on a specific file due to a NoSuchElementException in TrailingCommaRule when using --experimental.

Steps to Reproduce

  1. In an empty folder, create a file Test.kt with the following contents:
class Test {
  var foo = Bar()
    set(value) {
    }
}
  1. Run ktlint --experimental --debug Test.kt.
  2. ktlint produces an output similar to the following:
[DEBUG] Discovered ruleset with " standard" id.
[DEBUG] Discovered ruleset with "experimental" id.
[DEBUG] Discovered reporter with "baseline" id.
[DEBUG] Discovered reporter with "checkstyle" id.
[DEBUG] Discovered reporter with "json" id.
[DEBUG] Discovered reporter with "html" id.
[DEBUG] Discovered reporter with "plain" id.
[DEBUG] Discovered reporter with "sarif" id.
[DEBUG] Initializing "plain" reporter with {verbose=false, color=false, color_name=DARK_GRAY}
[DEBUG] Checking D:\tmp\Test.kt
Resolving .editorconfig files for D:\tmp\Test.kt file path
Loaded .editorconfig: []
[DEBUG] Internal Error (experimental:trailing-comma)
com.pinterest.ktlint.core.RuleExecutionException: java.util.NoSuchElementException: Sequence contains no 
element matching the predicate.
        at com.pinterest.ktlint.core.KtLint$lint$1.invoke(KtLint.kt:166)
        at com.pinterest.ktlint.core.KtLint$lint$1.invoke(KtLint.kt:147)
        at com.pinterest.ktlint.core.KtLint$visitor$2$3.invoke(KtLint.kt:317)
        at com.pinterest.ktlint.core.KtLint$visitor$2$3.invoke(KtLint.kt:315)
        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.KtLint$visitor$2.invoke(KtLint.kt:315)
        at com.pinterest.ktlint.core.KtLint$visitor$2.invoke(KtLint.kt:293)
        at com.pinterest.ktlint.core.KtLint.lint(KtLint.kt:147)
        at com.pinterest.ktlint.internal.FileUtilsKt.lintFile(FileUtils.kt:172)
        at com.pinterest.ktlint.KtlintCommandLine.process(Main.kt:395)
        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:1128)     
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)     
        at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.util.NoSuchElementException: Sequence contains no element matching the predicate.        
        at com.pinterest.ktlint.ruleset.experimental.trailingcomma.TrailingCommaRule.visitValueList(TrailingCommaRule.kt:357)
        at com.pinterest.ktlint.ruleset.experimental.trailingcomma.TrailingCommaRule.visit(TrailingCommaRule.kt:61)
        at com.pinterest.ktlint.core.KtLint$lint$1.invoke(KtLint.kt:154)
        ... 20 more
D:\tmp\Test.kt:3:9: Internal Error (experimental:trailing-comma). Please create a ticket at https://github.com/pinterest/ktlint/issues (if possible, please re-run with the --debug flag to get the stacktrace and provide the source code that triggered an error) (cannot be auto-corrected)
[DEBUG] 1625ms / 1 file(s) / 1 error(s)

Additional Information

I was just migrating my project ltex-ls to use the new trailing-comma rules when I discovered this. However, the bug occurs even if the trailing-comma rules have not been enabled (no .editorconfig file or trailing-comma rules explicitly set to false). It suffices to use --experimental to trigger the bug.

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): Command-line ktlint
  • Version of Gradle used (if applicable): N/A
  • Operating System and version: Windows 10
  • Link to your project (if it's a public repository): https://github.com/valentjn/ltex-ls (but you obviously won't find the example code there)
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

Successfully merging a pull request may close this issue.

1 participant