简体   繁体   中英

WSL2: Launching VSCode from the command line using a *.code-workspace file

I have Windows 10 + WSL2 with the latest version of VSCode. I also have a simple.code-workspace file which I can double-click (from within the windows file-explorer) and launch VSCode in a way that it pops-up already attached to a specific docker-container that is up-and-running.

This works great with double-clicking on the.code-workspace file:

{
    "folders": [
        {
            "uri": "vscode-remote://attached-container+7b2...27d/workspace/foobar"
        }
    ],
    "remoteAuthority": "attached-container+7b2...27d",
    "settings": {}
}

Apart from double-clicking I can also invoke VSCode from Windows command-line (dos) and this works ok too:

  "C:\Program Files\Microsoft VS Code\Code.exe" "C:\path\to\foobar.code-workspace"

However when I try to open the workspace from within WSL2:

  code ./foobar.code-workspace

Even though VSCode pops-up the workspace doesn't open properly. What should I do to make the WSL2 (bash) command-line work the same way as in Windows?

And to answer my own question:

  "/mnt/c/Program Files/Microsoft VS Code/Code.exe"   ./foobar.code-workspace

Strangely enough this does the trick.

To make the script a bit more dynamic I guess one could write something like:

  "$(which code | xargs -0 dirname | xargs -0 dirname)/Code.exe"  ./foobar.code-workspace

Hope this helps some folks out there save a few hours worth of time.

At the end of the day wsl is installed on top of a windows environment you could also a.bat or.ps1 file and adds the commands they require without the need for the workspace. For example I have my script like this: wsl (cd ~/Projects; code./test; command3; command4) exit inside the parentheses put all the commands you need and exit closes the terminal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM