How to disable ktlint max-line-length standard rule

3k views Asked by At

I'm using ktlint 0.50.0, with Spotless 6.20.0, on AGP 8.1.0. Running spotless as a PreCommit Git hook. Due to the existence of too many old code, I'm trying to disable some rules. The one that gives me trouble is the "max_line_length".

How can I disable it?

I created a simple ".editorconfig" file (I want to get all the defaults for the rest of the rules):

root = true

[*]

[*.java]


[{*.gradle.kts,*.kt,*.kts,*.main.kts}]


ktlint_standard_comment-wrapping = disabled    # this one is successfully disabled

So, I tried all possible combinations, also tried the property on different levels:

max_line_length = off           # as property
max_line_length = 2147483647    # using Int.MAX_VALUE

ktlint_standard_max-line-length = disabled
ktlint_standard_max_line_length = disabled
ktlint_standard_max-line-length = off
ktlint_standard_max_line_length = off

I don't want to use in-file ktlint comments, as this requires changes in the files... and I do not want to go there.

When trying to commit, I'm always getting the Max line Length exceeded error...

Any suggestion on how can I disable this specific rule?

What am I missing?

Any suggestion...

1

There are 1 answers

0
ivtoto On

The only thing that worked for me was to "overwrite" those parameters through the Spotless editorConfigOverride API. Changing:

spotless {
    kotlin {
        ktlint(Versions.ktlint)
    }
}

to:

spotless {
    kotlin {
        ktlint(Versions.ktlint)
                .userData(["android" : "true"])
                .editorConfigOverride([
                        "ktlint_standard_comment-wrapping" : "disabled",
                        "max_line_length" : 2147483647,
                ])
    }
}

Note the 2147483647 value, this is Int.MAX_VALUE. I checked in the ktlint MaxLineLengthEditorConfigProperty and CommentWrappingRule code that this should disable it... and it worked.

I completely removed the .editorconfig file, as those were the only "custom" options.

Before that, I also tried setting a higher value for the max_line_length in the .editorconfig file... on every level and it did not work, it was always defaulting to 150.