As per docs:
If Git is not installed on Windows, PyCharm searches for Git in WSL and uses it from there. Also, PyCharm automatically switches to Git from WSL for projects that are opened when you use the \wsl$ path.
The new feature in 2020.2 professional allowing configuring of git executable through \\wsl$\ works OK when using the Microsoft Store installed Ubuntu 18.04.
However - any imported version of the same distribution (wsl --import <distro name> <location> <image>) isn't found by found by PyCharm despite it being clearly browsable under Windows, and operating fine under wsl2.
This makes the new feature somewhat limited as one of the key ideas of wsl2 is that you can export and import many images at once.
I guess the question is - what structural assumptions is PyCharm's git locator making about Linux distros?
To reproduce install Ubuntu-18.04 in WSL2.
Export it out:
wsl --export Ubuntu-18.04 my-image.tar
Import it back in:
wsl --import My-18.04 my-distro-dir my-image.tar
Now start-up My-18.04:
wsl -d My-18.04
Note that despite the above process working and My-18.04 distro containing git - PyCharm 2020.2 will not find it AND refuse to browse to it when given the address \\wsl$\My-18.04\usr\bin\git - despite this address being browsable from Windows.
It makes no difference if I make my custom distro the default distro or if I remove all other distros. I've tried reinstall both WSL and PyCharm - with no change in behaviour.
Lastly I tried changing the underlying git.xml file:
<option name="myPathToGit" value="\\wsl$\My-18.04\usr\bin\git" />
But after restarting PyCharm, it cannot find the location.
If anyone has a fix or workaround I'd be very grateful - I've tried nesting the ext4 inside a fake LocalState directory - that didn't work? This is a bit of a deal-breaker in rolling out PyCharm as our Python IDE, so really keen to find a workable solution.
If you need any more info, just let me know.
Some more digging and I've found a very similar issue reported here:
With a solution suggested in the Ruby Mine 2020.2 docs:
This *almost* works by adding the following to the PyCharm config file - wsl.distributions.xml
<presentable-name>Ubuntu 18.04 - Custom</presentable-name>
The one remaining question (which makes it different to the reported issue) is what to put in the executable-path - given that exported WSL2 builds have no exe - nor do they need one?
The answer seems to be that PyCharm still expects a distro exe to exist - as provided by the template here:
This should be update to work directly with the wsl.exe binary - passing in the -d switch to specify the distribution, rather than have a different exe per distro.
I tried a very quick and ugly hack - of assuming the first param passed is always "run" so ignoring that completely, and then passing the rest to bash via wsl.exe - this got PyCharm to successfully verify git in the git settings page - but git clone is still failing:
rem throw the first parameter away
if [%1]== goto afterloop
set params=%params% %1
My-18.04 --exec bash -c %params%
So it looks like a viable workaround would be to produce a better version of the above script, or even build your own distro exe - but I think this should be resolved in PyCharm.
For now perhaps the best solution is to continue to use wslgit or similar and not to use PyCharm functionality?