Search code examples
mercurialwindows-subsystem-for-linux

Mercurial in WSL: HGEDITOR $PATH is different from my $PATH


I am trying to configure Mercurial inside of my WSL2 instance on windows.

I am on WSL and chose "Ubuntu" as my distro, which gave me 22.04.3 LTS.

I installed mercurial via snap: sudo snap install mercurial which gave me hg --version 6.4.4.

hg generally works, but when I try to change my editor to the code binary, hg can't find that binary in my path. This is despite the fact that it's available in my interactive bash shell:

mason@Ryzen-7:~/Documents/Code/coda$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/dotnet/:/mnt/c/Program Files/TortoiseHg/:/mnt/c/Users/mason/.cargo/bin:/mnt/c/Users/mason/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/mason/AppData/Local/Programs/Microsoft VS Code/bin:/snap/bin

mason@Ryzen-7:~/Documents/Code/coda$ which code
/mnt/c/Users/mason/AppData/Local/Programs/Microsoft VS Code/bin/code

As a little hack to find out what PATH hg can see, I updated my editor = echo $PATH. Then when I try to launch an editor, it just prints the PATH that is using when it tries to spawn an editor, and that PATH is, quite surprisingly, different from mine:

mason@Ryzen-7:~/Documents/Code/coda$ hg config -e
/snap/mercurial/12/usr/sbin:/snap/mercurial/12/usr/bin:/snap/mercurial/12/sbin:/snap/mercurial/12/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

hg's $PATH, then, doesn't contain the necessary /mnt/c/Users/mason/AppData/Local/Programs/Microsoft VS Code/bin.

Why is hg's PATH different from the parent shell, and how can I modify the path that hg sees?

Edit: I tried one more thing. I opened up an interactive python3 shell and tried os.environ. python3 has correctly inherited its parent's enrivonment including my full $PATH.


Solution

  • I had this problem too and it was driving me crazy! It's because mercurial was installed via snap and is running in a sandbox. Mercurial doesn't have permission to launch an arbitrary binary from inside snap - that's why it can never find anything.

    I got around this by downloading the mercurial source from https://www.mercurial-scm.org/downloads and running from a local installation (no compilation necessary).

    I don't know how to get snap to allow mercurial access to other binaries on the filesystem.