SudoKillMe / vscode-extensions-open-in-browser

81 stars 42 forks source link

Support WSL #53

Open kemingy opened 4 years ago

kemingy commented 4 years ago

Currently, it seems this extension doesn't work in WSL.

Here is an example for the error message:

Windows can't find '\mnt\c\ {some path} \index.html'. Make sure you typed the name correctly, and then try agian.

This should be replace to 'C:/ {some path}/ index.html'.

Marshal27 commented 3 years ago

This is a "hacky" solution, but it got me going using this extension onWSL2 and Windows 10 Pro 19041.685.

In index.ts I made the following changes, compiled and manually replaced the contents in %USERPROFILE%\.vscode\extensions\techer.open-in-browser-2.0.0\out\index.js

Import child process to run commands against cmd prompt in windows. import * as cp from 'child_process';

Added a const to convert windows path into unix friendly. const convertPath = (windowsPath) => windowsPath.replace(/^\\\\\?\\/,"").replace(/\\/g,'\/').replace(/\/\/+/g,'\/')

Added helper function to run shell commands against cmd prompt

const execShell = (cmd: string) =>
    new Promise<string>((resolve, reject) => {
        cp.exec(cmd, (err, out) => {
            if (err) {
                return reject(err);
            }
            return resolve(out);
        });
    });

Converted openDefault to async and used wsl.exe to run the wslpath command from a dos prompt and return the output.

export const openDefault = async (path: any): Promise<void> => {
  let uri;
  if (path) {
    uri = await execShell('wsl wslpath -w ' + convertPath(path.fsPath));
  } else {
    const _path = currentPageUri();
    uri = _path && _path.fsPath;
  }
  const browser = standardizedBrowserName(defaultBrowser());
  open(uri, browser);
};
Temepest74 commented 2 years ago

please add support by default, it is needed

dovwatbnh commented 6 months ago

Would love to have this as well

romanlex commented 1 month ago

what about this issue?