ktlint not checking kotlin file
Asked Answered
B

2

5

I want my project to perform ktlintCheck on all kotlin file, but it only check on build.gradle.kts file. build.gradle.kts file as below

ktlint {
    version.set("9.4.0")
    debug.set(true)
    verbose.set(true)
    android.set(false)
    outputToConsole.set(true)
    reporters {
        reporter(ReporterType.PLAIN)
        reporter(ReporterType.CHECKSTYLE)
    }
    ignoreFailures.set(false)
    kotlinScriptAdditionalPaths {
        include(fileTree("src/"))
    }
    filter {
        exclude("**/generated/**")
        include("**/kotlin/**")
    }
}
subprojects {
    apply(plugin = "org.jlleitschuh.gradle.ktlint")
    ktlint {
        debug.set(true)
    }
}

When I run gradlew ktlintCheck, the Terminal output as below:

gradlew ktlintCheck

> Task :ktlintKotlinScriptCheck
[DEBUG] Discovered ruleset with " standard" id.
[DEBUG] Discovered reporter with "checkstyle" id.
[DEBUG] Discovered reporter with "json" id.
[DEBUG] Discovered reporter with "html" id.
[DEBUG] Discovered reporter with "plain" id.
[DEBUG] Initializing "plain" reporter with {verbose=true, color=true, color_name=DARK_GRAY}
[DEBUG] Initializing "plain" reporter with {verbose=true, color=true, color_name=DARK_GRAY}, output=C:\Code\XXXX\build\reports\ktlint\ktlintKotlinScriptCheck\ktlintKotlinScriptCheck.txt
[DEBUG] Initializing "checkstyle" reporter with {verbose=true, color=true, color_name=DARK_GRAY}, output=C:\Code\XXXX\build\reports\ktlint\ktlintKotlinScriptCheck\ktlintKotlinScriptCheck.xml
[DEBUG] Checking C:\Code\XXXX\build.gradle.kts
Resolving .editorconfig files for C:\Code\XXXX\build.gradle.kts file path
[DEBUG] 809ms / 1 file(s) / 0 error(s)

Beg answered 18/9, 2020 at 2:50 Comment(2)
What is you project structure?Microwatt
project structure like this: \project\src\main\kotlin\com\abc\tara\api\UserController.ktBeg
O
7

I faced a similar issue, and I found a solution that worked for me. You can try adding the following configuration to your build.gradle file in the module:

android {
// Your existing android configuration

sourceSets {
    getByName("main") {
        java.srcDir("src/main/kotlin")
    }
}

By adding this configuration inside the android block, you're telling Gradle to treat the src/main/kotlin directory as a Java source directory as well. This way, ktlint can properly analyze the Kotlin files in that directory.

Make sure to sync your project after adding this configuration.

Ornithic answered 20/4, 2023 at 16:8 Comment(0)
C
1

To begin there is an error in the configuration of your build.gradle file, in the block Ktlint settings, the line where it appears "version.set (" 9.4.0 ")" is incorrect as well as unnecessary. In any case, if you still decide to use this setting, it should be, for example, version.set ("0.37.2") , since it refers to the Ktlint version and not the jlleitschuh / ktlint-gradle plugin

To solve the problem make the following modifications to the build.gradle.kts file (I use Gradle 6.6.1 with Kotlin DSL in Intellij IDEA community Edition)

Please replace the plugin with the previous version (9.3.0):

build.gradle.kts file in root project

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType // for Ktlint reports 

plugins {
    kotlin("jvm") version "1.4.10"  // Kotlin Compiler
    id("org.jetbrains.dokka") version "1.4.10" // Documentation Engine For Kotlin
    id("org.jlleitschuh.gradle.ktlint") version "9.3.0" // Kotlin Linter
}

group = "my group" // Replace with your group
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    implementation("org.junit.jupiter:junit-jupiter:5.4.2")
    testImplementation(kotlin("test-junit5"))
    implementation(kotlin("stdlib-jdk8"))
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "1.8"
}

tasks.test {
    useJUnitPlatform()
}

val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
    jvmTarget = "1.8"
}

val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
    jvmTarget = "1.8"
}

ktlint {
    // THIS LINE IS NOT necessary and is incorrect -> version.set("9.4.0") 
    verbose.set(true)
    outputToConsole.set(true)
    coloredOutput.set(true)
    debug.set(false) // in your configuration this option must be set to true
    android.set(false)
    outputColorName.set("RED")
    ignoreFailures.set(false)
    enableExperimentalRules.set(false)
    reporters {
        reporter(ReporterType.CHECKSTYLE)
        reporter(ReporterType.JSON)
        reporter(ReporterType.HTML)
    }
    filter {
        exclude("**/style-violations.kt")
        exclude("**/generated/**")
        include("**/kotlin/**")
    }
}
Corpuscle answered 29/9, 2020 at 1:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.