Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start Atom or VSCode natively installed on Windows from within WSL (Ubuntu)?

I have installed Atom editor natively on Windows 10 by downloading an running the installer. Now I start WSL Ubuntu distro and want to start Atom (atom-editor) from there with the command atom . or VSCode (visual-studio-code) with the command code .

Atom starts, but not in the directory where the command was executed, instead it shows files from C:\\Windows. Moreover Ubuntu WSL terminal shows following error message:

atom .
grep: /etc/wsl.conf: No such file or directory
"\\wsl$\Ubuntu-18.04\home\wlad\projects\udemy\flask-bootcamp\Flask-Bootcamp-master"
CMD.EXE wurde mit dem oben angegebenen Pfad als aktuellem Verzeichnis gestartet.
UNC-Pfade werden nicht unterstützt.
Stattdessen wird das Windows-Verzeichnis als aktuelles Verzeichnis gesetzt.

Sorry it's German localized but it says something like UNC-paths are not supported

(haven't tested VSCode yet)

So how can I use Atom or VSCode editor installed on Windows 10 from within WSL?

** UPDATE ** As of today (April 2020) there is a much better way to use VSCode on Windows w/ WSL, VirtualMachines (VM) and even Containers. Check out remote-development plugin for VSCode.

like image 301
Wlad Avatar asked Jul 10 '19 18:07

Wlad


People also ask

How do I start VS Code in WSL?

Alternatively, you can open a Remote WSL window directly from VS Code: Start VS Code. Press F1, select Remote-WSL: New Window for the default distro or Remote-WSL: New Window using Distro for a specific distro. Use the File menu to open your folder.

Can I Install VS Code in WSL?

Install VS Code and the Remote WSL extensionInstall Visual Studio Code on Windows (not in your WSL file system). When prompted to Select Additional Tasks during installation, be sure to check the Add to PATH option so you can easily open a folder in WSL using the code command.

How do I run a Windows program in WSL?

Run Windows tools from Linux WSL can run Windows tools directly from the WSL command line using [tool-name].exe . For example, notepad.exe . Applications run this way have the following properties: Retain the working directory as the WSL command prompt (for the most part -- exceptions are explained below).

How do I start WSL on Windows Ubuntu?

To enable WSL, click Windows Start and search for Windows Features. When the Turn Windows Features on or off item displays, select it. Enable WSL in Windows Features. Once the list populates, scroll down to Windows Subsystem for Linux, and ensure that it's checked.


Video Answer


3 Answers

I created a short script to handle the three atom commands I use most (I use Ubuntu with WSL):

  1. atom
  2. atom .
  3. atom RELATIVE_PATH_FILE

This script is not optimized, and I'm sure many people will find edge cases, but it gets the job done for me. To use it, simply placed it in ~/.local/bin/atom and make it executable by running chmod +x ~/.local/bin/atom. You may need to restart your shell for ~/.local/bin to be added to your path. In order to simplify things a bit, I mapped the WSL network drive for my ubuntu installation to U:, so you'll either want to do the same or modify the script accordingly on line 9.

#!/bin/bash

if [ -z $1 ]; then
  pushd /mnt/c > /dev/null
  /mnt/c/Windows/System32/cmd.exe /c "atom"
else
  [[ $1 = "." ]] && path=$(pwd) || path=$(realpath $1)

  winPath=$(echo "U:$path" | sed -e 's/\//\\/g')

  pushd /mnt/c > /dev/null
  /mnt/c/Windows/System32/cmd.exe /c "atom $winPath"
fi

popd > /dev/null

The script performs a few simple steps. First, if there is no command line argument, it simply calls atom using cmd.exe without arguments. If the command line argument is ., it gets the path to the current directory, otherwise, it gets the absolute path to the given file using realpath. The path is converted from POSIX to Windows format using sed before calling atom using cmd.exe as before, but with the path argument.

The pushd and popd commands are just there to get rid of the annoying warning message about UNC paths not being supported:

...
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory
like image 59
Eduardo Avatar answered Oct 10 '22 02:10

Eduardo


In the "Known issues" section of the blog post @Wlad mentioned, there states

Accessing Linux files is treated the same as accessing a network resource, and any rules for accessing network resources will still apply e.g: When using CMD, cd \\wsl$\Ubuntu\home will not work (as CMD does not support UNC paths as current directories), however copy \\wsl$\Ubuntu\home\somefile.txt C:\dev\ will work

So as Atom may use cmd.exe to launch itself from the command line (maybe some batch file), and given the fact that cmd.exe cannot open network resources as current directory (which WSL directory is treated as), there came the failure as you attempted to launch Atom from WSL shell.

Actually, in VS Code there is a better solution to launch VS Code directly from the WSL shell: VS Code Remote.

You can take the following steps to enable VS Code to be directly launched from WSL shell:

  1. Install the extension Remote - WSL to VS Code on the Windows side;
  2. Then when you type code . in your WSL shell, VS Code Remote Server will be automatically installed, and VS Code will soon launch.

By using VS Code Remote, you can not only open the directory in VS Code, but can also be benefited in many other aspects: for example, you can use the WSL shell as the integrated shell in VS Code and run programs in WSL directly from VS Code.

Here is the official doc for VS Code Remote - WSL.

like image 30
Yang Hanlin Avatar answered Oct 10 '22 02:10

Yang Hanlin


The script in Eduardo's answer is a great approach, but didn't allow to open multiple directories/repos at once (e.g. atom terraform-modules terraform-repo), which I do often.

The following is my twist on it:

#!/bin/bash

winPathPrefix="U:"

function convertToWinPath() {
  echo "${winPathPrefix}$(realpath ${1})" | sed -e 's/\//\\/g'
}

declare -a atomCmd=(/mnt/c/Windows/System32/cmd.exe /c "atom")

for path in "$@"; do
  atomCmd+=($(convertToWinPath ${path}))
done

${atomCmd[@]} 2>/dev/null

That is entirely based on Eduardo's script and should allow a more general use case

like image 2
Tom Klino Avatar answered Oct 10 '22 02:10

Tom Klino