andy-5 / wslgit

Use Git installed in Bash on Windows/Windows Subsystem for Linux (WSL) from Windows and Visual Studio Code (VSCode)
MIT License
1.18k stars 59 forks source link

Permission denied running Git hooks #139

Closed daniel-shuy closed 9 months ago

daniel-shuy commented 9 months ago

When attempting to run NPM scripts in a Git pre-commit hook, wslgit fails with Permission denied.

What's odd is I can run the NPM script manually in WSL with the same user used by wslgit (checked with echo $USER in the pre-commit hook).

andy-5 commented 9 months ago

Can you check if you can run it manually from outside of WSL using something like:

wsl -e /bin/bash -c "git commit -m 'My commit message.'"

This is more or less the command that gets run by wslgit. If that does not work, you could check if it works when running an interactive shell (using the i flag):

wsl -e /bin/bash -ic "git commit -m 'My commit message.'"

An interactive shell is usually required to apply your .bashrc file, and for some commands (like push, pull, etc.) wslgit automatically uses interactive shells. This can be configured as described here.

daniel-shuy commented 9 months ago

I got the same Permission denied error for both

daniel-shuy commented 9 months ago

I managed to narrow down the issue. Somehow the PATH is not set correctly when running in wslgit/wsl -e.

I am using asdf-nodejs to manage my Node.js versions, but its not working when running in wslgit/wsl -e because ~/.asdf/shims is not in the PATH.

andy-5 commented 9 months ago

Where does asdf setup the correct PATH? After a quick look in the documentation, it seems this is usually done in .bashrc for the Bash shell. .bashrc should be loaded by an interactive shell (with the i flag) in most configurations.

You can check if .bashrc is executed by putting something like echo "Executing .bashrc" at the top of .bashrc, and repeat the manual commands from above (for using wslgit you must remove this output again, it will not work otherwise).

If there is no output, what distribution are you using? Do you have a .profile, .bash_profile or .bash_login file in your home directory?

daniel-shuy commented 9 months ago

Ah, my bad, you hit the nail on the head, turns out I have both ~/.bashrc and ~/.bash_profile, which was causing the ~/.bash_profile to be ignored. Thanks and sorry for the trouble