vue.jsvisual-studio-codevitevue-devtools

Opening a file located on WSL with VSCode from the command line


I want to open the file I'm editing from a Vite instance running on WSL using the code command. For this, Vite passes the Linux-compatible path and the -r flag to the code command, but it ends up opening the file in Windows (and not Remote WSL), which fails to load the file properly due to incompatibilities between the systems, although it can find the filename based on the path.

code -r -g /path/to/file.vue

The goal is for VSCode to open the invoked file correctly in the appropriate WSL environment (if multiple WSL distributions are installed), using the Remote WSL extension.

I have no control over the invoked command, as I want to open it through either the vite-plugin-vue-devtools or vite-plugin-vue-inspector.

VSCode is not found on the WSL Linux system itself; it is only present on the Windows system, and it can manage the workspaces on the WSL file system using the Remote WSL extension.


Solution

  • Let's assume ~/bin exists and it is in your PATH, or any other location of your choosing where binaries are stored, like /usr/local/bin.

    On Windows, on a PowerShell command prompt run: Get-Command code | Select-Object Source. This will output the location of the code binary on your system. Something along the lines of C:\Users\<YOUR USER>\AppData\Local\Programs\Microsoft VS Code\bin\code.cmd. Note it is a CMD script, this will be important later.

    Now on the WSL instance of your choice you can run: ln -s "$(wslpath -a 'C:\Users\<YOUR USER>\AppData\Local\Programs\Microsoft VS Code\bin\code')" ~/bin/code. Pay close attention to the file name, .cmd was intentionally removed.

    code is now a "native" command on your WSL distribution. The conversion between UNC and Posix paths is handled automatically by WSL.

    Behind the scenes WSL is using binfmt_misc to run the commands with a custom interpreter that handles interoperability.

    You can learn more about this feature at Run Windows Tools From Linux