25

I have the Creators update installed. WSL is operational. I can execute most .exe files by simply calling notepad.exe But when it comes to VS Code.... I can't use the default code command or call code.exe... I have also tried code and code.cmd. Why doesn't VS Code execute like other programs? And is there a way to enable the code command?

EDIT: I now get these errors:

me@mypc:/mnt/c/Users/me/Documents/project_folder$ code . /mnt/c/Program Files (x86)/Microsoft VS Code/bin/code: line 7: realpath: comma nd not found /mnt/c/Program Files (x86)/Microsoft VS Code/bin/code: line 14: ./Code.exe: No such file or directory

Auzy
  • 2,075
  • 2
  • 22
  • 34

5 Answers5

27

The Creators update did install the interop functionality. However, it seems you need to install realpath in WSL in order for the path to be recognized. I'm not sure why this is the case but running sudo apt-get install realpath fixed it for me!

EDIT: After updating to the Fall Creators Update launching VSCode from WSL works out of the box

Auzy
  • 2,075
  • 2
  • 22
  • 34
  • This might be helpful to others if you have a simliar issue and are using zsh https://medium.com/@finnzeit/set-and-use-zsh-as-default-shell-in-wsl-on-windows-10-the-right-way-4f30ed9592dc – Samwise Dec 05 '17 at 09:37
19

You could always create an alias in the WSL Bash shell

alias code="/mnt/c/Program\ Files/Microsoft\ VS\ Code/Code.exe"

If you add this to your .bashrc or .zshrc file then it will always be available when you start a new instance of your environment.

Damo
  • 4,532
  • 3
  • 30
  • 49
3

WARNING: Do not change Linux files using Windows apps and tools

Update: The above is not relevant to version 1903 or newer.

With that out of the way, I recently experienced the exact same behavior. Turns out, in my noobish first whack at WSL with VS Code I also installed a native copy from apt which overwrote the Windows path.

So in WSL I removed it with something like this...

$ sudo apt remove code -y; sudo apt autoremove -y

Then confirmed the path was correct...

$ which code
/mnt/c/Program Files/Microsoft VS Code/bin/code

Then relaunched WSL terminal and all was well in the wonderful world of coding once again. :)

Note: I suppose another option would be to fix the path in WSL, but without a GUI package installed it will just launch in the background and never appear anyway.

Chiramisu
  • 4,589
  • 7
  • 44
  • 77
2

Another alternative is to use the following:

cmd.exe /c code

This also works for VSCodium:

cmd.exe /c codium
Daniel
  • 7,256
  • 5
  • 54
  • 77
0

I had the same problem after manually updating Ubuntu on wsl2. I solved this by adding this function to .bashrc config file. It's better solution than alias because it does not block the console.

Before adding it to your config test the function in bash and adjust the path if needed. For sure, you must change {username} to your Windows account username.

function code () { /mnt/c/Users/{username}/AppData/Local/Programs/Microsoft\ VS\ Code/Code.exe; }

If that work add it to your configuration script e.g. .bashrc, .zshrc. Here's a script that does that.

echo "function code () {
  /mnt/c/Users/{username}/AppData/Local/Programs/Microsoft\ VS\ Code/Code.exe \"\$@\"; 
}" >> ~/.bashrc
source ~/.bashrc
code .

Although my recommendation is to make backup and install fresh distro from Microsoft Store then open VS Code with Remote - WSL extension. If there came out an error with initializing WSL Extensions reinstall it first. If you used 19.04 Ubuntu and upgraded to 20.04 it sometimes needs to download initialization scripts for that version or it fails. That should help :)

proximab
  • 1,299
  • 11
  • 17
  • Code's Linux Binary lives in .\bin in the VS Code's installation dir. This worked for me: ``function code () { /mnt/c/Program\ Files/Microsoft\ VS\ Code/bin/code "$@"; }`` Don't forget the "$@" as that passes any file or folder into the workspace. – JonShipman Jun 18 '21 at 20:55