paulirish / git-open

Type `git open` to open the GitHub page or website for a repository in your browser.
MIT License
3.29k stars 246 forks source link

Fix opening browser on wsl2 #167

Closed wispamulet closed 3 years ago

wispamulet commented 4 years ago

Hi!

I'm trying to use this on my Windows10 with WSL2, when browser directory is on Windows, normally it should be something like this /mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe.

If a user has set $BROWSER to this, it cannot open browser automatically. It seems like when ${BROWSER:-$open} invoked it cannot handle the spaces in $BROWSER

So adding double quotes around it would simply solve it.

I'm not very familiar with shell script. And I've tested on my local machine and they all passed. Hope this will help. 😂

derimagia commented 4 years ago

Spaces in BROWSER are allowed so this would sadly break that. xdg-open is the main script that deals with BROWSER and it allows it: https://github.com/freedesktop/xdg-utils/blob/56991bc165577f011f9ad7ca721c5a5134710e33/scripts/xdg-open.in#L374-L400.

Upstream issue is https://github.com/Microsoft/WSL/issues/1766

Does unsetting BROWSER not work for you? It looks like there's existing code that would use your default browser anyway. If this doesn't work then I would accept the PR if it only quotes it for WSL

wispamulet commented 4 years ago

Conclusion

  1. Unsetting BROWSER works for me
  2. Adding quotes with BROWSER also works as well in some situation
  3. Maybe it's not git-open's problem and it should not be fixed like this 🙄
  4. I'm confused with white spaces now

Test

I'm trying to understand the logic of git open, from here

if uname -r | grep -q -i Microsoft; then
              open='powershell.exe Start'
            else
              open='xdg-open'
            fi;;

it says if it's on WSL, it would use powershell to open a url, ${BROWSER:-$open} "$openurl" would turn into powershell.exe Start someurl. I can manually type this command and it works as well.

I've tested something else, like github cli and xdg-open itself.

Situation 1, when there is no BROWSER

# ✅ succussfully open remote repo in my browser, essentially it's using powershell.exe to handle url with default browser on my windows
$ git open

# ❌ failed
# xdg-open: no method available for opening 'https://github.com/me/my-remote-url'
$ gh repo view --web

# ❌ failed
# xdg-open: no method available for opening 'https://github.com/me/my-remote-url'
xdg-open "https://github.com/me/my-remote-url"

Situation 2, when export BROWSER="/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe"

# ❌ failed
# ✅ adding quotes would solve this, e.g. ${BROWSER:-$open} "$openurl" => "${BROWSER:-$open}" "$openurl"
$ git open

# ❌ failed
# fork/exec /mnt/c/Program: no such file or directory
$ gh repo view --web

# ❌ failed
# /usr/bin/xdg-open: 870: /usr/bin/xdg-open: /mnt/c/Program: not found
# xdg-open: no method available for opening 'https://github.com/me/my-remote-url'
$ xdg-open "https://github.com/me/my-remote-url"

# ✅ success
$ $BROWSER "https://github.com/me/my-remote-url"

Situation 3, when export BROWSER="/mnt/c/Program\ Files\ (x86)/Google/Chrome/Application/chrome.exe", with back slash before white space

# ❌ failed
# ❌ adding quotes would not solve this either
$ git open

# ✅ success
# it seems like it's using xdg-open behind the scene, but somehow it just work 😂
$ gh repo view --web

# ❌ failed
# /usr/bin/xdg-open: 870: /usr/bin/xdg-open: /mnt/c/Program\: not found
# xdg-open: no method available for opening 'https://github.com/me/my-remote-url'
$ xdg-open "https://github.com/me/my-remote-url"

# ❌ failed
# zsh: no such file or directory: /mnt/c/Program\ Files\ (x86)/Google/Chrome/Application/chrome.exe
$ $BROWSER "https://github.com/me/my-remote-url"
derimagia commented 3 years ago

It looks like there is also a script wslview in WSL: https://github.com/cli/cli/issues/826

This means this should work for other things as well:

export BROWSER=wslview

Would accept a PR to automatically use this but for now you can either unset it or use the wslview script that's included in WSL.

wispamulet commented 3 years ago

Thx for this hint! This is what I'm looking for 😍

For me, this is not included in distro by default and I install it manually from wslu Maybe someone could add this tip in README