JLLeitschuh / ktlint-gradle

A ktlint gradle plugin
MIT License
1.48k stars 163 forks source link

org.gradle.execution.TaskSelectionException: Task 'ktlintCheck' not found in project #583

Open BILLyTheLiTTle opened 2 years ago

BILLyTheLiTTle commented 2 years ago

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.

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 ########

Am I missing something?

INGUANA commented 2 years ago

I am having the same issue.

rpanagakos commented 2 years ago

Same situation here.

cliffgr commented 2 years ago

I have the same issue too

JLLeitschuh commented 2 years ago

Has this only become an issue since updating, or did you have this problem with 10.2.1?

BILLyTheLiTTle commented 2 years ago

This is my first attempt with ktlint. Unfortunately, I have no feedback from previous versions!

pvtan commented 2 years ago

Same issue too in 10.3.0, setting up for the first time. Tried going back to previous versions. Version10.0.0 worked for me.

RomainBonneEP commented 2 months ago

Same issue for me on version 11 and it's work too on version 10.0.0