typicode / husky

Git hooks made easy 🐶 woof!
https://typicode.github.io/husky
MIT License
32.34k stars 1.02k forks source link

Add macOS section to troubleshooting #1317

Closed sdavids closed 7 months ago

sdavids commented 10 months ago

https://typicode.github.io/husky/troubleshooting.html#command-not-found

On macOS UI applications do not have the environment set in ~.zshrc.

Node version managers like nvm/fnm rely on setup in ~/.zsh.

Graphical git clients like Tower for example cannot use the git hooks created by husky because they will not find the husky npm installation.

Therefore on macOS you have to have a .huskyrc.

Here are two examples:

nvm

#!/usr/bin/env sh

export NPM_CONFIG_USERCONFIG="${HOME}/.npmrc"
export NVM_DIR="${HOME}/.nvm"

if [ -f "${NVM_DIR}/nvm.sh" ]; then
  . "${NVM_DIR}/nvm.sh"

  if [ -f .nvmrc ]; then
    nvm use
  fi
fi

fnm

#!/usr/bin/env sh

export NPM_CONFIG_USERCONFIG="${HOME}/.npmrc"

if type fnm > /dev/null 2>&1; then
  eval "$(fnm env)"

  if [ -f .node-version ] || [ -f .nvmrc ]; then
    fnm use --silent-if-unchanged
  fi
fi

Please mention the above under Troubleshooting, so other macOS users find it easier.

Klaasvaak commented 9 months ago

I have been struggling with getting husky working with the git-tower application for so long. For some of the projects I work with it worked with above's example. But not for all. And indeed the issue is:

On macOS UI applications do not have the environment set in ~/.zshrc.

What I did was source the ~/.zshrc. Adding this to the .huskyrc file:

#!/usr/bin/env sh

source ~/.zshrc
sdavids commented 9 months ago
#!/usr/bin/env sh

source ~/.zshrc

I suggest you adjust it as follows:

#!/usr/bin/env sh

set -eu

[ -e /etc/zshenv ] && . /etc/zshenv
[ -e "${ZDOTDIR:=${HOME}}/.zshenv" ] && . "${ZDOTDIR:=${HOME}}/.zshenv"
[ -e /etc/zprofile ] && . /etc/zprofile
[ -e "${ZDOTDIR:=${HOME}}/.zprofile" ] && . "${ZDOTDIR:=${HOME}}/.zprofile"
[ -e /etc/zlogin ] && . /etc/zlogin
[ -e "${ZDOTDIR:=${HOME}}/.zlogin" ] && . "${ZDOTDIR:=${HOME}}/.zlogin"

git-hooks invoked by a graphical client are run in a non-interactive login shell.


Zsh - Startup/Shutdown files

Shell startup scripts

ZSH: .zprofile, .zshrc, .zlogin - What goes where?

mxmlnglt commented 7 months ago

I suggest you adjust it as follows: ...

AFAICT, nvm only adds its config lines to .zshrc; so @Klaasvaak solution worked for me. Thanks!!

typicode commented 7 months ago

Closing as there's a dedicated section: https://typicode.github.io/husky/how-to.html#node-version-managers-and-guis

There's only instructions for nvm because there are tens of version manager and it would be too complex to keep track how to load them.

Unless there's a check in .zshrc to prevent loading in non-interactive mode, sourcing it manually should work (even in non-interactive mode).

That being said, you're right that different files are loaded by zsh based on the context :+1: