11

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
6
  • 1
    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. Jan 13, 2022 at 1:05
  • @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.
    – drewkiimon
    Jan 13, 2022 at 1:12
  • 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. Jan 13, 2022 at 1:30
  • 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.
    – drewkiimon
    Jan 13, 2022 at 1:44
  • 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)"? Jan 13, 2022 at 1:47

7 Answers 7

7

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

3
  • 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!
    – drewkiimon
    Feb 5, 2022 at 19:00
  • 1
    @Ando despite being a member for over 7 years, you still haven't taken the tour; please do. Feb 6, 2022 at 1:00
  • This didn't work for me but it was helpful in getting me to realise that the problem had started after I added a remote ssh connection.
    – TrojanName
    Sep 22, 2023 at 10:03
7

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 =)

7
  • this fixed my VS Code Internal Terminal not opening up in CWD
    – Hassan
    Jul 11, 2022 at 10:26
  • 1
    +1 for me, this worked a treat. Incidentally code . --verbose was also helpful as it showed me that vs code was just hanging, initially I had assumed it was silently exiting every time I tried to invoke it. Jan 4, 2023 at 21:21
  • 1
    Note: adding "/mnt" to $PATH will cause high input latency on WSL2 terminal. Jan 23, 2023 at 9:29
  • Before i try this, can anyone confirm if you lose all the settings for that folder/workspace?
    – TrojanName
    Sep 22, 2023 at 10:05
  • @TrojanName you could always just do mv instead of rm if you're afraid it will mess your stuff. Backup =) Sep 23, 2023 at 11:12
5

I just added this to my .bashrc:

alias code='/mnt/c/Users/[YOUR USER DIR NAME]/AppData/Local/Programs/Microsoft\ VS\ Code/bin/code'
3

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.
3
  • 1
    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. Jan 13, 2022 at 2:40
  • @NotTheDr01ds thank you for your revision! It's true they will lose everything on their instance :(
    – drewkiimon
    Jan 13, 2022 at 3:15
  • That solved my problem! Thanks @drewkiimon Sep 6, 2022 at 19:40
3

Mine works a bit different compared to @cloudcreatordotio , I add this line to my .bashrc and steps shown below:

  1. open your ubuntu shell

  2. open the .bashrc

    nano .bashrc
    
  3. add the following line

    alias code="'/mnt/c/Users/[YOUR USER DIR NAME]/AppData/Local/Programs/Microsoft VS Code/Code.exe'"
    
0

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"

0

In a PowerShell:

wsl --update
wsl --shutdown

This solved the problem for me.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .