'Open Linux subsystem directory v.s. raw Windows directory in VS Code?

I want to open a WSL directory in VS Code. What's the difference between the following two methods and which should I use?

  1. First method, open WSL window. It's common.

enter image description here

  1. Second method, directly open directory and select the directory in wsl$ path.

enter image description here



Solution 1:[1]

Visual Studio Code recognize better the Connection than opening it directly

Solution 2:[2]

In most cases, you'll want to use the "New WSL Window" (or its equivalent). This utilizes the "Remote - WSL" extension to connect to your WSL instance through a small server that it installs in your WSL user's home directory. You'll probably find it already installed in your case in ~/.vscode-server.

This server handles the "translation" between the Windows VSCode and the Linux files, folders, and processes.

An easy way to think about the difference between the two methods of opening a folder:

  • Using "New WSL Window" puts VSCode in "WSL/Linux" mode
  • Using "Open Folder" and opening \\wsl$\... directly keeps VSCode in "Windows mode".

Comparing the two techniques with a real file. I have a simple Python file in my WSL home directory that I wrote up for another answer a few days ago. If I:

  • Open my home directory through \\wsl$\ in VSCode, then the VSCode Python extension complains:

    Python is not installed. Please download and install Python before using the extension.

    Since I don't have the Windows version of Python installed, and VSCode is operating in "Windows mode", it can't find the Linux/WSL Python interpreter.

However, if I:

  • Open my home directory through "Remote-WSL: New WSL Window", then open my Python file, then VSCode finds my Python interpreter, and I can run and debug the file in WSL through VSCode.

Side note #1: There is another method that has the same effect as using the command palette's "New WSL Window" -- From inside a WSL directory, run code .

Side note #2: There may be times when you want to open a file that lives inside WSL in "Windows mode". You may want to run it in a Windows version of a tool (Python, Java, whatever) to check compatibility.

As a general rule of thumb, however, you should probably do your Linux development with files inside WSL using VSCode's "WSL Mode" and Windows development with files that live on a Windows drive using "Windows (a.k.a. normal) mode".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nicolas Gomez R
Solution 2 NotTheDr01ds