LiTTle
LiTTle

Reputation: 1881

Task 'ktlintCheck' not found in project

I have a sample project. The build.gradle (project) is:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.1.3' apply false
    id 'com.android.library' version '7.1.3' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
    id "org.jlleitschuh.gradle.ktlint" version '10.3.0' apply true
}

subprojects {
    apply plugin: "org.jlleitschuh.gradle.ktlint" // Version should be inherited from parent

    // Optionally configure plugin
    ktlint {
        debug = true
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

The build.gradle (app) is:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id "org.jlleitschuh.gradle.ktlint"
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.codestyleexpetiment"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

ktlint {
    version = "0.45.2"
}
dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

I have installed the pre-commit hook successfully.

The 4 below attempts are working the same way even if use app:ktlintCheck.

The pre-commit script is:

#!/bin/sh
######## KTLINT-GRADLE HOOK START ########

CHANGED_FILES="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $NF ~ /\.kts?$/ { print $NF }')"

if [ -z "$CHANGED_FILES" ]; then
    echo "No Kotlin staged files."
    exit 0
fi;

echo "Running ktlint over these files:"
echo "$CHANGED_FILES"

diff=.git/unstaged-ktlint-git-hook.diff
git diff --color=never > $diff
if [ -s $diff ]; then
  git apply -R $diff
fi

./gradlew --stacktrace ktlintCheck -PinternalKtlintGitFilter="$CHANGED_FILES"
gradle_command_exit_code=$?

echo "Completed ktlint run."


if [ -s $diff ]; then
  git apply --ignore-whitespace $diff
fi
rm $diff
unset diff

echo "Completed ktlint hook."
exit $gradle_command_exit_code
######## KTLINT-GRADLE HOOK END ########

The library/plugin I used is this. Am I missing/forgetting something?

Upvotes: 5

Views: 2895

Answers (1)

dmaixner
dmaixner

Reputation: 874

This happened to me as well. The reason was, that in pre-commit script there is this line:

git apply -R $diff

This line removes all non-staged files. In my case it also reverted build.gradle file, where ktlint plugin was applied (and hence task ktlintCheck registered).

Simple solution was just to stage all changes (or at least the build.gradle) before calling pre-commit hook:

git add .

Or commit build.gradle file first.

Upvotes: 0

Related Questions