Fixing VS Code in WSL with systemd

     

You might be as naive as I was and try to install VS Code natively in WSL using the official docs, but then you’ll get the following:

$ code
To use Visual Studio Code with the Windows Subsystem for Linux, please install Visual Studio Code in Windows and uninstall the Linux version in WSL. You can then use the `code` command in a WSL terminal just as you would in a normal command prompt.
Do you want to continue anyway? [y/N] y
To no longer see this prompt, start Visual Studio Code with the environment variable DONT_PROMPT_WSL_INSTALL defined.

If you then proceed, you’ll get a very odd-looking VS Code indeed. Scaling’s off, window decorations are off, etc.

Then you might as well go ahead and revert the damage:

sudo apt purge --autoremove code

Now start the Windows version:

$ code
/mnt/c/Users/bviktor/AppData/Local/Programs/Microsoft VS Code/bin/code: 61: /mnt/c/Users/bviktor/AppData/Local/Programs/Microsoft VS Code/Code.exe: Exec format error

What the heck? So after some investigation, it turns out to be a systemd-specific issue. The workaround is pretty simple:

echo ':WSLInterop:M::MZ::/init:PF' | sudo tee /usr/lib/binfmt.d/WSLInterop.conf
sudo systemctl restart systemd-binfmt
code

And bam! VS Code is up and running in WSL even on systemd.

Thanks a lot for the hint, cerebrate!