Closed ctdavids closed 3 weeks ago
You last code sample contains line below:
class ThisDoesNotFailFormattingTest {a
Note the last character on this line. Is this intentional?
No, it's not intentional and isn't required for the bug (nor is it present in the uploaded sample). Not sure how it got there. I'll edit it away.
So I have a guess as to what might be going on here. It might be running afoul of "standard:no-unused-imports" due to ignoring the contents inside the formatter off tags. I don't know why that would cause the stack trace (not that I've looked at its details), but it would explain the difference in behaviour between the "in the file" case and the "imported from elsewhere" case.
This may relate to ktlint 1.3.0 as well since version 23.0 of the plugin uses that by default which I don't believe was true with 22.0.
This problem does not seem to be caused by the ktlint-intellij-plugin
but in ktlint
itself. I can reproduce the exact same stacktrace, by malforming the "@formatter:on" tag:
$ ktlint-1.3.0 --stdin
08:03:08.208 [main] INFO com.pinterest.ktlint.cli.internal.KtlintCommandLine -- Enable default patterns [**/*.kt, **/*.kts]
fun foo123() {
// @formatter:off
listOf(
"x" to 1, "x" to 2, "x" to 3,
"x" to 1, "x" to 2, "x" to 3,
"y" to 1, "y" to 2, "y" to 3,
"z" to 1, "z" to 2, "z" to 3,
)
// The "@formatted:on" tag below is malformed on purpose.
// It will cause an exception when indenting
// @formstter:on
}
Exception in thread "main" java.lang.IllegalArgumentException: Stack should be empty:
IndentContext(fromASTNode=Element(kotlin.FILE), toASTNode=PsiWhiteSpace, nodeIndent=, firstChildIndent=, childIndent=, lastChildIndent=, activated=true)
IndentContext(fromASTNode=BLOCK, toASTNode=PsiElement(RBRACE), nodeIndent=, firstChildIndent= , childIndent= , lastChildIndent= , activated=false)
IndentContext(fromASTNode=PsiElement(LBRACE), toASTNode=PsiElement(RBRACE), nodeIndent=, firstChildIndent=, childIndent= , lastChildIndent=, activated=true)
at com.pinterest.ktlint.ruleset.standard.rules.IndentationRule.afterLastNode(IndentationRule.kt:1066)
at com.pinterest.ktlint.rule.engine.internal.RuleExecutionContext.executeRule(RuleExecutionContext.kt:65)
at com.pinterest.ktlint.rule.engine.internal.CodeFormatter.executeRule(CodeFormatter.kt:116)
at com.pinterest.ktlint.rule.engine.internal.CodeFormatter.format(CodeFormatter.kt:87)
at com.pinterest.ktlint.rule.engine.internal.CodeFormatter.format(CodeFormatter.kt:56)
at com.pinterest.ktlint.rule.engine.internal.CodeFormatter.format(CodeFormatter.kt:27)
at com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine.lint(KtLintRuleEngine.kt:91)
at com.pinterest.ktlint.cli.internal.KtlintCommandLine.lint(KtlintCommandLine.kt:558)
at com.pinterest.ktlint.cli.internal.KtlintCommandLine.process(KtlintCommandLine.kt:466)
at com.pinterest.ktlint.cli.internal.KtlintCommandLine.lintStdin(KtlintCommandLine.kt:425)
at com.pinterest.ktlint.cli.internal.KtlintCommandLine.lintOrFormat(KtlintCommandLine.kt:296)
at com.pinterest.ktlint.cli.internal.KtlintCommandLine.run(KtlintCommandLine.kt:246)
at com.github.ajalt.clikt.parsers.Parser.finalizeAndRun(Parser.kt:348)
at com.github.ajalt.clikt.parsers.Parser.parse(Parser.kt:218)
at com.github.ajalt.clikt.parsers.Parser.parse(Parser.kt:42)
at com.github.ajalt.clikt.core.CliktCommand.parse(CliktCommand.kt:457)
at com.github.ajalt.clikt.core.CliktCommand.parse$default(CliktCommand.kt:454)
at com.github.ajalt.clikt.core.CliktCommand.main(CliktCommand.kt:474)
at com.github.ajalt.clikt.core.CliktCommand.main(CliktCommand.kt:481)
at com.pinterest.ktlint.Main.main(Main.kt:20)
This may relate to ktlint 1.3.0 as well since version 23.0 of the plugin uses that by default which I don't believe was true with 22.0.
Indeed, with ktlint 1.2.1 above problem does not occur.
I will close this issue, and resolve it in ktlint 1.3.1 (https://github.com/pinterest/ktlint/issues/2695).
Great thanks. I found a workaround as well which was to have at least one test that relied on the import and was NOT inside the formatter tags (thus meaning there was no unused import). For some reason this avoided the failure.
Great thanks. I found a workaround as well which was to have at least one test that relied on the import and was NOT inside the formatter tags (thus meaning there was no unused import). For some reason this avoided the failure.
Actually, you just found another bug in ktlint. Tnx!
Appreciate your rapid responses!
Discovered this testing the solution for https://github.com/nbadal/ktlint-intellij-plugin/issues/527
The dev mode plugin produces this failure:
when being asked to format:
However oddly it does NOT fail when formatting the same thing, but with the infix function defined in the same file:
Example project attached with the relevant files being in the test code and named as above.
ktlintExample.zip