Running Git hooks with environment variables in IntelliJ

• 2 min read

If you use the IntelliJ (or any JetBrains IDE) commit GUI in combination with the following:

  • A language version manager like NVM or SDKMAN (you should)
  • Git hooks (e.g. via Husky) that rely on some environment variable like PATH or JAVA_HOME

You'll most likely have encountered errors like this when you try to commit:

JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation.

Obviously, this is unlikely to be the case as you can run Java, Node or whatever through your terminal. So what's the problem?

The problem

If you open up IntelliJ using the terminal (using the bin/idea.sh script), you'll find that your Git hooks will suddenly start working with the commit GUI. Obviously, this isn't great as you'll usually want to open IntelliJ up via a desktop launcher for convenience, and to avoid keeping an extra shell open.

Language version managers like NVM or SDKMAN work by hooking into .bashrc or .zshrc startup scripts. If you look inside one of these, you'll see something like:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Unfortunately, opening IntelliJ through a desktop launcher doesn't run .*rc startup scripts like in a terminal. This is because desktop applications are opened in non-interactive shells (unlike terminal shells, which are interactive). Launching IntelliJ in this way will not execute any of the commands needed by the language version managers.

The solution

There's a few solutions to this problem, but the one I find the simplest is to add any required environment variables to .profile, .bash_profile or .zprofile in your home directory. These startup scripts are ran by your desktop shell upon login and anything you define in here will be seen correctly by IntelliJ.

I prefer to use .profile for simplicity and have something like this in it:

export PATH="$HOME/.nvm/current/bin:$PATH"
export JAVA_HOME="$HOME/.sdkman/candidates/java/current"

Note that in the above, the 'current' directories are just symlinks to the actual locations.

Finally, just log in again and you should see that your Git hooks now work with the commit GUI. Nice and easy.