Ktlint doesn't allow wildcard import? (Using IntelliJ auto import feature)

21.6k Views Asked by At

I am running a Kotlin project with ktlint maven plugin (first time user). but whenever I do a maven build. I often see the failure along the lines of

src/main/kotlin/com/myproject/model/User.kt:7:1: Wildcard import (cannot be auto-corrected)

Since I use IntelliJ, I often rely on auto import where many subpackages are grouped into a wildcard (*). Is this what ktline used to enforce good import practices?

3

There are 3 best solutions below

3
On BEST ANSWER

As of version 0.34 or thereabouts you can override individual rules via EditorConfig. Add this to your .editorconfig file in the root of the project:

[*.{kt,kts}]
ktlint_no-wildcard-imports = disabled

https://pinterest.github.io/ktlint/faq/#how-do-i-globally-disable-a-rule

1
On

I was using ktlint-gradle and using the command ./gradlew ktlintCheck and my .editorconfig wasn't getting picked up. I solved it by adding this to build.gradle

ktlint {
    disabledRules.set(setOf("no-wildcard-imports"))
}
3
On

ktlint changed the rule layout in .editorconfig (again). Now it has to be:

ktlint_standard_no-wildcard-imports = disabled