Task 'ktlintCheck' not found in project

2.2k Views Asked by At

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.

  • I can run the ktlintCheck from Gradle window in Android studio.

  • I can run the task from command line gradlew ktlintCheck

  • If i substitute the ktlintCheck with signingReport (or any other task from android plugin) in pre-commit script I can run it as well.

  • BUT, I cannot run the ktlintCheck and ktlintFormat from pre-commit script and I receive this error:

    Commit failed with error 0 file committed, 1 file failed to commit: Reformat 1 Running ktlint over these files: app/src/main/java/com/example/codestyleexpetiment/MainActivity.kt

             FAILURE: Build failed with an exception.
    
             * What went wrong:
             Task 'ktlintCheck' not found in project ':app'.
    
             * Try:
             Run gradlew tasks to get a list of available tasks. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    
             * Exception is:
             org.gradle.execution.TaskSelectionException: Task 'ktlintCheck' not found in project ':app'.
             at org.gradle.execution.DefaultTaskSelector.getSelection(DefaultTaskSelector.java:113)
             at org.gradle.execution.DefaultTaskSelector.getSelection(DefaultTaskSelector.java:78)
             at org.gradle.execution.CompositeAwareTaskSelector.getSelection(CompositeAwareTaskSelector.java:93)
             at org.gradle.execution.commandline.CommandLineTaskParser.parseTasks(CommandLineTaskParser.java:43)
             at org.gradle.execution.TaskNameResolvingBuildConfigurationAction.configure(TaskNameResolvingB... (show balloon)
    

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?

1

There are 1 best solutions below

0
On

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.