kotlinter-gradle icon indicating copy to clipboard operation
kotlinter-gradle copied to clipboard

Gradlew not found because of outdated path in pre-push hook

Open xtyrrell opened this issue 2 years ago • 5 comments

When git pushing, with the pre-push hook installed, I got this error:

.git/hooks/pre-push: line 6: /Users/max/workspace/project-2/gradlew: No such file or directory

lintKotlin found problems, running formatKotlin; commit the result and re-push
.git/hooks/pre-push: line 8: /Users/max/workspace/project-2/gradlew: No such file or directory
error: failed to push some refs to 'gitlab.com:exampleorg/examplegroup/project.git'

I looked in the pre-push hook code and it has:

GRADLEW=/Users/max/workspace/project-2/gradlew

The error seems to be because I moved my project to a different directory, so this path is outdated and wrong now.

Hopefully this Google-searchers.

xtyrrell avatar Jul 04 '22 15:07 xtyrrell

Is there any appetite to make this path less brittle?

One idea would be to expect gradlew to be at ../../gradlew. This would make it look for gradlew next to the .git the hook is installed in, which may or may not be a good assumption.

xtyrrell avatar Jul 04 '22 15:07 xtyrrell

After thinking about this a bit more, I don't think that's necessary; I think that just replaces one brittle path with another.

I think the best way to handle this would be to, in the git hook script, check that the gradlew file exists before trying executing it. If not, print a clear and actionable explanation of what is going wrong and a recommended step to fix it. I would say the first step should be for the user to re-install the git hook (which would fix the hook if you've moved your project to another location) and, if that doesn't work, to open an issue here.

That would at least unblock users who cannot push because the hook broke.

We could also think about re-running installKotlinterPrePushHook task every time some other tasks run, so that if you, for example, run the build before the git hook runs, it would fix itself before you have a chance to see the error message. But I think we should start with the first solution.

xtyrrell avatar Jul 05 '22 10:07 xtyrrell

Good suggestions. I agree a clear error message would be a good start.

jeremymailen avatar Aug 02 '22 07:08 jeremymailen

Another suggestion, why not leave gradlew variable as ./gradlew.

Here's the broken hook on windows, because i have a space in my folders name:

#!/bin/sh
set -e
##### KOTLINTER HOOK START #####
##### KOTLINTER 3.13.0 #####
GRADLEW=D:/all/work-nagarro/Client Projects/CapacityEstimationService/gradlew.bat
if ! $GRADLEW lintKotlin ; then
    echo 1>&2 "\nlintKotlin found problems, running formatKotlin; commit the result and re-push"
    $GRADLEW formatKotlin
    exit 1
fi
##### KOTLINTER HOOK END #####

Here's how it works for me:

#!/bin/sh
set -e
##### KOTLINTER HOOK START #####
##### KOTLINTER 3.13.0 #####
GRADLEW=./gradlew
if ! $GRADLEW lintKotlin ; then
    echo 1>&2 "\nlintKotlin found problems, running formatKotlin; commit the result and re-push"
    $GRADLEW formatKotlin
    exit 1
fi
##### KOTLINTER HOOK END #####

I believe the change should be made to this file: https://github.com/jeremymailen/kotlinter-gradle/blob/6ab22c96603f2e1752b2e9d971bf9758f64caa33/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/GitHookTasks.kt.

Would you accept a MR?

TheBestPessimist avatar Feb 03 '23 10:02 TheBestPessimist

the problem would be for those multimodule projects where gradlew and .git folder are not the same. A more robust solution is needed, I am afraid

mostrovoi avatar Mar 27 '23 15:03 mostrovoi