Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WSL2 terminal does not recognize Visual Studio Code

When I try to execute code from WSL, like this:

cmd.exe
wsl code .

I get the following error:

/bin/bash: code: command not found

However, if I execute the same command but first run cmd.exe as an Administrator, vscode opens as expected.

The question is why "code" is not recognizable when I run as non-admin, and how can I solve it?

Additional info: WSL2 version 41959 vscode version 1.38 vscode Remote WSL extension version 0.39.5

like image 353
kDar Avatar asked Sep 10 '19 10:09

kDar


People also ask

How do I open VS Code in terminal WSL?

Alternatively, you can open a Remote WSL window directly from VS Code: Start VS Code. Press F1, select Remote-WSL: New Window for the default distro or Remote-WSL: New Window using Distro for a specific distro. Use the File menu to open your folder.

Does Visual Studio work with WSL?

Visual Studio's WSL 2 toolset allows you to use Visual Studio to build and debug C++ code on WSL 2 distros without adding a SSH connection. You can already build and debug C++ code on WSL 1 distros using the native WSL 1 toolset introduced in Visual Studio 2019 version 16.1.

How do I run a Visual Studio Code in terminal?

You can also run VS Code from the terminal by typing 'code' after adding it to the path: Launch VS Code. Open the Command Palette (Cmd+Shift+P) and type 'shell command' to find the Shell Command: Install 'code' command in PATH command.


3 Answers

As @Biswapriyo mentioned, this is an open bug where WSL cannot access Windows C drive. Workaround that helped me is to restart WSL like this:

wsl --shutdown
wsl
like image 139
kDar Avatar answered Nov 17 '22 00:11

kDar


  1. Open the PowerShell administrator window
wsl --list --version

Show as wsl 2

  1. Enter the Linux console
wsl
  1. Change related files' rights
chmod u+x vscode_dir/code.exe
chmod u+x vscode_dir/bin/code
  1. Create symbolic link
ln -s vscode_dir/bin/code code
mv code ~/.local/bin
  1. Modify .bashrc, add a line as follows:
export PATH="$HOME/.local/bin:$PATH"
  1. source .bashrc or restart terminal.

Then you can happily play code in wsl2-ubuntu environment or terminal of VSCode.

like image 35
s8186255 Avatar answered Nov 17 '22 00:11

s8186255


My problem was that I was using the root user after running sudo -i. Exiting and using my normal user on WSL solved the error.

like image 26
Chiel Avatar answered Nov 16 '22 22:11

Chiel