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 153 forks source link

Execute with zsh shell (oh-my-zsh and SourceTree error) #92

Open tauren opened 7 years ago

tauren commented 7 years ago

I am using the zsh shell with oh-my-zsh. I also often use the SourceTree client. Lastly, I'm using react-boilerplate which uses pre-commit.

When I commit from the command line (zsh shell), everything works ok. But I just started getting errors when committing from inside the SourceTree client (which worked fine yesterday):

git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree commit -q -F /var/folders/wt/bj_21zvn7518qvf6bfxtt3w40000gn/T/SourceTreeTemp.IWP5H8 
/Users/tauren/.oh-my-zsh/oh-my-zsh.sh: line 12: autoload: command not found
/Users/tauren/.oh-my-zsh/oh-my-zsh.sh: line 31: syntax error near unexpected token `('
/Users/tauren/.oh-my-zsh/oh-my-zsh.sh: line 31: `for config_file ($ZSH/lib/*.zsh); do'
/Users/tauren/.zshrc: line 95: brew: command not found
/Users/tauren/.zshrc: line 95: /nvm.sh: No such file or directory
zsh-syntax-highlighting: error: not compatible with NO_FUNCTION_ARGZERO
./node_modules/pre-commit/hook: line 49: : command not found
./node_modules/pre-commit/hook: line 49: : command not found
Completed with errors, see above

I've been using this setup for quite some time without issues, and I'm not quite sure what changed. The most recent thing I did was blow away node_modules in my react-boilerplate project and run npm install again. I did update to the latest SourceTree a few days ago, but I'm pretty sure I've committed using it since then. I tried updating to pre-commit@1.2.2 from 1.1.3, but the same thing happens.

As with any oh-my-zsh setup, my ~/.zshrc file sources ~/.oh-my-zsh/oh-my-zsh.sh. This file executes autoload which is a ZSH shell command and blows up. It looks like pre-commit is running in a bash shell, so the command wouldn't be available.

But what I don't understand how it was ever working... This error just started, and my OMZ script has had autoload in it forever.

Is there something I can do to fix this? Can pre-commit take measures to run with the correct shell? Anything else I should do?

rosskevin commented 7 years ago

I have the same issue with zsh, though slightly different setup, I use tower. Recently I blew away all my node/npm/n installations, and reinstalled n.

./node_modules/pre-commit/hook: line 49: : command not found
./node_modules/pre-commit/hook: line 49: : command not found
./node_modules/pre-commit/hook: line 51: shell_session_update: command not found

This works fine, so I don't see why the hook is failing:

~ ❯❯❯ which node
/Users/kross/n/bin/node
~ ❯❯❯ node -v
v8.1.0

I can also execute the hook.

From in the project:

./node_modules/pre-commit/hook
No errors!

From outside the project (I expect problems, but it finds node):

 ❯❯❯ ./projects/ui/node_modules/pre-commit/hook
module.js:487
    throw err;
    ^

Error: Cannot find module 'pre-commit'
    at Function.Module._resolveFilename (module.js:485:15)
    at Function.resolve (internal/module.js:18:19)
    at [eval]:1:21
    at ContextifyScript.Script.runInThisContext (vm.js:44:33)
    at Object.runInThisContext (vm.js:116:38)
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (module.js:569:30)
    at evalScript (bootstrap_node.js:432:27)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:575:3

So, I don't have any idea why within tower it is having a problem. I can which node anywhere...

I added set -x and see that it cannot find node:

++ which node
++ which nodejs
++ which iojs
+ HAS_NODE=
+ [[ -z '' ]]
+ source_home_file .bash_profile
+ file=/Users/kross/.bash_profile
+ [[ -f /Users/kross/.bash_profile ]]
+ source /Users/kross/.bash_profile
++ [[ -s /Users/kross/.profile ]]
++ source /Users/kross/.profile

...

++ which node
+ NODE=
++ which nodejs
+ NODEJS=
++ which iojs
+ IOJS=
+ LOCAL=/usr/local/bin/node
+ BINARY=
+ [[ -n '' ]]
+ [[ -n '' ]]
+ [[ -n '' ]]
+ [[ -x /usr/local/bin/node ]]
+ [[ '' == *--dry-run* ]]
++ '' -e 'console.log(require.resolve('\''pre-commit'\''))'
./node_modules/pre-commit/hook: line 54: : command not found
+ '' ''
./node_modules/pre-commit/hook: line 54: : command not found
+ shell_session_update
./node_modules/pre-commit/hook: line 56: shell_session_update: command not found
rosskevin commented 7 years ago

@tauren I found my problem by adding set -x to the script. I had an old ~/.bash_profile lingering.
e.g.

+ source_home_file .bash_profile
+ file=/Users/kross/.bash_profile
+ [[ -f /Users/kross/.bash_profile ]]
+ source /Users/kross/.bash_profile
++ [[ -s /Users/kross/.profile ]]
++ source /Users/kross/.profile

The moment I rm ~/.bash_profile, tower executed properly.

y0hnn commented 7 years ago

I had the issue for days, and found out that I did not have the path for node bin set in my .zshrc. I just needed to add : export PATH="/usr/local/bin:$PATH"

tristanbbq commented 6 years ago

Had same issue with sourcetree where pre-commit hook would fail and prevent me from committing in the client. Added /usr/local/bin to path as mentionned by @yohannprigent and it fixed it. thanks!

buglovebug commented 5 years ago

I just encountered the same, the reason is default shell not zsh, use chsh -s to change default shell to zsh

uzprocode commented 1 year ago

▶ whereis brew brew: /opt/homebrew/bin/brew /opt/homebrew/share/man/man1/brew.1

I just needed to add : export PATH="/opt/homebrew/bin:$PATH" in my '.zshrc'.