alirezanet / Husky.Net

Git hooks made easy with Husky.Net internal task runner! 🐶 It brings the dev-dependency concept to the .NET world!
https://alirezanet.github.io/Husky.Net/
MIT License
684 stars 31 forks source link

fatal: cannot run .husky/pre-commit: No such file or directory on Mac-os #84

Closed Filiphasan closed 1 year ago

Filiphasan commented 1 year ago

Version

0.5.4

Details

Hi, i have an issue for v0.5.4, my project already has pre-commit file in .husky folder image

My pre-commit file is

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

#Run the command group in .husky/task-runner.json
dotnet husky run --group pre-commit

#Allow Committing
exit 0

My task-runner.json file is

{
  "tasks": [
    {
      "name": "dotnet-format",
      "group": "pre-commit",
      "command": "dotnet",
      "args": ["dotnet-format", "--include", "${staged}"],
      "include": ["**/*.cs", "**/*.vb"]
    }
  ]
}

My .confif/dotnet-tool.json file is

{
  "version": 1,
  "isRoot": true,
  "tools": {
    "husky": {
      "version": "0.5.4",
      "commands": [
        "husky"
      ]
    },
    "dotnet-format": {
      "version": "5.1.250801",
      "commands": [
        "dotnet-format"
      ]
    }
  }
}

Steps to reproduce

It gives this error in different situations sometimes as well. For instance, I clone the project from scratch and then face this issue on a branch with Husky already installed. As far as I can understand, I encounter this error when changing branches locally.

alirezanet commented 1 year ago

Hi @Filiphasan, Have you attached husky to one your projects? if not if you switch to a repo/branch that doesn't have all the husky files, you might encounter similar errors. or just check if you manually install husky again, would it fix the problem? I tried to reproduce the error but it is working on my two machines.

Filiphasan commented 1 year ago

Hi @alirezanet

I manually configured the Husky for my project with the following tag in my .csproj file. I am experiencing the issue on macOS. When I tried it on Windows, I didn't encounter any problems. Thank you for your support.

<Target Name="husky" AfterTargets="Build" Condition="'$(HUSKY)' != 0">
        <Exec Command="dotnet tool restore" WorkingDirectory="../" />
        <Exec Command="dotnet husky install" WorkingDirectory="../" />
</Target>
alirezanet commented 1 year ago

Oh Unfortunately at the moment I only can test it in Linux/Windows operating systems, can you please verify if your git pre-commit hook have required permissions and it is executable on mac after installing husky? git hooks should have +x permission

Filiphasan commented 1 year ago

Yes, i have already +x permission

ls -l .husky/pre-commit                                
-rwxr-xr-x@ 1 hasan.erdal  staff  162 Aug 25 11:20 .husky/pre-commit
alirezanet commented 1 year ago

well normally you have it, but I mean check it when you've faced the error... since the error message is:

fatal: cannot run .husky/pre-commit: No such file or directory

I think either the hook is missing for some reason or the hook doesn't have permission.

Filiphasan commented 1 year ago

Hi @alirezanet,

I found the cause of my issue. My project has .gitattributes file for end-of-line settings and in this file there is "* text eol=crlf" settings for all files.

My old .gitattributes file

# Set end-of-line to CRLF for All files.
* text eol=crlf

And now

# Set end-of-line to CRLF for All files.
* text eol=crlf

# Exclude files or folders
.husky/** -text

This changes has solved my "fatal: cannot run .husky/pre-commit: No such file or directory" issues.

Thanks for your support

alirezanet commented 1 year ago

Hi @Filiphasan, thank you for letting me know, happy that you've managed to solve the problem. 👌