pinterest / ktlint

An anti-bikeshedding Kotlin linter with built-in formatter
https://pinterest.github.io/ktlint/
MIT License
6.07k stars 504 forks source link

CLI argument for file reporting broken in Ktlint 1.2.0 #2576

Closed ccjernigan closed 4 months ago

ccjernigan commented 4 months ago

Expected Behavior

Using the CLI, generating a ktlint report to a checkstyle formatted file should succeed.

Observed Behavior

15:29:57.715 [main] ERROR com.pinterest.ktlint.cli.internal.ReporterAggregator -- reporter "output=build/reports/ktlint/ktlint.xml" wasn't found (available: baseline, checkstyle, format, html, json, plain, plain-summary, sarif)

Looking at the error, it suggests the command line parser has dropped the report type. This is a regression in 1.2.0.

Steps to Reproduce

Invoke the ktlint CLI with the arguments --reporter=checkstyle,output=${path}

See custom ktlint gradle convention script below.

Your Environment

plugins {
    java
}

val ktlint by configurations.creating

dependencies {
    ktlint(
        versionCatalogs
        .named("libs")
        .findLibrary("ktlint")
        .get()
    ) {
        attributes {
            attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named<Bundling>(Bundling.EXTERNAL))
        }
    }
}

tasks {
    val editorConfigFile = rootProject.file(".editorconfig")
    val outputDir = File(File(layout.buildDirectory.asFile.get(), "reports"), "ktlint")
    val outputFile = File(outputDir, "ktlint.xml")
    val ktlintArgs = listOf(
        "--reporter=plain",
        "--reporter=checkstyle,output=${outputFile.path}",
        "**/src/**/*.kt",
        "!**/build/**.kt",
        "--editorconfig=$editorConfigFile"
    )

    register("ktlint", org.gradle.api.tasks.JavaExec::class) {
        description = "Analyze code style with ktlint."
        classpath = ktlint
        mainClass = "com.pinterest.ktlint.Main"
        args = ktlintArgs
    }
}
lorenz-jumio commented 4 months ago

Experiencing the same issue (was about to open it too, thanks @ccjernigan 🙂)

From looking at the code I suspect line 168 in KtlintCommandLine.kt to be the problem: split(",") would put output=... as a own reporter configuration, leading to the error down the line in ReporterAggregator

FloEdelmann commented 4 months ago

For reference, the error was probably introduced in

paul-dingemans commented 4 months ago

Experiencing the same issue (was about to open it too, thanks @ccjernigan 🙂)

From looking at the code I suspect line 168 in KtlintCommandLine.kt to be the problem: split(",") would put output=... as a own reporter configuration, leading to the error down the line in ReporterAggregator

You're right that this is part of the problem. I made a mistake while migrating to clikt. The reporterConfiguration has to be a list because multiple reporters can be specified:

ktlint --reporter=plain --reporter=checkstyle,output=ktlint-report-in-checkstyle-format.xml

I have assumed that the list of reporters would be separated by comma's. But the comma is indeeded needed for parsing the different elements in a reporter configuration. Apparently this was not covered by the unit tests.