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

Fix browser open when inside WSL #1074

Merged
merged 1 commit into from
Mar 24, 2022
Merged
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
29 changes: 24 additions & 5 deletions lib/livebook/utils.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Livebook.Utils do
@moduledoc false

require Logger

@type id :: binary()

@doc """
Expand Down Expand Up @@ -308,14 +310,31 @@ defmodule Livebook.Utils do
Opens the given `url` in the browser.
"""
def browser_open(url) do
{cmd, args} =
win_cmd_args = ["/c", "start", String.replace(url, "&", "^&")]

cmd_args =
case :os.type() do
{:win32, _} -> {"cmd", ["/c", "start", String.replace(url, "&", "^&")]}
{:unix, :darwin} -> {"open", [url]}
{:unix, _} -> {"xdg-open", [url]}
{:win32, _} ->
{"cmd", win_cmd_args}

{:unix, :darwin} ->
{"open", [url]}

{:unix, _} ->
cond do
System.find_executable("xdg-open") -> {"xdg-open", [url]}
# When inside WSL
System.find_executable("cmd.exe") -> {"cmd.exe", win_cmd_args}
true -> nil
end
end

System.cmd(cmd, args)
case cmd_args do
{cmd, args} -> System.cmd(cmd, args)
nil -> Logger.warn("could not open the browser, no open command found in the system")
end

:ok
end

@doc """
Expand Down