kotlingradlestatic-analysisktlint

Deactivate specific linter in spotless/ktlint


I'm new to Kotlin, and I'm updating an old repository that hadn't any linter yet.

I installed spotless/ktlint, and with the default configuration in build.gradle.ts, I've got the following error:

spotless {
    kotlin {
        target("**/src/**/*.kt")
        ktlint()
    }
    kotlinGradle {
        target("**/*.gradle.kts")
        ktlint()
    }
}

The error message:

* What went wrong:
Execution failed for task ':spotlessKotlin'.
> Error on line: 1, column: 9
  rule: standard:package-name
  Package name must not contain underscore

I know where the error is, but as the project is quite big, I'd prefer to temporarily deactivate this specific linter (and all the others that are likely to fail and long to correct), but I didn't find any way to deactivate this specific error.

By going into the code, I was able to restrict to a few linters, but I feel like I'm missing a lot of them.

spotless {
    kotlin {
        target("**/src/**/*.kt")
        ktlint()
        clearSteps()
        endWithNewline()
        indentWithSpaces(4)
        trimTrailingWhitespace()
    }
    kotlinGradle {
        target("**/*.gradle.kts")
        ktlint()
    }
}

So, I have two questions:

An additional question : is this the best linter ? Are there other options that would be better?


Solution

  • endWithNewline is one of the few general purpose steps of spotless, you can find more in FormatExtension docs.

    What you need is to suppress individual ktlint rules:

    ktlint()
    suppressLintsFor {
        step = "ktlint"
        shortCode = "standard:package-name"
    }
    suppressLintsFor {
        step = "ktlint"
        shortCode = "standard:no-wildcard-imports"
    }
    suppressLintsFor {
        step = "ktlint"
        shortCode = "standard:no-empty-file"
    }
    

    The one you need is standard:package-name. You can find more standard ktlint rules here. All rules from the standard and custom rule sets are enabled by default.

    There is also expiremental rule set, which can be enabled in .editorconfig file. The process of setting .editorconfig file is described in spotless docs. Default value is the .editorconfig file located in the top project. You can also set its path with setEditorConfigPath function or override it entirely with editorConfigOverride function.

    It is possible to suppress rules in editorconfig too, but some rules cannot be suppressed this way.

    spotless {
        kotlin {
            ktlint()
                .setEditorConfigPath("$projectDir/config/.editorconfig")  // sample unusual placement
                .editorConfigOverride(
                    mapOf(
                        "ktlint_experimental" to "enabled",  // enable a rule set
                        "ktlint_standard_filename" to "disabled",  // disable a rule
                    )
                )
        }
    }
    

    editorConfigOverride options will override what's supplied in .editorconfig file.