19

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

kDar
  • 3,246
  • 4
  • 19
  • 23
  • Try to run `echo %PATH%` in normal cmd and admin cmd. Did code.exe folder appear? – Biswapriyo Sep 10 '19 at 17:19
  • C:\Program Files\Microsoft VS Code\bin in both – kDar Sep 11 '19 at 07:17
  • Seems the problem is mounting my C drive is working only with elevated privilege. Nothing to do with VS code. If I execute wsl ls /mnt/c I get "ls: cannot access '/mnt/c': Input/output error" – kDar Sep 11 '19 at 09:07
  • OMG! "ls: cannot access '/mnt/c': Input/output error" -- is a real bug. See this issue https://github.com/microsoft/WSL/issues/4377 and wait until it is fixed. – Biswapriyo Sep 11 '19 at 09:11

5 Answers5

32

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
kDar
  • 3,246
  • 4
  • 19
  • 23
17
  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.

s8186255
  • 181
  • 1
  • 5
  • 3
    where vscode_dir = /mnt/c/Program\ Files/Microsoft\ VS\ Code/bin Just doing step 5 and adding this to my path was enough for me. – Maarten Nov 17 '20 at 16:17
  • I'm getting `ln: failed to create symbolic link 'code': File exists` – martin Dec 12 '20 at 18:21
  • /root/.local/bin/code: 63: /root/.local/Code.exe: not found – martin Dec 12 '20 at 18:26
  • All I had to do was create a symbolic link `ln -s code /mnt/c/Program\ Files/Microsoft\ VS\ Code/bin/code`. The `code` link was placed somewhere in the path. I did not need to mark those files executable, they already were. – hookenz Jul 21 '21 at 21:59
1

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.

Chiel
  • 1,865
  • 1
  • 11
  • 24
0

In my case I am using Debian and echo %PATH% did not output anything.

I used the second part of suggested solution from resolved github issue

For Debian, /etc/profile contributed to this problem.

Here is the path definition in /etc/profile

if [ "`id -u`" -eq 0 ]; then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
  PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi

Option 1:

You can delete above lines, then wsl --shutdown to restart Debian.

Option 2:

If you would like to keep these lines, you can also append ":$PATH" to each path like below, then wsl --shutdown

if [ "`id -u`" -eq 0 ]; then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH"
else
  PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:$PATH"
fi

Thank you to licanchua

Cell
  • 173
  • 2
  • 11
0

I'd recommend checking /etc/wsl.conf to see if appendWindowsPath setting shares the Windows PATH with WSL, and also if it is specifically setting a particular user.

For the config settings, see https://learn.microsoft.com/en-us/windows/wsl/wsl-config

Ron Newcomb
  • 2,886
  • 21
  • 24