ktlint icon indicating copy to clipboard operation
ktlint copied to clipboard

The maximum line length of 120 has been exceeded. The rule cannot be modified or disabled.

Open Turalllb opened this issue 2 years ago • 14 comments

ktlint { version.set("0.45.2") android.set(true) additionalEditorconfigFile.set(file("/my-android/.editorconfig")) disabledRules.set(Set.of("max_line_length", "import-ordering", "no-wildcard-imports", "final-newline", "missing-newline-after", "no-unused-imports", "indent")) } }

image image

Result: Exceeded max line length (100) (cannot be auto-corrected)

With these settings, I keep getting an error about exceeding 100 characters. What could I set up wrong, why is everything so not obvious (

Also tell me, is it possible to disable the rule that prohibits putting a comma at the end of the enumeration, for example, in the class constructor after the very last argument? I would like to be able to put a comma, ide allows me to do this, but ktlint does not

image image version 10.3.0

Turalllb avatar Jun 15 '22 21:06 Turalllb

Please update to latest version of ktlint (0.45.2) first and report back if the problem still occurs.

paul-dingemans avatar Jun 16 '22 07:06 paul-dingemans

@paul-dingemans Updated the version and edited the question. The problem persists. I seem to be doing something wrong

Turalllb avatar Jun 16 '22 12:06 Turalllb

I tried to specify the full path to the .editorConfig file, but it did not help

Turalllb avatar Jun 16 '22 13:06 Turalllb

Based on your last screendump, I get the feeling that you only epxerience the problem with ktolin script (*.kts) files. That is caused by the space before *.kts in the .editorconfig file. Change it to: [{*.kt,*.kts}].

paul-dingemans avatar Jun 16 '22 16:06 paul-dingemans

Also tell me, is it possible to disable the rule that prohibits putting a comma at the end of the enumeration, for example, in the class constructor after the very last argument? I would like to be able to put a comma, ide allows me to do this, but ktlint does not

Please read documentation. https://github.com/pinterest/ktlint#trailing-comma

paul-dingemans avatar Jun 16 '22 16:06 paul-dingemans

@paul-dingemans image

I was already happy, but deleting this space didn’t help either. (I even deleted everything else and left it as on the screen, but that doesn’t help either. I cleared the cache in android studio, all to no avail.

Turalllb avatar Jun 16 '22 19:06 Turalllb

In screenshot above you have set max_line_length=off so that might be the cause. Also, you need to explcitily save the .editorconfig file as changes otherwise are not picked up by IntelliJ IDEA.

If error still occurs then please paste the source code instead of only providing a screenshot.

paul-dingemans avatar Jun 16 '22 19:06 paul-dingemans

@paul-dingemans

[*.{kt,kts}]
max_line_length=105
indent_size=4
insert_final_newline=false



disabled_rules=max_line_length
ktlint {
        version.set("0.45.2")
        android.set(true)
        additionalEditorconfigFile.set(file("/my_project/.editorconfig"))
        //disabledRules.set(Set.of("max_line_length", "import-ordering", "final-newline", "missing-newline-after", "no-unused-imports", "indent"))
    }

Here are my settings. In the logs, I get a message about exceeding 100 characters, although I set 105 for the experiment. It is noteworthy that I commented out the android.set(true) line and I picked up the setting about 105 characters, but I returned this setting again, and then turned it off again and that's it .. I can no longer come to this state and did not understand what affected it. I also did not understand what you said about the fact that ide does not save changes. I tried turning "enable editorConfig support" off and on thinking I could get it to save the settings, but alas.

Turalllb avatar Jun 16 '22 22:06 Turalllb

it is also not clear where it is better to write disabled_rules in gradle or in .editorConfig and which entry has a higher priority

Turalllb avatar Jun 16 '22 22:06 Turalllb

The ktlint gradle plugin is maintained in another project, so I can not be 100% sure what is happening. From the perspective of Ktlint, I can say that:

  1. The max line length is not checked for lines that only contain an EOL comment like your //disabledRules.set(...)
  2. In your example, an additional .editorconfig files seems to be provided via additionalEditorconfigFile.set(file("/my_project/.editorconfig")). Assuming that the gradle plugin passes this file to ktlint then all other .editorconfig files are ignored.
  3. If the API consumer of ktlint, e.g. the gradle plugin, is calling ktlint correctly then it can override properties which are specified in the .editorconfig file. This override the setting provided via .editorconfig files (including the additional file mentioned in previous step).

paul-dingemans avatar Jun 18 '22 09:06 paul-dingemans

I can confirm that the check cannot be disabled.

I think there's something wrong with editorconfig inheritance, for example, if I put max_line_length=off in [*] everything works. But if I put max_line_length=off in [**/test/**.kt] -- it doesn't work. Same goes for [*.kt] as well.

Looks like ktlint respects only root-level declaration.

sanyarnd avatar Jul 27 '22 18:07 sanyarnd

@sanyarnd I am happy to look into this, if you can document the full reproduction path including sources to be used, commands being executed and output (using option --verbose).

paul-dingemans avatar Jul 27 '22 19:07 paul-dingemans

sample project ktlint-max-length-bug.zip

.editorconfig:

root = true

[*]
max_line_length = 120

[{*.kt, *.kts}]
max_line_length = 80
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL


[**/test/**.kt]
max_line_length = off
ktlint_ignore_back_ticked_identifier = true

mvn package output:

[ERROR] src\main\kotlin\LongLineClass.kt:2:1: Exceeded max line length (80)
[ERROR] src\test\kotlin\LongLineTest.kt:2:1: Exceeded max line length (80)

sanyarnd avatar Jul 27 '22 20:07 sanyarnd

I can not reproduce the problem with your sample project. What OS are you running on? Based on the backslashed in the path, it looks like Windows.

When I run command: ktlint --trace --relative

11:31:46.237 [pool-1-thread-2] TRACE com.pinterest.ktlint.KtlintCommandLine - Checking src/main/kotlin/LongLineClass.kt
11:31:46.237 [pool-1-thread-1] TRACE com.pinterest.ktlint.KtlintCommandLine - Checking src/test/kotlin/LongLineTest.kt
11:31:46.753 [pool-1-thread-1] TRACE com.pinterest.ktlint.core.internal.EditorConfigLoader - Resolving .editorconfig files for ./src/test/kotlin/LongLineTest.kt file path:
	max_line_length: off, ij_kotlin_code_style_defaults: KOTLIN_OFFICIAL, ktlint_ignore_back_ticked_identifier: true
11:31:46.753 [pool-1-thread-2] TRACE com.pinterest.ktlint.core.internal.EditorConfigLoader - Resolving .editorconfig files for ./src/main/kotlin/LongLineClass.kt file path:
	max_line_length: 80, ij_kotlin_code_style_defaults: KOTLIN_OFFICIAL

Please send your complete output of command ktlint --trace --relative for further analysis.

Please also note that the "space" in [{*.kt, *.kts}] does not result in what you might expect (the settings would only be on kts-files for which the filename actually starts with a space).

paul-dingemans avatar Aug 07 '22 09:08 paul-dingemans

Tested 0.47.0 and error is gone now

sanyarnd avatar Aug 23 '22 12:08 sanyarnd

Tested 0.47.0 and error is gone now

Ok, tnx for confirming.

paul-dingemans avatar Aug 31 '22 17:08 paul-dingemans

@Turalllb This is a little late but it seems like "max_line_length" should be "max-line-length" instead?

logi-nilsson avatar Apr 26 '23 09:04 logi-nilsson

@Turalllb This is a little late but it seems like "max_line_length" should be "max-line-length" instead?

Funny, just a couple of day there was another issue in which the same problem occurred. As I suggested in https://github.com/pinterest/ktlint/issues/1974, ktlint should give a more clear warning.

paul-dingemans avatar Apr 26 '23 19:04 paul-dingemans

funny enough max_line_length is actually max-line-length

Kashif-E avatar May 05 '23 09:05 Kashif-E

funny enough max_line_length is actually max-line-length

I am not sure what you mean. Do you care to explain?

paul-dingemans avatar May 05 '23 14:05 paul-dingemans