Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong scriptPath when installing husky through hook ran via git GUI #606

Closed
giovannipiller opened this issue Nov 13, 2019 · 5 comments
Closed

Comments

@giovannipiller
Copy link

giovannipiller commented Nov 13, 2019

Hi there! 😁

We have a hook that performs a npm ci (or npm install) if package-lock.json changes during a merge or checkout. This works flawlessly when we run git straight from the Terminal (ex. git checkout develop), but has issues when the hook is triggered from a GUI.

Interestingly, this issue doesn't happen on 2.7.0, but it does on pretty much any 3.x.
Looks like it could be related to the use of git rev-parse.

git rev-parse --show-toplevel --git-common-dir seems to produce different results:

  • from npm ci via GUI:
    • toplevel: /Users/myuser/development/myproject/node_modules/husky
    • common-dir: /Users/myuser/development/myproject/.git
  • from npm ci via Terminal:
    • toplevel: /Users/myuser/development/myproject (same as pwd)
    • common-dir: ../../.git

This results in .git/hooks/* created with a wrong scriptPath value when executed via GUI, which causes hooks to not be executed unless they're reinstalled via Terminal. See output below.

Any idea on how we can workaround this issue?

From GUI (GIT-Tower)

As many Git GUI do, Git-Tower doesn't run in the same environment as the Terminal.

Debugger during husky installation

> husky@3.0.8 install /Users/myuser/development/myproject/node_modules/husky
> node husky install

husky:debug Current working directory is '/Users/myuser/development/myproject/node_modules/husky'
husky:debug INIT_CWD environment variable is set to '/Users/myuser/development/myproject'
husky > Setting up git hooks
husky:debug GIT_DIR environment variable is set to '/Users/myuser/development/myproject/.git'
husky:debug If you're getting "fatal: not a git repository" errors, you may want to unset GIT_DIR
husky:debug Git rev-parse command returned:
husky:debug   --show-top-level: /Users/myuser/development/myproject/node_modules/husky
husky:debug   --git-common-dir: /Users/myuser/development/myproject/.git
husky:debug Installing hooks in '/Users/myuser/development/myproject/.git/hooks'
husky > Done

Hook result

Excerpt from .git/hooks/pre-commit

[...]
scriptPath="run.js"
[...]

❌ hooks are not executed


From terminal

Debugger during husky installation

> husky@3.0.8 install /Users/myuser/development/myproject/node_modules/husky
> node husky install

husky:debug Current working directory is '/Users/myuser/development/myproject/node_modules/husky'
husky:debug INIT_CWD environment variable is set to '/Users/myuser/development/myproject'
husky > Setting up git hooks
husky:debug Git rev-parse command returned:
husky:debug   --show-top-level: /Users/myuser/development/myproject
husky:debug   --git-common-dir: ../../.git
husky:debug Installing hooks in '../../.git/hooks'
husky > Done

Hook result

Excerpt from .git/hooks/pre-commit

[...]
scriptPath="node_modules/husky/run.js"
[...]

✅ hooks work as expected

System info

  • macOS 10.14.6
  • git version 2.21.0 (Apple Git-122)
  • node v12.13.0
  • npm 6.13.0
@typicode
Copy link
Owner

Hi @giovannipiller,

Thanks for the bug report. Could you try with husky@next?

@giovannipiller
Copy link
Author

After installing husky@next, I'm able to run hooks from Terminal (ex. git commit -m "test" triggers a hook), but they never run when committing from GUI.

Looks like npx is not available from GIT Tower environment.
Tomorrow I'm going to investigate a bit more.

Failed hooks via GUI

Here's the log during failed commit hooks via GUI:

Can't find npx in PATH: /Applications/Xcode.app/Contents/Developer/usr/libexec/git-core:/Applications/Tower.app/Contents/Resources/git-flow:/Applications/Tower.app/Contents/Resources/git-lfs:/usr/bin:/bin:/usr/sbin:/sbin
Skipping pre-commit hook
Can't find npx in PATH: /Applications/Xcode.app/Contents/Developer/usr/libexec/git-core:/Applications/Tower.app/Contents/Resources/git-flow:/Applications/Tower.app/Contents/Resources/git-lfs:/usr/bin:/bin:/usr/sbin:/sbin
Skipping prepare-commit-msg hook
Can't find npx in PATH: /Applications/Xcode.app/Contents/Developer/usr/libexec/git-core:/Applications/Tower.app/Contents/Resources/git-flow:/Applications/Tower.app/Contents/Resources/git-lfs:/usr/bin:/bin:/usr/sbin:/sbin
Skipping commit-msg hook

Output of installation from terminal

Just for completion.

$ pwd && HUSKY_DEBUG=1 npm i husky@next
/Users/myuser/development/myproject

> husky@4.0.0-beta.5 preuninstall /Users/myuser/development/myproject/node_modules/husky
> node husky uninstall

husky > Uninstalling git hooks
husky:debug Current working directory is /Users/myuser/development/myproject/node_modules/husky
husky:debug INIT_CWD is set to /Users/myuser/development/myproject
husky:debug Git rev-parse command returned:
husky:debug   --git-common-dir: .git
husky:debug   --show-prefix: 
husky > Done

> husky@4.0.0-beta.5 install /Users/myuser/development/myproject/node_modules/husky
> node husky install

husky > Setting up git hooks
husky:debug Current working directory is /Users/myuser/development/myproject/node_modules/husky
husky:debug INIT_CWD is set to /Users/myuser/development/myproject
husky:debug Git rev-parse command returned:
husky:debug   --git-common-dir: .git
husky:debug   --show-prefix: 
husky:debug Package manager: npm
husky:debug Installing hooks in /Users/myuser/development/myproject/.git/hooks
husky > Done

@giovannipiller
Copy link
Author

giovannipiller commented Nov 19, 2019

Here's 3 workarounds when using GIT-Tower or any other GUI with husky@next.
Note: this works if you use NVM, as it adds itself to your $PATH by editing your .bash_profile, which GUI apps never load.
Note 2: I already had a ~/.node_path set-up, but that didn't change things.

1. .husky.local (or ~/.huskyrc)

Create a .husky.local in the root of your GIT project, and either change $PATH there, or do something like:

#!/bin/sh
source ~/.bash_profile

Pros: it's versioned and shared with all your co-workers, if you use .husky.local
Cons: the ~/.bash_profile of your co-workers might differ greatly from yours

Personally, this is my favorite solution.

2. GIT-Tower environment vars

You can create a PLIST XML file to let GIT-Tower know the value of environment variables.
The file must be created here:
~/Library/Application Support/com.fournova.Tower3/environment.plist

Pros: when using GIT-Tower, this config is shared across all projects on the machine
Cons: must be created on each machine; might require manual updates after installing new node versions from NVM.

3. Permanently edit your PATH

Permanently edit your path to include everything added from your ~/.bash_profile.
ex. sudo nano /etc/paths, and add you paths there.

Pros: system level, won't affect just GIT-Tower.
Cons: affects all users of the machine; must be kept up-to-date just like the previous solution.

@giovannipiller
Copy link
Author

@typicode I think it would be a good idea to have husky@next break if it can't find the command for the current package manager. If a hook fails silently, GUI often won't display any log.

ex. We could have an error message, possibly with some instructions on how to fix it.

I guess we'd have to change the run_command () function from the installed .git/hooks/* to exit 1 if package manager's command doesn't exist.

@typicode
Copy link
Owner

Thanks for the feedback. Husky 5 will break if some command is not found 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants