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

Intermittent failures in IndentationRule causing 'Stack should be empty' errors #7005

Open
nfirmani-dd opened this issue Feb 29, 2024 · 0 comments

Comments

@nfirmani-dd
Copy link

Behavior

When running detekt with the ktlint formatter set enabled (see attached reproducer project), the IndentationRule which is called in ktlint intermittently sees a failure during parsing.

Config:

  # full detekt.yml in attached project
formatting: 
  Indentation:
    active: true
    autoCorrect: true
    indentSize: 4

Stacktrace:

Location: com.pinterest.ktlint.ruleset.standard.rules.IndentationRule.afterLastNode(IndentationRule.kt:965)
The original exception message was: Stack should be empty:
  IndentContext(fromASTNode=Element(kotlin.FILE), toASTNode=PsiWhiteSpace, nodeIndent=, firstChildIndent=, childIndent=, lastChildIndent=, activated=true)
  IndentContext(fromASTNode=Element(CLASS), toASTNode=PsiElement(RBRACE), nodeIndent=, firstChildIndent=, childIndent=, lastChildIndent=, activated=false)
  IndentContext(fromASTNode=Element(CLASS_BODY), toASTNode=PsiElement(RBRACE), nodeIndent=, firstChildIndent=    , childIndent=    , lastChildIndent=, activated=false)
  IndentContext(fromASTNode=PsiElement(LBRACE), toASTNode=PsiElement(RBRACE), nodeIndent=, firstChildIndent=, childIndent=    , lastChildIndent=, activated=true)
  IndentContext(fromASTNode=BLOCK, toASTNode=PsiElement(RBRACE), nodeIndent=    , firstChildIndent=    , childIndent=    , lastChildIndent=    , activated=false)
  IndentContext(fromASTNode=Element(FUN), toASTNode=PsiElement(RPAR), nodeIndent=    , firstChildIndent=, childIndent=, lastChildIndent=, activated=true)

This occurs in a file that looks like the following, note the incorrect spacing here: (){

class XYZA {
    fun tearDown(){
    }
}

There was a similar issue reported a while back, #6357, but they could not provide reproduction steps.

Is this a bug in ktlint? What's the actual potential issue/root cause?

I don't think it's a bug in ktlint, and I tried to reproduce it in the ktlint project, checking out the tag that detekt uses, and then adding the following test runs successfully

> ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRuleTest.kt
 @Test
 fun `improperly spaced brackets should not crash`() {
     val code =
         """
         class XYZA {
             fun tearDown(){
             }
         }
         """.trimIndent()
     indentationRuleAssertThat(code)
         .hasNoLintViolations()
 }

I also tried to test using the command line, but could not reproduce it.

Looking at both the source of the detekt/FormatterRule and the ktlint/IndentationRule, it looks like detekt is doing some pre-processing of the ASTNodes being sent to ktlint, so perhaps that could be the issue? In any case, the following code from ktlint seems to be ineffectively removing the nodes from the stack, causing an eventual validation error at the end of the rule.

    override fun afterVisitChildNodes(node: ASTNode
        ...
        while (indentContextStack.peekLast()?.toASTNode == node) {
        indentContextStack.removeLast()

Which later causes the exception to be thrown in override fun afterLastNode() ... require(indentContextStack.isEmpty()).

Your Environment

  • Version of detekt used: 1.23.5 (but also reproduces on 1.23.3)
  • Version of Gradle used: 8.0, 8.2
  • Operating System and Java version: Java 17.0.2+8-LTS Mac OS X as well as Java 17.0.3+6-LTS Mac OS X

Steps to Reproduce

Given the following project
repro.zip

  • Run ./gradlew app:detekt .
  • Now, the interesting bit, it might fail the first time, but generally fails on the second invocation
  • Run git reset --hard HEAD to dismiss autofixes (if present)
  • Run ./gradlew app:detekt again. You might have to repeat this process with or without the git reset to see the error, but I confirmed with a colleauge that they could repro on the third attempt on the sample project before sending this bug report

Thanks in advance!

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

No branches or pull requests

2 participants