observing / pre-commit

Automatically installs a git pre-commit script in your git repository which runs your `npm test` on pre-commit
MIT License
1.88k stars 152 forks source link

Problems on Windows - Help! No such file or directory 2: ./node_modules/pre-commit/hook : integer expression expected: [: 127 : numeric argument required 5: #102

Open replete opened 7 years ago

replete commented 7 years ago

I've been pulling my hair out trying to make sense of this for someone I'm working with who is having this problem. It's breaking their workflow.

No such file or directory 2: ./node_modules/pre-commit/hook
: integer expression expected: [: 127
: numeric argument required 5:

I've tried Windows machines near me, and VMs and don't have the problem.

This happens in cmd, powershell, MinGW, WebStorm.

Any ideas?

dajaj commented 7 years ago

Same problem with babun on Windows 7, I got this when I try to commit :

: No such file or directory     .git/hooks/pre-commit: line 2: ./node_modules/pre-commit/hook
: integer expression expected   .git/hooks/pre-commit: line 4: [: 127
: numeric argument required     .git/hooks/pre-commit: line 5: exit: 0

Hope we'll find a solution

codepunkt commented 6 years ago

Same here on Windows 10 in WSL Bash

hbowden commented 6 years ago

Same on Windows 7

neverendingqs commented 6 years ago

If you open up .git/hooks/pre-commit, you can see that the code is written in bash:

#!/bin/bash
./node_modules/pre-commit/hook
RESULT=$?
[ $RESULT -ne 0 ] && exit 1
exit 0

Interestingly enough, I get the same message in Cygwin, but I don't in Git bash's mingw64.

In any case, the problem is probably something something doesn't understand bash.

Cygwin:

$ uname -r
2.10.0(0.325/5/3)

$ git --version
git version 2.16.2

Git bash:

$ git --version
git version 2.5.0.windows.1
neverendingqs commented 6 years ago

EDIT: this is a red herring. See next comment for details.

Update: looks like .git/hooks/pre-commit was not happy about the line ending. It had \r\n (Windows). Switching to \n got me further.

EDIT: looks like it purposely chooses Windows line ending (https://github.com/observing/pre-commit/blob/f25888ffc67d21beaa1572b53ab6704a67266b64/install.js#L104-L108). Maybe it shouldn't(?).

I suspect the problems I am encountering are specific to Cygwin, so if you're not using Cygwin, give the above a shot.

neverendingqs commented 6 years ago

This is the full error after changing the line endings:

> git commit -m "test"
/cygdrive/c/Program Files/nodejs/node
pre-commit:
pre-commit: Received an error while parsing or locating the `package.json` file:
pre-commit:
pre-commit:   Cannot find module '\cygdrive\c\path\to\package.json'
pre-commit:
pre-commit: Skipping the pre-commit hook.
pre-commit:

It looks like it's failing on https://github.com/observing/pre-commit/blob/f25888ffc67d21beaa1572b53ab6704a67266b64/hook#L49.

I am using Cygwin Git with a Windows Node installation. This makes sense to me, since Windows Node probably doesn't understand a non-Windows path.

Using "C:\Program Files\Git\bin\git" commit -m "test" works for me, and surprisingly, with pre-commit containing either line ending. Looks like Windows Git with Windows Node installation works out-of-the-box.

Hopefully this helps!

rofrol commented 6 years ago

In node_modules/pre-commit/index.js change line

this.json = require(path.join(this.root, 'package.json'));

to

this.json = require(path.join(this.root, 'package.json').replace(/\\([a-zA-Z])\\/, '$1:\\'));

This will change \c\Users\rofrol\project1\package.json to c:\Users\rofrol\project1\package.json.