ktlint-gradle icon indicating copy to clipboard operation
ktlint-gradle copied to clipboard

Automatically lint on build

Open Pearcekieser opened this issue 3 years ago • 2 comments

For a demo project, I wanted to automatically lint and correct issues as a part of the build process.

// auto lint on build
listOf("ktlintKotlinScriptCheck", "ktlintMainSourceSetCheck", "ktlintTestSourceSetCheck").forEach {
    tasks.getByName(it) {
        dependsOn("ktlintFormat")
    }
}

Expected behavior: code is automatically formatted on prior to check so check only fails on non-auto fixable issues.

Actual behavior: If I build the project with a lint error, the first build will fail, but if I run build a second time it succeeds. It seems like "ktlintFormat" is getting run on the first build but the Check step is not evaluating against the updated files on the first build.

I think I should probably change to a solution that uses git integration or the IDE to automatically preform formatting on save rather than rely on the build to format the code. I will probably transition to that approach, but I want to root cause this issue and potentially fix it to improve my understanding of gradle and the ktlint plugin.

Thanks for the help

Pearcekieser avatar Dec 28 '21 16:12 Pearcekieser

If I'm understanding what you're saying here. With the logic you have above, if you have a lint error, the first time you run your build's check task, it fails. The second time it passes. That does seem unusual and strange. Could you provide more details like the version of Gradle and potentially a simple reproducer project?

JLLeitschuh avatar Jan 03 '22 23:01 JLLeitschuh

I did some more testing and set up a sample project.

I'm running version 7.2 but I also tested 7.3.3. I set it up Pearcekieser/ktlint-sample. You can use git reset --hard to reintroduce the ktlint error after a lint fix. Under 7.3.3 I see a failure right after a clean, but subsequent builds pass even if its just one build git reset --hard ; .\gradlew build but after a clean it takes a check then a build to pass git reset --hard ; .\gradlew clean; .\gradlew check; .\gradlew build.

Under 7.2 it always requires check before a build even if the build is not right after a clean: gradle clean; git reset --hard ; gradle build ; git reset --hard ; gradle build fails with 7.2 but passes on 7.3.3

setup

git clone ...
cd ...
gradle wrapper --gradle-version 7.3.3 --distribution-type all

(status of last build) -> fails

.\gradlew clean ; git reset --hard ; .\gradlew build 

git reset --hard ; .\gradlew clean; .\gradlew build 

(status of last build) -> pass

git reset --hard ; .\gradlew clean; .\gradlew build ; .\gradlew build

git reset --hard ; .\gradlew clean; .\gradlew check ; .\gradlew build

git reset --hard ; .\gradlew clean; .\gradlew build ; git reset --hard ; .\gradlew build

<after one of the failed builds> ; git reset --hard ; .\gradlew build 

Under 7.2 I see a different behavior:

(status of last build) -> fails

git reset --hard ; gradle build 

git reset --hard ; gradle clean; gradle build 

gradle clean ; git reset --hard ;  gradle build 

(status of last build) -> passed on 7.3.3 but fail with 7.2

gradle clean; git reset --hard ; gradle build ; git reset --hard ; gradle build 

(status of last build) -> pass

git reset --hard ; gradle clean; gradle check ; gradle build
git reset --hard ; gradle clean; gradle build ; gradle build

My version info:

------------------------------------------------------------
Gradle 7.2
------------------------------------------------------------

Build time:   2021-08-17 09:59:03 UTC
Revision:     a773786b58bb28710e3dc96c4d1a7063628952ad

Kotlin:       1.5.21
Groovy:       3.0.8
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          11.0.10 (Amazon.com Inc. 11.0.10+9-LTS)
OS:           Windows 10 10.0 amd64

Pearcekieser avatar Jan 07 '22 05:01 Pearcekieser

This was fixed in 11.4.0

wakingrufus avatar Aug 07 '23 19:08 wakingrufus