Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addKtlintCheckGitPreCommitHook always successfully even i broke the rule [pre-commit] #351

Closed
hardbobby opened this issue Apr 14, 2020 · 15 comments

Comments

@hardbobby
Copy link

when using addKtlintCheckGitPreCommitHook, my changed_files with broken rule still got successfully then use addKtlintFormatGitPreCommitHook facing still same issues
this is my build gradle on project level

buildscript {
    repositories {
        maven { url "https://plugins.gradle.org/m2/" }
 }

dependecies {
        classpath "org.jlleitschuh.gradle:ktlint-gradle:9.2.1"
 }
}
allprojects {
    apply plugin: "org.jlleitschuh.gradle.ktlint"
 ktlint {
        version = "0.35.0"
        debug = true
        verbose = true
        android = true
        outputToConsole = true
        outputColorName = "RED"
        enableExperimentalRules = false
    }
}

when using ktlintCheck my changed_files ignored.

is that any problem with your PinternalKtlintGitFilter ?

@Tapchicoma
Copy link
Collaborator

Is changed_files some source set? Or folder?

@hardbobby
Copy link
Author

Is changed_files some source set? Or folder?

No, I just run this task addKtlintCheckGitPreCommitHook and, generate pre-commit file then I run my pre-commit, and there is no error message even I broke the rule

@Tapchicoma
Copy link
Collaborator

Which OS are you using? Also note that this hook only checks git staged files. Changed, but non-staged files will be ignored.

@hardbobby
Copy link
Author

OS? Windows 10, yeah i only checked git staged files

@Tapchicoma
Copy link
Collaborator

Do you have a lot of staged files? Probably you are affected by this one: #326

@hardbobby
Copy link
Author

no, I only had 1 staged file and broke the rule

@Tapchicoma
Copy link
Collaborator

Also note Windows is not officially supported for git hook. You may look into this issue: #270

@Tapchicoma
Copy link
Collaborator

@hardbobby hi, have you solved your issue?

@hardbobby
Copy link
Author

@hardbobby hi, have you solved your issue?

not yet, because of my peripheral have memory issue to use Linux, ill try later, thanks mate

@ksyamkrishnan
Copy link

In Mac also I am facing the same issue @Tapchicoma. I broke rule only one file, but this is not showing any error or warning?
The following is the generated pre-commit.sh
`#!/bin/sh
set -e
######## KTLINT-GRADLE HOOK START ########

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

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

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

./public/trunk/gradlew -p ./public/trunk --quiet ktlintCheck -PinternalKtlintGitFilter="$CHANGED_FILES"
echo "$CHANGED_FILES"
echo "Completed ktlint run."

echo "$CHANGED_FILES" | while read -r file; do
if [ -f $file ]; then
git add $file
fi
done

echo "Completed ktlint hook."
######## KTLINT-GRADLE HOOK END ########
`

@Tapchicoma
Copy link
Collaborator

@ksyamkrishnan generated hook looks correct. Please open a new issue. If you will create a project reproduces your issue - this would be awesome.

@hardbobby if you still have this problem and could reproduce it - please add a new comment.

@fjr619
Copy link

fjr619 commented Oct 7, 2021

with latest version still got same problem, ktlintFormat not working on windows 10

@edwardmp
Copy link

edwardmp commented May 3, 2022

I have the same issue.

I noticed two things:

  1. If you use IntelliJ IDEA, by default it doesn't use the git staging area, hence this script won't work. You can search for the action enable git staging and enable it.
  2. It still didn't work perfectly, e.g. the linter would report errors but still allow me to commit. This was due to .githooks/pre-commit: line 21: =1: command not found, specifically this line: $gradleCommandExitCode=$?. Not sure what the real difference here is, but removing the dollar sign everywhere the variable is referenced solves it for me. Using the dollar sign syntax is also discouraged apparently: https://github.com/koalaman/shellcheck/wiki/SC1066

I'll try to create a PR for this.

@edwardmp
Copy link

edwardmp commented May 3, 2022

Issue is actually already described in #562 and fixing PR is merged. However this is not released yet.

@JLLeitschuh
Copy link
Owner

Should now be released:
https://github.com/JLLeitschuh/ktlint-gradle/releases/tag/v10.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants