JLLeitschuh / ktlint-gradle

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

addKtlintFormatGitPreCommitHook failed #511

Open stMayhem opened 3 years ago

stMayhem commented 3 years ago

After running ./gradlew addKtlintFormatGitPreCommitHook I'm getting this error:

Execution failed for task ':buildSrc:generateExternalPluginSpecBuilders'.
> Could not resolve all files for configuration ':buildSrc:compileClasspath'.
   > Could not find org.jlleitschuh.gradle:ktlint-idea:10.1.0.
     Searched in the following locations:
       - https://dl.google.com/dl/android/maven2/org/jlleitschuh/gradle/ktlint-idea/10.1.0/ktlint-idea-10.1.0.pom
       - https://repo.maven.apache.org/maven2/org/jlleitschuh/gradle/ktlint-idea/10.1.0/ktlint-idea-10.1.0.pom
       - https://jcenter.bintray.com/org/jlleitschuh/gradle/ktlint-idea/10.1.0/ktlint-idea-10.1.0.pom
       - https://plugins.gradle.org/m2/org/jlleitschuh/gradle/ktlint-idea/10.1.0/ktlint-idea-10.1.0.pom
     Required by:
         project :buildSrc

This is my build.gradle.kts file in buildSrc folder:

plugins {
    `kotlin-dsl`
    `kotlin-dsl-precompiled-script-plugins`
}

repositories {
    google()
    mavenCentral()
    jcenter()
    maven("https://plugins.gradle.org/m2/")
}

dependencies {
    implementation("com.android.tools.build:gradle:4.1.0")
    implementation(kotlin("gradle-plugin", "1.4.10"))
    implementation("org.jlleitschuh.gradle:ktlint-gradle:10.1.0")
    implementation("org.jlleitschuh.gradle:ktlint-idea:10.1.0")
}

Main plugin ktlint-gradle:10.1.0 works great.

Tapchicoma commented 3 years ago

ktlint-idea 10.1.0 should be downloadable from Gradle plugins.

Will it possible to provide repro project?

stMayhem commented 3 years ago

[Archive.zip] I created example project with this error. To reproduce it is required to uncomment the marked line.

stMayhem commented 3 years ago

@Tapchicoma Hello. Have you checked the code?

Tapchicoma commented 3 years ago

@stMayhem in the provided archive.zip I see only some non-related screenshots :thinking:

stMayhem commented 3 years ago

my bad, I don't know how it could have happened...here is the correct archive: https://drive.google.com/file/d/1K66laEgYJC-_GdbnFD6FR_F_3pFrYyPN/view?usp=sharing

Tapchicoma commented 3 years ago

You just need to add into root project build.gradle.kts following lines:

plugins {
    id("org.jlleitschuh.gradle.ktlint")
    id("org.jlleitschuh.gradle.ktlint-idea")
}

And in buildSrc/build.gradle.kts you could remove this one:

    //implementation("org.jlleitschuh.gradle:ktlint-idea:10.1.0")

When you are adding plugin to buildSrc module dependencies, you need only this artifact: implementation("org.jlleitschuh.gradle:ktlint-gradle:10.1.0")

stMayhem commented 3 years ago

Yes, now everything works, thanks a lot. But after calling the git hook, files with incorrect formatting are not added to the commit. Is this the expected behavior? I use git commit -m "", correct formating files are commited, incorrect formatting files are not staged for commit.

Tapchicoma commented 3 years ago

Git hook only checks staged files. So if your unstaged files have some code style violations - they will not be formatted.

stMayhem commented 3 years ago

yes, I use git add . before commit, all files were staged before hook, but after the commit it are not staged again

stMayhem commented 3 years ago

I figured it out, the script doesn't add files in which the format has changed to the wrong one and there have been no other changes. If there were other changes in the file, then everything is ok.