Wsl2 - Vscode's Shell Command `Code` No Longer Working in Linux Terminal

Wsl2 - Vscode's Shell Command `Code` No Longer Working in Linux Terminal

I am very new to Linux and WSL overall. I was able to use code . to open my current directory in VSCode, but after deleting something that I do not remember it just doesn't work anymore. I put in the command and it just hangs there.

I have deleted my Ubuntu-20.04 instance and recreated it, but still it will not work. Also checked my $PATH and it seems to be there.

Also, the add shell command in VSCode does not come up, so I can't uninstall and reinstall the shell shortcut.

Anyone have any idea?

drewbie@drewbie:/$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/dotnet/:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files (x86)/QuickTime/QTSystem/:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files/Go/bin:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/ProgramData/DockerDesktop/version-bin:/mnt/c/Users/Andrew/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/Andrew/AppData/Local/Programs/Microsoft VS Code/bin:/mnt/c/Program Files/heroku/bin:/mnt/c/Users/Andrew/AppData/Local/GitHubDesktop/bin:/mnt/c/Users/Andrew/go/bin:/snap/bin

whereis and which

drewbie@drewbie:~$ whereis code
code: /mnt/c/Users/Andrew/AppData/Local/Programs/Microsoft VS Code/bin/code /mnt/c/Users/Andrew/AppData/Local/Programs/Microsoft VS Code/bin/code.cmd
drewbie@drewbie:~$ which code
/mnt/c/Users/Andrew/AppData/Local/Programs/Microsoft VS Code/bin/code

After doing what @NotTheDr01ds said in the thread below

drewbie@drewbie:~$ vi "$(which code)"
drewbie@drewbie:~$ code .
+ [  = true ]
+ COMMIT=899d46d82c4c95423fb7e10e68eba52050e30ba3
+ APP_NAME=code
+ QUALITY=stable
+ NAME=Code
+ DATAFOLDER=.vscode
+ realpath /mnt/c/Users/Andrew/AppData/Local/Programs/Microsoft VS Code/bin/code
+ dirname /mnt/c/Users/Andrew/AppData/Local/Programs/Microsoft VS Code/bin/code
+ dirname /mnt/c/Users/Andrew/AppData/Local/Programs/Microsoft VS Code/bin
+ VSCODE_PATH=/mnt/c/Users/Andrew/AppData/Local/Programs/Microsoft VS Code
+ ELECTRON=/mnt/c/Users/Andrew/AppData/Local/Programs/Microsoft VS Code/Code.exe
+ IN_WSL=false
+ [ -n Ubuntu-20.04 ]
+ IN_WSL=true
+ [ true = true ]
+ export WSLENV=ELECTRON_RUN_AS_NODE/w:WT_SESSION::WT_PROFILE_ID
+ wslpath -m /mnt/c/Users/Andrew/AppData/Local/Programs/Microsoft VS Code/resources/app/out/cli.js
+ CLI=C:/Users/Andrew/AppData/Local/Programs/Microsoft VS Code/resources/app/out/cli.js
+ WSL_EXT_ID=ms-vscode-remote.remote-wsl
+ ELECTRON_RUN_AS_NODE=1 /mnt/c/Users/Andrew/AppData/Local/Programs/Microsoft VS Code/Code.exe C:/Users/Andrew/AppData/Local/Programs/Microsoft VS Code/resources/app/out/cli.js --ms-enable-electron-run-as-node --locate-extension ms-vscode-remote.remote-wsl
6

3 Answers

I think this issue was caused to me by an update of VSCode, which happened automatically.

To solve it:

  1. open VSCode in Windows

  2. open the Remote Explorer

  3. click on a folder in a WSL target of interest: you'll see that this causes some re-installation

  4. now you can go back to using the $ code . command from a folder in WSL2, just like you used to

2

I ended up getting it to work in the end. I believe it was the following that fixed it.

  1. Go to your Terminal and delete your settings.json file. You make wish to save a backup first.

  2. You are going to ctrl-a, delete it all, then SAVE THE FILE.

  3. Very important step. When you go back to your Terminal, click SAVE in the bottom right corner.

  4. Now, this is not fun, but we are going to unregister Ubuntu-20.04 from the powershell.

    Note: This is a destructive operation, and all data inside the WSL instance's filesystem will be lost. Again, before this step, you may wish to take a backup first with wsl --export Ubuntu-20.04 <backup-filename.tar> to be safe. When you are ready, from powershell, write wsl --unregister Ubuntu-20.04 or whatever your Distro is.

  5. Restart your computer!!!

  6. When your computer turns back on, you are going to re-install your distro and set it as default. wsl --install -d <Distribution Name>

  7. Go through the setups, then all I did was code . and got the interaction below. It now works for me.

drewbie@drewbie:~$ cd ~
drewbie@drewbie:~$ ls
drewbie@drewbie:~$ ls -la
total 24
drwxr-xr-x 3 drewbie drewbie 4096 Jan 12 18:12 .
drwxr-xr-x 3 root    root    4096 Jan 12 18:12 ..
-rw-r--r-- 1 drewbie drewbie  220 Jan 12 18:12 .bash_logout
-rw-r--r-- 1 drewbie drewbie 3771 Jan 12 18:12 .bashrc
drwxr-xr-x 2 drewbie drewbie 4096 Jan 12 18:12 .landscape
-rw-r--r-- 1 drewbie drewbie    0 Jan 12 18:12 .motd_shown
-rw-r--r-- 1 drewbie drewbie  807 Jan 12 18:12 .profile
drewbie@drewbie:~$ code .
Installing VS Code Server for x64 (899d46d82c4c95423fb7e10e68eba52050e30ba3)
Downloading: 100%
Unpacking: 100%
Unpacked 1722 files and folders to /home/drewbie/.vscode-server/bin/899d46d82c4c95423fb7e10e68eba52050e30ba3.
2

Since the accepted answer relies on side effects and did not work for me, and the second one is totally unacceptable (complete resinstall of WSL) I searched a bit more and found a safer and effective way:

From this older question in StackOverflow, answered by rrmesquita:

Within the WSL terminal, type rm -r ~/.vscode-server to delete the VS Code WSL server.

Exit the terminal and from your PowerShell/Cmd, run wsl --shutdown . Then you can log in back to your WSL and run code . and it should work normally.

Answering because Google led me here and maybe this will make life easier for someone else =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.