Skip to content

Husky 9 seems to setup .git/config hooksPath incorrectly for Windows #1345

@RoystonS

Description

@RoystonS

Context

  • Terminal is Windows Command Prompt, using Git for Windows
  • Using husky 9.0.2

When running husky install with v8, the .git/config file was correctly modified with:

hooksPath = .husky

When running husky with v9, the .git/config is being modified - on Windows - with:

hooksPath = .husky\\_

(Note the escaped backslash.)

The result is that none of the hooks actually run. It looks like this path should be .husky/_ instead, even on Windows, because Git, when processing these paths, uses Linux forward-slash separators.

Demo / repro

With a brand new repository, installing husky 9 and running husky init gives me a demonstration pre-commit hook containing npm test:

C:\Dev>mkdir huskyplay

C:\Dev>cd huskyplay

C:\Dev\huskyplay>git init
Initialized empty Git repository in C:/Dev/huskyplay/.git/

C:\Dev\huskyplay>npm init
This utility will walk you through creating a package.json file.
...<npm init output elided>...
C:\Dev\huskyplay>npm i -D husky

added 1 package, and audited 2 packages in 1s

1 package is looking for funding
  run `npm fund` for details

found 0 vulnerabilities

C:\Dev\huskyplay>npx husky init

C:\Dev\huskyplay>git add package.json
warning: in the working copy of 'package.json', LF will be replaced by CRLF the next time Git touches it

C:\Dev\huskyplay>git commit -m "A commit"
[master (root-commit) 1c74e28] A commit
 1 file changed, 15 insertions(+)
 create mode 100644 package.json

That should have failed, because husky init installs a demonstration pre-commit hook that calls npm test, and by default npm test fails because of the default value of "test": "echo \"Error: no test specified\" && exit 1",.

This shows that the hooks aren't running.

If I modify .git/config to read hooksPath = husky/_ instead of hooksPath = husky\\_, and then try again:


C:\Dev\huskyplay>git add package-lock.json
warning: in the working copy of 'package-lock.json', LF will be replaced by CRLF the next time Git touches it

C:\Dev\huskyplay>git commit -m "Another commit"

> [email protected] test
> echo "Error: no test specified" && exit 1

"Error: no test specified"
husky - pre-commit script failed (code 1)

This time we see the expected hook fail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions