SourceTree : Hook failing because paths don't seem to be set correctly

oldskool73
Contributor
February 21, 2013

Hi,

I have a pre-commit hook that calls grunt to lint before checking in. When commiting from the command line it works fine. When commiting via SourceTree it doesn't work. The pre-commit hook runs...

#!/bin/sh
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
ROOT="$(git rev-parse --show-toplevel)"
cd $ROOT
git stash -q --keep-index

cd $ROOT/build
/usr/local/bin/grunt $BRANCH
RESULT=$?
cd $ROOT

git stash pop -q
[ $RESULT -ne 0 ] && exit 1
exit 0

gives the error :

git -c diff.mnemonicprefix=false -c core.quotepath=false commit -q -F /var/folders/Ta/Ta3HaDmvGz8e5G+8MKxNvLtVItU/-Tmp-/SourceTreeTemp.bXmip3 

env: node: No such file or directory

Completed with errors, see above

If I try to run the ST generated git command from the shell manually it works fine too, so I'm guessing it's something up with the paths. I tried creating a launchctl.conf and adding /usr/local/bin to it but that didn't change the error. The path appears to be correct in env.

> which node

/usr/local/bin/node

> env | grep /usr/local/bin

PATH=/usr/local/share/python:/usr/local/opt/php54/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/Users/adamr/bin:/Users/adamr/Applications/AWS-ElasticBeanstalk-CLI-2-1.3/eb/macosx/python2.7:/Users/adamr/Applications/AWS-ElasticBeanstalk-CLI-2-1.3/api/bin

Thanks,

Adam

19 answers

29 votes
aamirafridi
Contributor
November 7, 2015

If you are on Mac than try to open sourcetree with this command

open /Applications/SourceTree.app/Contents/MacOS/SourceTree

Now pre-commit and pre-push hooks will run when you try to commit and push from sourcetree

Robin Caroff
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 14, 2015

Works for me, thanks! I also create a an Automator workflow to start this command for a Dock icon.

Like brettz9 likes this
aamirafridi
Contributor
November 15, 2015

Create an alias Or may be try making a bash file and drag it to your dock. Never tried these though :p

Pulkit Singhal
Contributor
August 23, 2016

It really works with two important points to avoid wasting time:

  1. close all windows and quit sourcetree completely ... only closing the windows is not enough because that means that previous sourcetree process without the PATH is still running!
  2. `stree` is the only thing you need to type if you are already in the directory of interest and sourcetree command line tools are already installed
Like # people like this
Deleted user March 9, 2017

This worked for my setup where I'm using NVM for node versions. 

Marcin Gorny
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 20, 2019

It works! But could someone explain why? @aamirafridi 

Kamil Dubel
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 26, 2019

@Marcin Gorny it works because when you execute command in terminal you are opening sourcetree with your current PATH env (and node path is embedded there)

Like # people like this
Gospodin Chavdarov
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 13, 2021

Works for me as well. Thanks, @aamirafridi!

Rodney Foley
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 17, 2021

Does not work in 2021 for me on a Mac.  Shouldn't matter what shell you use but using ZSH and all the commands work from the terminal but do not when running from Sourcetree when launched via either `/Applications/SourceTree.app/Contents/MacOS/SourceTree` or `/Applications/SourceTree.app`

Same failure that it cannot find a command that does exist in the path it was launched from.

I mean Atlassian should just fix this and allow us to tell it what shell launch script to use when starting.  I mean it's typical Atlassian ignoring issues for 7 years or more. Never expect them o fix anything even if it appears simple.

Like # people like this
9 votes
Timothy Churchward
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 21, 2014

Not the nicest solution but adding

export PATH=/usr/local/bin:$PATH

directly to my pre-commit script worked for me.

Elliot Mitchell
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 5, 2021

2020 Checking in, still works

hpetrovi
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 29, 2021

This works. Great, thanks

5 votes
ltvpro
Contributor
September 22, 2018

New OSX does not allow to write into /usr/bin (I don't know exactly since which version), therefore we cannot create symbolic link in /usr/bin.

This works on 10.13:

https://apple.stackexchange.com/a/198282/297299

sudo launchctl config user path /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Cansin Yildiz
Contributor
September 18, 2019

Thanks for the suggestion. I ran below on macOS Mojave to get over this problem:

sudo launchctl config user path `echo $PATH` 
Like # people like this
Deleted user October 18, 2019

This also worked for me on MacOS Catalina.

4 votes
Bas Kamer
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 17, 2014

this works wonders for me...

#!/bin/bash

source ~/.bash_profile

so to use git-hooks with sourcetree i modified the script a little...

Megan Koester January 14, 2019

Thank you!  This seems to be by far the simplest solution.

grafxflow
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 28, 2020

Perfect for 10.15.3 :)

Uri Klar
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 3, 2020

Hi! Can you please explain where this code should be added?

Ankit Kumar
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 27, 2021

in git hooks file whichever you want to run while git process.... i.e, pre-commit in our case.

<git hook folder>/pre-commit ->

-------------------------------------------

#!/bin/bash

source ~/.bash_profile

--------------------------------------------

but this is not appropriate for new mac devices because it uses zsh so in that case

-> source ~/.zshrc

so this solution is not for everyone

Like Kassim Maguire likes this
3 votes
kristian.gerardsson May 10, 2021

8 years later... 

Here devs, i fixed it for you!..

Screenshot 2021-05-11 at 11.24.55.png

 

1. Other Git UI's don't have this problem.

2. I'm not going to manually change my .git-hook files. I have over 20 projects.

 

Also, the $PATH it is loading comes from somewhere. Yet mine doesn't even include /usr/bin etc.. Those should be quite obvious to include/append.

SourceTree shows me these only if i do echo $PATH in my git hook.

```

/Applications/Sourcetree.app/Contents/Resources/git_local/libexec/git-core
/Applications/Sourcetree.app/Contents/Resources/bin
/Applications/Sourcetree.app/Contents/Resources/git_local/bin
/Applications/Sourcetree.app/Contents/Resources/git_local/gitflow
/Applications/Sourcetree.app/Contents/Resources/git_local/git-lfs
/Users/kristiangerardsson/.nvm/versions/node/v12.15.0/bin
/Users/kristiangerardsson/.avn/bin
/opt/apache-maven-3.6.0/bin
/usr/local/Cellar/node/8.5.0/bin/
/Users/kristiangerardsson/.yarn/bin
/Users/kristiangerardsson/google-cloud-sdk/bin
/Applications/Visual

```

wout
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 29, 2021

Awesome :D

2 votes
Devon Humes
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 6, 2014

I have this exact problem, but adding /usr/local/bin to the /etc/paths file doesn't fix it. When I echo out the path in my pre-commit hook, the path variable does not include everything from /etc/paths. Is there somewhere else that I need to change?

1 vote
John Talbot
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 5, 2021

I'm not fond of changing system paths affecting all users or other programs. Symlinks into /bin being protected in later versions of OSX makes it very difficult as well.  If you are using husky for your githooks then they have the best answer of setting the path in your ~/.huskyrc file. I.e mine contains: 

PATH=/opt/local/bin:/opt/local/sbin:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin:/opt/X11/bin:/Library/Apple/usr/bin

 This makes hooks run even if sourcetree is already running as it takes affect on the next use of the githook by husky.

Frank Brullo
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 8, 2022

So far, this is the best solution I have read so far.

 

Thanks a lot!

P Palm September 1, 2022

Finally, something that works!
All the solutions mentioned above couldn't help me.
So thanks for posting this.

John Talbot
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 20, 2022

Your all Welcome!  This still works for me.

1 vote
kristian.gerardsson May 27, 2019

Helloooo, how is this still a thing in 2019? Why can't SourceTree pick up the $PATH variable?

 

echo $PATH
/opt/apache-maven-3.6.0/bin:/usr/local/Cellar/node/8.5.0/bin/:/Users/kristiangerardsson/.yarn/bin:/Users/kristiangerardsson/google-cloud-sdk/bin:/Users/kristiangerardsson/.rbenv/shims:/Users/kristiangerardsson/.rbenv/bin:/Users/kristiangerardsson/.fastlane/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin

 

Error when running commands in git-hooks: /bin/sh: mvn: command not found

Megan Koester May 28, 2019

Add this at the beginning of your githooks script: export PATH=/usr/local/bin:$PATH

kristian.gerardsson June 17, 2019

The problem is with mvn, which is in

/opt/apache-maven-3.6.0/bin

- It can be a different path for different devs, so you can't commit that.

- That won't work on windows

 

Terrible answer :P

ltvpro
Contributor
September 18, 2019

The problem is that your PATH from .bash_profile is just for bash shell. Different shell uses different profile file. Sourcetree launched from system, not from bash, therefore cannot inherit your bash profile. The solution is that setting PATH from system level as my answer above. 

1 vote
Ben Hutton
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 11, 2015

If you're using OS X, and want to be able to launch SourceTree from the dock while having the correct envrionment... you can follow this simple guide to create a sort of proxy app which will launch SourceTree from the terminal, having sourced your bash_profile...

https://mathiasbynens.be/notes/shell-script-mac-apps

My shell script...

#!/usr/bin/env bash
source ~/.bash_profile
open ~/Applications/SourceTree.app

Confirmed that this works!

0 votes
ac
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 15, 2022

A simple option change that fixed it for me is using system git instead of embedded git:

Settings->Git->Use System Git

Screenshot 2022-12-15 at 10.07.26 AM.png

0 votes
Kem Tekinay
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 13, 2021

My solution was to use a script as a git replacement. The only difficulty was that "submodule" is not a valid subcommand in the embedded git for reasons that elude me. (When I use Embedded Git directly, there is no error, but if I issue the same command against SourceTree.app/Contents/Resources/git_local/bin/git on the command line, there is.) 

#!/bin/bash

PATH=$PATH:/opt/local/bin

# Set git path based on params
if [[ "$@" == *'submodule'* ]]; then
GIT_PATH=/opt/local/bin/git

else
GIT_PATH=/Applications/SourceTree.app/Contents/Resources/git_local/bin/git
fi


"${GIT_PATH}" "$@"
0 votes
Florian Roth
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 3, 2017

My solution was to create a symbolic link in the /usr/bin directory for the program that the hook tried to use. 

In my case the tool "git-secrets"

cd /usr/bin/
sudo ln -s /usr/local/bin/git-secrets .
0 votes
simon hansen
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 20, 2017

I'm facing the same issues on my mac with SourceTree 2.5. The precommit hook should lint some php files which are written in php7 syntax. 

The problem is that SourceTree uses the system php (version 5.x) which can't parse the files.

Triggering the precommit hook manually works fine - it uses the correct php version specified as path variable.

So I printed the path in the commit hook and the order of the path variables is different in SourceTree than in terminal. Any suggestions?

Pulkit Singhal
Contributor
April 21, 2017

Based on previous posts, I start my `stree` from the terminal of interest everytime now so that the variables align.

0 votes
Pulkit Singhal
Contributor
August 23, 2016

If `nvm` is what takes care of your nodejs then ...

The right answer is available here:

https://answers.atlassian.com/questions/140339/answers/32030983
0 votes
Dmitri Shchurov April 7, 2015

This works better in my environment

#!/usr/bin/env sh
export HOOK_NAME=$0
export HOOK_ARGS=$@
source ~/.bash_profile
`which node` &lt;&lt; `tail -n +5 $0`

console.log('test ' + process.env['HOOK_NAME']);
console.log('args ' + process.env['HOOK_ARGS']);

All other advices have caused troubles. Inspired by Universal node.js shebang

Nitin_Surana
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 28, 2015

didn't worked for me.

0 votes
Gugulethu Mabuza
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 29, 2013

Has there been a fix for this? I am getting the same error when running a hook that has the command exec("php-cs-fixer fix...) Is there a plan to fix this?

stevestreeting
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 29, 2013

It's not something we can fix ourselves, it's an effect of the fact that terminals and full .apps have compeletely different environments (and PATH variables), so just adding something to your PATH in the terminal isn't going to make it visible to something launched by SourceTree. The way to resolve it is to add this folder to your global PATH for .apps too, which you do by editing /etc/paths or adding a new entry to /etc/paths.d

Devon Humes
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 4, 2014

I have this exact problem, but adding /usr/local/bin to the /etc/paths file doesn't fix it. When I echo out the path in my pre-commit hook, the path variable does not include everything from /etc/paths. Is there somewhere else that I need to change?

Navkaran Garcha
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 13, 2014

As discussed in an earlier post you could just symlink the missing paths?

For example my SourceTree application doesn't look in /usr/local/bin (the location of grunt and node on my machine) however it does look in /usr/bin (do 'echo $PATH' in a hook file for example and you will see what paths it is looking in) - so i just did the following in the terminal:

ln -s /usr/local/bin/grunt /usr/bin/grunt
ln -s /usr/local/bin/node /usr/bin/node

Hacky but works.

0 votes
KieranA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 24, 2013

I have been scouring around to see what's going on in this case. We should definitely have access to env vars as these are used in many cases. Using 'which node' doesn't necessarily mean it's correct unfortunately, it locates an application in the user's path, so just a few things to check. First, actually browser to /usr/local/bin/node and ensure it's installed there. If it's installed there, is it usable from there? Regarding env vars, have a look here to see if there's anything you're missing. 'node' has been discussed and changed at some point to 'nodejs' instead, but I can't determine when that happened, or if it has happened on Mac OS - users seem to be aliasing 'node' to 'nodejs'.

If all that fails, check out this guys blog regarding the exact error you're getting. Some env vars (dependent on how they're set, and from what app they're set from) don't get set on the same path which means they're unavailable in perhaps this context.

It will be a case of poke it and see, especially when it comes to the path var being set and env vars in general. With the error you're getting, I'm thinking that the env var isn't set in the correct place which is why I've linked those two articles which should hopefully shed some light on it.

oldskool73
Contributor
February 24, 2013

Hey thanks again for looking into this.

Turns out it was the same issue as described in the last post you linked to. If I launch ST via the command line ($open /Applications/SourceTree.app/Contents/MacOS/SourceTree) it works fine. It only fails when launched by clicking on the app or via spotlight. I'm a bit wary of creating a new $path in environment.plist as this will eventually get out of synch with the real $path and I'll invariably forget about it and end up even more confused later :) So for now I've just symlinked node and phpunit binaries from /usr/local/bin into /bin/ . Not ideal but seems to fix it.

Cheers,

Adam

CStumph
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 2, 2015

This worked for me too

0 votes
oldskool73
Contributor
February 22, 2013

10.6.8 at work where I first noticed this, and 10.7.3 at home getting the same error. Thanks.

0 votes
KieranA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 21, 2013

Hi Adam. Which version of OS X are you using? Thanks in advance.

Fery Christysen
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 7, 2018

Hi Kieran, I also face this problem, in my case, I have pre-commit git hook which run some lint and format code script. This script use my .bash_profile environment variable. If I commit through terminal, the pre-commit hooks runs successfully, while when I commit through SourceTree, if fails to find the specified environment variable.

I use MacOS version High Sierra 10.13.6

ltvpro
Contributor
September 22, 2018

This works:

https://apple.stackexchange.com/a/198282/297299

sudo launchctl config user path /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events