Linux – Editing files in a Windows Subsystem for Linux development environment

linuxUbuntuwindowswindows 10windows-subsystem-for-linux

Windows Subsystem for Linux (WSL) works pretty well for making most commandline Linux tools available and working on Windows without modification. However, it gets a little tricky for development, when one wants to

• Build a project using a Linux toolchain that doesn't have a well-supported Windows equivalent (Ruby, Node, etc)
• Edit files using a Windows-based GUI editor such as Visual Studio code.

The problem is that Windows apps cannot modify files inside the virtual lxss filesystem. Directly modifying these files is known to cause all sorts of issues.

Therefore, there seem to be only two suboptimal choices when it comes to using WSL for development:

1. Store the project under lxss (/home/foo). The normal toolchain works properly. However, editing is limited to either terminal-based Vim/Emacs or whatever can be run under a janky X server, which is less smooth than native editors running on Windows.

2. Store the project under the Windows filesystem (/mnt/c/Users/foo). Now any Windows-based editor can be used for development. However, the Linux-based toolchain is fragile as it's not designed to be used on a "network drive", and can cause problems with file watching or databases.

Is there any way to get the best of both worlds here – that is, to be able to edit using a native Windows application, but still have the Linux toolchain work as it normally would on a local drive?

With the new feature, you'll be able to safely access both Windows and Linux filesystem files from Windows, as long as you go through the 9P server. This will be handled natively from within WSL. For example, from the WSL command line you'll be able to type code /mnt/c/Users/username/src/windows-file.txt to open a Windows file in VS Code, or type code /home/username/src/linux-file.txt to open a Linux file in VS Code.
wslpath will convert between Windows- and Linux-style paths so you can easily open Windows files from the WSL command line. As per https://github.com/Microsoft/WSL/issues/3146#issuecomment-388118689, it will refuse to convert Linux filesystem paths (i.e. %AppData%\lxss), because without 9P it's unsafe to modify these files from Windows. This means you can't open /home/username/src/linux-file.txt, but you can use code "\$(wslpath -aw /mnt/c/Users/username/src/windows-file.txt)".
In the past, there were a number of third-party tools to perform the same conversion but wslpath does it natively-- in fact, ls -l /bin/wslpath shows that it's just a link to /init.