Skip to content

Commit 3701a50

Browse files
committed
browserutils: support $BROWSER variable on Linux
On Linux we now attempt to use the $BROWSER environment variable to launch a URL in 'the default' browser. This is preferred ahead of any 'shell execute' handlers. Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>
1 parent d31f62c commit 3701a50

1 file changed

Lines changed: 38 additions & 8 deletions

File tree

src/shared/Core/BrowserUtils.cs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,19 @@ public static void OpenDefaultBrowser(IEnvironment environment, Uri uri)
2929
if (PlatformUtils.IsLinux())
3030
{
3131
//
32-
// On Linux, 'shell execute' utilities like xdg-open launch a process without
32+
// On Linux we try and use the $BROWSER environment variable first if set.
33+
// Otherwise, we try a set of known 'shell execute' utilities.
34+
//
35+
if (TryGetBrowserEnvironment(environment, out string browser))
36+
{
37+
psi = new ProcessStartInfo(browser, url)
38+
{
39+
RedirectStandardOutput = true,
40+
RedirectStandardError = true,
41+
};
42+
}
43+
//
44+
// Sadly, 'shell execute' utilities like xdg-open launch a process without
3345
// detaching from the standard in/out descriptors. Some applications (like
3446
// Chromium) write messages to stdout, which is currently hooked up and being
3547
// consumed by Git, and cause errors.
@@ -41,17 +53,19 @@ public static void OpenDefaultBrowser(IEnvironment environment, Uri uri)
4153
// We try and use the same 'shell execute' utilities as the Framework does,
4254
// searching for them in the same order until we find one.
4355
//
44-
if (!TryGetLinuxShellExecuteHandler(environment, out string shellExecPath))
56+
else if (!TryGetLinuxShellExecuteHandler(environment, out string shellExecPath))
4557
{
4658
throw new Exception("Failed to locate a utility to launch the default web browser.");
4759
}
48-
49-
psi = new ProcessStartInfo(shellExecPath, url)
60+
else
5061
{
51-
RedirectStandardOutput = true,
52-
// Ok to redirect stderr for non-git-related processes
53-
RedirectStandardError = true
54-
};
62+
psi = new ProcessStartInfo(shellExecPath, url)
63+
{
64+
RedirectStandardOutput = true,
65+
// Ok to redirect stderr for non-git-related processes
66+
RedirectStandardError = true
67+
};
68+
}
5569
}
5670
else
5771
{
@@ -67,6 +81,22 @@ public static void OpenDefaultBrowser(IEnvironment environment, Uri uri)
6781
Process.Start(psi);
6882
}
6983

84+
public static bool TryGetBrowserEnvironment(IEnvironment env, out string value)
85+
{
86+
// We can check the BROWSER environment variable to see if a specific
87+
// browser has been requested by the user.
88+
if (PlatformUtils.IsLinux() || PlatformUtils.IsMacOS())
89+
{
90+
if (env.Variables.TryGetValue("BROWSER", out value) && !string.IsNullOrWhiteSpace(value))
91+
{
92+
return true;
93+
}
94+
}
95+
96+
value = null;
97+
return false;
98+
}
99+
70100
public static bool TryGetLinuxShellExecuteHandler(IEnvironment env, out string shellExecPath)
71101
{
72102
// One additional 'shell execute' utility we also attempt to use over the Framework

0 commit comments

Comments
 (0)