WSL 'Open Linux shell here' doesn't open in the current directory

If I right-click on a folder holding shift it does show open Linux shell here.
But if I am in say D: drive no matter which folder I am in it will always open the Linux shell starting at /mnt/d.

What could be the reason for that?


Likely something in a startup script is interfering, but let's walk through the troubleshooting.

First, let's check the command that Windows is using when you select "Open Linux shell here". In PowerShell:

(Get-ItemProperty -name "(default)" -LiteralPath Registry::HKEY_CLASSES_ROOT\Directory\shell\WSL\command).'(default)'

The result should be wsl.exe --cd "%V", and I'm guessing it is. If not, you can fix that with a registry edit.

But let's try that command from PowerShell as well:

wsl.exe --cd "D:\some directory" (where, obviously, some directory is replaced with something that exists on your drive).

I'm also guessing that this will have the same results for you that the right click did - It will end up in D:\ rather than the desired directory. But if it does work, then we'll need to investigate further on why there's a difference with the right-click Open-with command.

So now, let's see if we can make it work from the CLI. Try:

wsl.exe --cd "D:\some directory" -e bash --noprofile --norc

I'm hoping that works for you. That will indicate the problem is something in your .profile or .bashrc (or another startup script).

If so, see if you can track any startup script customization you've done that might affect the cd. It's possible that the change was made by some installer, as well. Or perhaps even that some distribution's stock startup scripts are a problem. You didn't mention which distro you are using, but things work correctly for me on Ubuntu 20.04.