Having a problem getting ktlint to read overrides ...
# ktlint
c
Having a problem getting ktlint to read overrides in .editorconfig; this setup:
Copy code
root = true

[*.{kt,kts}]
charset = utf-8
end_of_line = lf
ktlint_code_style = ktlint_official

[dsl/*.{kt,kts}]
ktlint_standard_filename = disabled
ktlint_standard_max-line-length = disabled
…fails to disable the two rules in the second stanza. Moving those rules to the first stanza works. Running 0.50.0 via ktlint-gradle v11.5.0 Have tried variations on the syntax such as
dsl/**.{kt,kts}
without luck.
p
Can you try to run Ktlint's CLI separately to rule out whether the problem is in Ktlint or in ktlint-gradle?
c
good idea, trying that noww..
Same issue. Downloaded 0.50.0 into the root of the project, ran with
./ktlint
- the second stanza is not processed.
p
Can you change the order of the sections?
c
I will try, but the docs are pretty clear that this is the right syntax. One sec.
no change. ktlint CLI shows all the violations for the to-be-disabled rules.
p
One other thing to try is to place a separate .editorconfig in your dsl directory
c
that worked! thanks. Looks like something is buggy there or perhaps docs are wrong.
p
I think it is the docs. For reading the
.editorconfig
we use a library. Cam you share the link to the docs that mislead you?
c
https://pinterest.github.io/ktlint/0.50.0/rules/configuration-ktlint/#overriding-editorconfig-properties-for-specific-directories It’s pretty specific:
Copy code
[*.{kt,kts}]
ktlint_standard_import-ordering = disabled

[api/*.{kt,kts}]
ktlint_standard_indent = disabled
p
Tnx. I will have a look.
❤️ 1
I have checked out problem above. The second section should be:
Copy code
[**/api/*.{kt,kts}]
ktlint_standard_indent = disabled
Can you verify that it works for you as well so that I can change the documentation? Or feel free, to submit a PR for it 😉
c
Hmmm. Had tried that pattern earlier (its noted in the .editorconfig docs), and again just now, its not working. This is
.editorconfig
Copy code
root = true

[*.{kt,kts}]
charset = utf-8
end_of_line = lf
max_line_length = 200

ktlint_code_style = ktlint_official
ktlint_standard = enabled
ktlint_experimental = disabled
ktlint_ignore_back_ticked_identifier = true

ktlint_function_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = 3
ktlint_function_signature_body_expression_wrapping = default

ktlint_standard_max-line-length = disabled
ktlint_standard_property-naming = disabled
ktlint_standard_chain-wrapping = enabled
ktlint_standard_function-signature = enabled
ktlint_standard_enum-wrapping = enabled

ij_kotlin_packages_to_use_import_on_demand = unset
ij_kotlin_name_count_to_use_star_import = 2147483647
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
ij_kotlin_allow_trailing_comma_on_call_site = false
ij_kotlin_allow_trailing_comma = false
ij_kotlin_method_call_chain_wrap = split_into_lines

[**/dsl/*.{kt,kts}]
ktlint_standard = disabled
ktlint_standard_filename = disabled
Invoking ktlint this way:
Copy code
val ktlint: Configuration by configurations.creating

dependencies {
    ktlint("com.pinterest:ktlint:0.50.0")
}

val ktlintFormat = tasks.register<JavaExec>("ktlintFormat") {
    group = LifecycleBasePlugin.VERIFICATION_GROUP
    description = "Check Kotlin code style and format"
    classpath = ktlint
    mainClass.set("com.pinterest.ktlint.Main")
    jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED")
    args("--format", "**/src/**/*.kt", "**.kts", "!build-logic/build/**")
//    args("--format", "**/src/**/*.kt", "**.kts", "!build-logic/build/**", "!dsl/src/**/*.kt")
}
Results in lots of:`/Users/chrislee/IdeaProjects/cloudshift/kotlin/awscdk-dsl-kotlin/dsl/src/main/kotlin/cloudshift/awscdk/dsl/cloudassembly/schema/_schema.kt11: File '_schema.kt' contains a single top level declaration and should be named 'Schema.kt' (standard:filename)` The goal it to selectively disable rules for
dsl/**
as its generated code, or exclude it entirely. Have been able to accomplish this by: • separate .editorconfig in
dsl
folder; • or, excluding it in the command line args (commented out in the gradle snippet above) Have not been able to influence it from within a single root
.editorconfig
.
For reference, had tested this yesterday also with the gradle-ktlint plugin, with the same effects.
p
I don’t understand why it does not work for you with
.editorconfig
. Did you by accident happen to change the
.editorconfig
file in IntelliJ IDEA (or Android Studio)? And did you save the file explicitly before rerunning
ktlint
? I see it happen regularly that without an explicit save of
.editorconfig
that the changes are not picked up by
ktlint
. Other question that I have, is the directory where the root
.editorconfig
is stored.
c
I’ve set my
.editorconfig
back to the above (added back
[**/dsl/*.{kt,kts}]
section) and removed the
dsl
exclusion from the command line (as above), and also confirmed (via
cat .editorconfig
) that the changes are indeed saved. It still does not disable for
**/dsl
:
Copy code
/Users/chrislee/IdeaProjects/cloudshift/kotlin/awscdk-dsl-kotlin/dsl/src/main/kotlin/cloudshift/awscdk/dsl/services/oam/_oam.kt:1:1: File '_oam.kt' contains a single top level declaration and should be named 'Oam.kt' (standard:filename)
The root
.editorconfig
is at the root of the project; the
dsl
directory (to exclude) is also at the root of the project, and does not contain an
.editorconfig
(there are no other
.editorconfig
files in the project).
Output from `./gradlew --version`; given how ktlin is being executed (similar to CLI) the only likely relevant piece is the JVM version.
Copy code
------------------------------------------------------------
Gradle 8.2.1
------------------------------------------------------------

Build time:   2023-07-10 12:12:35 UTC
Revision:     a38ec64d3c4612da9083cc506a1ccb212afeecaa

Kotlin:       1.8.20
Groovy:       3.0.17
Ant:          Apache Ant(TM) version 1.10.13 compiled on January 4 2023
JVM:          19.0.2 (<http://Amazon.com|Amazon.com> Inc. 19.0.2+7-FR)
OS:           Mac OS X 13.4.1 aarch64
Consistently reproducible running Gradle task from IntelliJ or from command line. Running installed ktlint CLI:
Copy code
➜  awscdk-dsl-kotlin git:(main) ✗ ktlint --version
0.50.0
Has the same challenge of not honouring the additional section in`.editorconfig`
(of note, IntelliJ JVM version is 17.x)
Project is available here; what is currently committed is not using the additional section in
.editorconfig
515 Views