Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for WSL #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions browser_linux.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
package browser

import (
"errors"
"fmt"
"io/ioutil"
"os/exec"
"strings"
)

func openBrowser(url string) error {
providers := []string{"xdg-open", "x-www-browser", "www-browser"}

wsl, err := isWSL()
if err != nil {
return errors.New("error determining Windows Subsystem for Linux")
}

if wsl {
cmd := "cmd.exe"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linux systems under WSL ship with a wslview utility available in PATH that can be used in place of xdg-open. Therefore, the fix for this would be to just add wslview as fallback to providers above; no detection necessary nor spawning cmd.exe.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback. I started a fresh install of Windows 10, and wslview doesn't seem to exist in Linux WSL2 installations by default. However, installing WSL utilities adds wslview and creates a symbolic link for x-www-browser, which resolves the original issue.

$ apt install wslu
Reading package lists... Done
....
update-alternatives: using /usr/bin/wslview to provide /usr/bin/x-www-browser (x-www-browser) in auto mode
update-alternatives: using /usr/bin/wslview to provide /usr/bin/www-browser (www-browser) in auto mode
$ ls -la /usr/bin/x-www-browser
lrwxrwxrwx 1 root root 31 Aug  7 00:20 /usr/bin/x-www-browser -> /etc/alternatives/x-www-browser

$ ls -la /etc/alternatives/x-www-browser
lrwxrwxrwx 1 root root 16 Aug  7 00:20 /etc/alternatives/x-www-browser -> /usr/bin/wslview

If the implementation above is an overhead to the package, I'm happy to close the PR in favor of having wslview utility as a required dependency in WSL installations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed dive. I see added value in your approach being compatible even if WSL utilities aren't installed, but I'm surprised that the cmd.exe approach works. Do Linux subsystems contain a cmd.exe in their PATH that, when executed, is connected to the host Windows system? Sorry, I'm not very experienced with WSL, and I don't have a Windows instance I can try on at the moment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do believe that the providers approach should be tried before the isWSL block.

args := []string{"/c", "start"}
url := strings.ReplaceAll(url, "&", "^&")
args = append(args, url)
return exec.Command(cmd, args...).Start()
}

// There are multiple possible providers to open a browser on linux
// One of them is xdg-open, another is x-www-browser, then there's www-browser, etc.
// Look for one that exists and run it
Expand All @@ -19,3 +35,11 @@ func openBrowser(url string) error {

return &exec.Error{Name: strings.Join(providers, ","), Err: exec.ErrNotFound}
}

func isWSL() (bool, error) {
data, err := ioutil.ReadFile("/proc/version")
if err != nil {
return false, fmt.Errorf("read /proc/version: %v", err)
}
return strings.Contains(strings.ToLower(string(data)), "microsoft"), nil
}