Christopher Oezbek
Christopher Oezbek

Reputation: 26313

URL for opening vscode in file in WSL workspace

I would like to generate clickable links in the exception handler of my web application that open the corresponding source code in vscode.

Visual Studio Code provides the vscode://file/ schema handler for this as documented under command line docs.

Unfortunately, this does not understand file links which are inside the Windows Subsystem for Linux.

Any idea how to generate links which open the code editor on a particular file (+ line)?


Edit: It seems it doesn't currently work and it did not make the backlog:

https://github.com/microsoft/vscode/issues/111188


Edit 2:


Edit 3: Yeah! As mentioned in Ryan's answer there is now a way to open files by appending line numbers to the URL:

`vscode://vscode-remote/wsl+${WSL_DISTRO_NAME}/path/to/file.ext:1`

With this I can finally click links in the Better Error Gem to open in VSCode.

Note: If the file does not exist then a new workspace is opened unfortunately.

Upvotes: 4

Views: 1693

Answers (1)

Ryan Wheale
Ryan Wheale

Reputation: 28380

I found this thread to be the most helpful for finding accurate information:

https://github.com/microsoft/vscode/issues/108257

The OP was pretty much there, but I wanted to give a full rundown:

  • It is not possible to stat a remote file path to determine if it's a directory or a file (comment).
  • Therefore, all paths are considered a directory unless the path ends in :linenumber[:column] (comment)
  • You can simply append :1 to the path to make sure it's treated as a file.

Here are some examples of urls which worked:

vscode://vscode-remote/wsl+Ubuntu-18.04/home/me/src/myexpressapp/app.js:1
vscode://vscode-remote/wsl+Ubuntu-18.04/home/me/src/myexpressapp/app.js:1:20
vscode://vscode-remote/wsl+Ubuntu-18.04/home/me/src/myexpressapp/app.js:20:1
vscode://vscode-remote/wsl+Ubuntu-18.04/home/me/src/myexpressapp/

Upvotes: 2

Related Questions