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

Increase number of consecutive runs of ktlint in case format introduces new problem #2129

Closed
paul-dingemans opened this issue Jul 13, 2023 · 0 comments · Fixed by #2132
Closed
Assignees

Comments

@paul-dingemans
Copy link
Collaborator

Given code below:

class EnhanceStackTraceWithTreadDumpAsJsonTest : DebugTestBase() {
    @Test
    fun testEnhancedStackTraceFormatWithDeferred() = runTest {
        val enhancedStackTraceFromJson = Gson().fromJson(enhancedStackTraceAsJsonString, Array<StackTraceElementInfoFromJson>::class.java)
    }
}

and .editorconfig:

root = true

[**/*.{kt,kts}]
ktlint_code_style = ktlint_official
ktlint_experimental = enabled
ktlint_standard = enabled

then ktlint 0.50.0 is not able to format this in 3 consecutive runs.

After the first iteration, the code is transformed to:

class EnhanceStackTraceWithTreadDumpAsJsonTest : DebugTestBase() {
    @Test
    fun testEnhancedStackTraceFormatWithDeferred() =
        runTest {
            val enhancedStackTraceFromJson = Gson().fromJson(enhancedStackTraceAsJsonString, Array<StackTraceElementInfoFromJson>::class.java)
        }
}

But now line 5 is exceeding the max-line-length (defaulted to 140). After fixing this in the second iteration, the code is transformed to:

class EnhanceStackTraceWithTreadDumpAsJsonTest : DebugTestBase() {
    @Test
    fun testEnhancedStackTraceFormatWithDeferred() =
        runTest {
            val enhancedStackTraceFromJson = Gson().fromJson(
                enhancedStackTraceAsJsonString,
                Array<StackTraceElementInfoFromJson>::class.java
            )
        }
}

Next problem is that the multiline expression, starting at line 5, is wrapped in the third iteration. The code is transformed to:

class EnhanceStackTraceWithTreadDumpAsJsonTest : DebugTestBase() {
    @Test
    fun testEnhancedStackTraceFormatWithDeferred() =
        runTest {
            val enhancedStackTraceFromJson =
                Gson().fromJson(
                    enhancedStackTraceAsJsonString,
                    Array<StackTraceElementInfoFromJson>::class.java
                )
        }
}

And finally the last problem (a missing trailing comma at line 8 is identified) which is fixed in the fourth iteration:

class EnhanceStackTraceWithTreadDumpAsJsonTest : DebugTestBase() {
    @Test
    fun testEnhancedStackTraceFormatWithDeferred() =
        runTest {
            val enhancedStackTraceFromJson =
                Gson().fromJson(
                    enhancedStackTraceAsJsonString,
                    Array<StackTraceElementInfoFromJson>::class.java,
                )
        }
}

Because of the order in which the rules are executed, it is sometimes needed to iterate multiple times. Setting this number too high has consequences for total througput time in case an accidental endless loop is triggered (for example see #2128).

Options:

  • Increase maximum number of consecutive runs to 5
  • make the maximum number of consecutive runs configurable with current default of 3
  • accept that occasionally multiple manual incovations are needed to fix all problems
@paul-dingemans paul-dingemans self-assigned this Jul 15, 2023
paul-dingemans added a commit that referenced this issue Jul 15, 2023
…ich (theoretically) can be autocorrected in file ... in 3 consecutive runs of format` only when after the third consecutive run still a violation exists that can be autocorrected.

Closes #2129
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