Score:3

WSL2 - VSCode's shell command `code` no longer working in Linux terminal

ng flag

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

After inputing code . it just sits there

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
NotTheDr01ds avatar
vn flag
Can you uninstall and reinstall the *Remote - WSL* extension in VSCode? At worst, how about uninstalling and reinstalling VSCode itself? I don't usually resort to "reinstall" as the first go-to- answer, but given (a) you aren't sure what you deleted, and (b) reinstalling is pretty quick and painless, I think it's worth a shot first.
drewkiimon avatar
ng flag
@NotTheDr01ds I uninstalled and re-installed `Remote - WSL` extension, but still it did not fix it. I am a bit hesitant in uninstalling VSCode. As a last hail mary, maybe. But not yet.
NotTheDr01ds avatar
vn flag
Fair enough. `code` is actually a shell script, so you can try debugging it to potentially see where it's hanging. `vi $(which code)` and add `set -x` as the first line after the shebang. Then try running and add the output to the question. I can compare it to mine, at least.
drewkiimon avatar
ng flag
I tried to do what you did, with no avail. :( I was able to figure out which code it is using and where it is. Not sure if there's some symlink I need to do? I see that thrown around sometimes.
NotTheDr01ds avatar
vn flag
Ah, sorry, it's probably due to the spaces in the path. That's my bad - I should know to quote things like that. Try it with quotes - `vi "$(which code)"`?
drewkiimon avatar
ng flag
@NotTheDr01ds I got it to work! Posting it in my question above. Thank you so much for your help so far!
Score:3
jp flag

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

    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

drewkiimon avatar
ng flag
Dang. I'd check this, but I decided just to dual boot Linux instead on a separate drive. If anyone can validate this I'll make it the answer!
mickmackusa avatar
ve flag
@Ando despite being a member for over 7 years, you still haven't taken the [tour]; please do.
Score:2
ng flag

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. The said file opened from the terminal

  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. Save button click it!

  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.
NotTheDr01ds avatar
vn flag
Excellent - Good to hear you got it working! A bit drastic anyway, with the WSL/Ubuntu removal and the Windows Terminal settings removal, but glad you are past the problem. I'm going to make a slight edit to your answer for the sake of "safety", but no biggie.
drewkiimon avatar
ng flag
@NotTheDr01ds thank you for your revision! It's true they will lose everything on their instance :(
Score:0
zm flag

None of the reasonable solutions for this problem worked for me on a fresh laptop. The root cause turned out to be the order I performed my setup in.

My error: Prior to launching code I had setup the /etc/wsl/conf file to append the Windows Path to the wsl Path as false. These /mnt/c/Windows/paths can cause issues when doing various development tasks, but apparently they are needed initially for code to work.

[interop]
appendWindowsPath = false

So, when I went to launch code . within wsl it didn't know where to find it, as a result.

Fix:

Allow the windows paths to be appeneded

sudo nano /etc/wsl.conf

[interop]
appendWindowsPath = true

Exit out of wsl and then shut it down wsl --shutdown

Get back into wsl and launch code code .

It should install.

Optional:

You can then remove the windows paths from the configuration file once more and add the path in the ~/.bashrc file:

export PATH="/mnt/c/Program Files/Microsoft VS Code/bin:$PATH"

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.