DEV Community

elizabeth
elizabeth

Posted on

explorer.exe Exit Code 1 in WSL: Fix BROWSER False Failure

You set BROWSER=explorer.exe, run a CLI that wants to open a sign-in page, and the Windows browser opens — then the CLI says no browser opened. The launch worked; the status check did not. This tutorial shows how to reproduce that false failure, why explorer.exe returns 1 after a successful hand-off, and how to build a small BROWSER wrapper that accepts only that hand-off status while still propagating real errors. It also tests the wrapper under set -e and shows where to export it.

Table of Contents

Reproduce the False Failure

Start with the launcher by itself:

set +e
explorer.exe https://example.com
printf 'explorer status: %d\n' "$?"
Enter fullscreen mode Exit fullscreen mode

On an affected WSL setup, the default Windows browser opens and the status is 1. That is the whole mismatch in one command: the URL reached Windows, but the Linux process saw a non-zero exit.

Now put it behind BROWSER the way a CLI sees it:

set +e
export BROWSER=explorer.exe
"$BROWSER" https://example.com
status=$?
set -e
printf 'BROWSER status: %d\n' "$status"
Enter fullscreen mode Exit fullscreen mode

The browser still opens, and the status is still 1. A CLI that treats any non-zero status as failure will report “no browser opened” and may tell you to set BROWSER, even though it is already set. A bug report for the Pseudolife-MCP console documents exactly this pattern: export BROWSER=explorer.exe opens the browser, then the console prints explorer.exe failed (exit status: 1) and advises setting BROWSER. The report is useful because it separates the launch from the status check.

Use this quick diagnosis before building anything:

What you see What it means What to do
Browser opens, status 1 Explorer hand-off, false failure Build the wrapper
Browser opens, status 0 No exit-code problem Check the CLI’s detection logic
Browser does not open, status 1 Real launch problem Fix the URL, interop, or default browser
explorer.exe not found or 127 WSL interop or Windows PATH Fix interop first

If command -v explorer.exe prints nothing, the wrapper cannot help. Microsoft’s WSL interop documentation explains that Windows executables are called by their full name, including .exe, and resolve through the Windows PATH rather than the Linux PATH. Fix that first.

Also test the exact URL your CLI uses, not just https://example.com. Query strings and fragments can behave differently, and a hand-off status cannot tell you that the right page actually rendered.

Why Explorer Returns Exit Code 1

explorer.exe is a Windows shell process, not a console browser. When WSL starts it, Explorer hands the URL to Windows and the default browser opens. The Linux process sees Explorer’s exit status, not the browser’s. On affected WSL builds, that hand-off status is 1 even though the browser opened. The MCP console report treats that 1 as a failure; that is the false negative.

“Hand-off” is the key word: Explorer passed the target to Windows and exited. It does not mean the browser finished loading the page, and it does not report the browser’s own status.

set -e turns the false negative into an abrupt exit. The Bash manual’s description of set -e says the shell exits immediately when a simple command returns non-zero, with exceptions for commands in if or while tests, && or || lists, and commands inverted with !. A CLI that runs $BROWSER "$url" as an ordinary command under set -e can exit before its own error handling runs.

Tools that launch the browser in the background have a similar problem. A settle window is a short delay a launcher waits before deciding the browser did not start. The MCP console report says its POSIX launcher treats any non-zero exit inside a 2-second settle window as failure. Explorer exits 1 quickly after the hand-off, so the health check fires even though the page opened.

The tempting one-line fix is:

export BROWSER='explorer.exe || true'
Enter fullscreen mode Exit fullscreen mode

Do not use it. BROWSER is a command value, not a shell expression. The xdg-open manual’s description of BROWSER says the value is split on spaces, quoting is not supported, and the URL is appended as the last argument. It also supports a colon-separated fallback list and a %s placeholder. With explorer.exe || true, a consumer that does not run a shell can pass || and true as literal arguments, or append the URL after true. Either way, the launch is no longer reliable.

Even at the call site, "$BROWSER" "$url" || true hides every failure: missing Explorer, disabled interop, permission errors, and genuine launch failures. You want to ignore only the status that means “handed off.”

Build a Status-Aware BROWSER Wrapper

Create a small executable that validates the target, checks that Explorer exists, accepts 0 and 1, and propagates everything else.

mkdir -p "$HOME/.local/bin"
cat >"$HOME/.local/bin/wsl-browser" <<'EOF'
#!/bin/bash
# Launch an HTTP(S) URL in the Windows default browser from WSL.
# explorer.exe can return 1 after a successful hand-off, so 1 is
# accepted here. Other non-zero statuses are real failures.

set -u

if [ "$#" -ne 1 ]; then
  printf 'wsl-browser: expected exactly one URL, got %d argument(s)\n' "$#" >&2
  exit 2
fi

url=$1
case "$url" in
  http://*|https://*) ;;
  *)
    printf 'wsl-browser: refusing non-HTTP(S) target: %s\n' "$url" >&2
    exit 2
    ;;
esac

if ! command -v explorer.exe >/dev/null 2>&1; then
  printf 'wsl-browser: explorer.exe not found; check WSL interop and PATH\n' >&2
  exit 127
fi

if explorer.exe "$url"; then
  exit 0
else
  status=$?
  if [ "$status" -eq 1 ]; then
    # Explorer's hand-off status on affected WSL builds.
    exit 0
  fi
  printf 'wsl-browser: explorer.exe failed with status %d\n' "$status" >&2
  exit "$status"
fi
EOF
chmod +x "$HOME/.local/bin/wsl-browser"
Enter fullscreen mode Exit fullscreen mode

Check the syntax:

bash -n "$HOME/.local/bin/wsl-browser"
Enter fullscreen mode Exit fullscreen mode

Why this shape:

  • The case rejects missing and non-HTTP(S) targets before Explorer runs. A malformed argument is a real usage error, not a hand-off.
  • command -v turns missing Explorer or broken interop into 127 instead of a false success.
  • The if explorer.exe context lets the wrapper inspect the status without set -e aborting the script. The wrapper itself does not use set -e; it needs to handle the status.
  • Only 0 and 1 return success. 2, 126, 127, and anything else propagate unchanged.
  • There is no global || true.

If your Explorer returns 0, the wrapper passes that through too. The wrapper does not make Explorer’s status truthful; it translates the known ambiguous status into the answer the CLI needs.

If you are still deciding between the direct explorer.exe one-liner, the BROWSER export, wslview, and an open shim, the four methods for opening the host browser from WSL explain the launch mechanics; this wrapper adds status handling on top.

The wrapper is URL-only on purpose. If your tool also opens local HTML files, add a path branch that translates the Linux path with wslpath -w before passing it to Explorer. Keep the wrapper path free of spaces, because BROWSER consumers may split the value on spaces.

Test the Wrapper Without Masking Errors

Test 1: a valid URL under set -e

set -e
export BROWSER="$HOME/.local/bin/wsl-browser"
"$BROWSER" https://example.com
printf 'still running\n'
Enter fullscreen mode Exit fullscreen mode

The browser should open and still running should print. Without the wrapper, the same sequence with BROWSER=explorer.exe would stop at Explorer’s status 1. This is the exact regression the wrapper fixes.

Test 2: an invalid target

set +e
"$BROWSER" 'not a URL'
printf 'status=%d\n' "$?"
set -e
Enter fullscreen mode Exit fullscreen mode

Expected: status 2, and no browser launch. The wrapper rejects the target before calling Explorer.

Test 3: Explorer is not available

set +e
PATH=/usr/bin:/bin "$BROWSER" https://example.com
printf 'status=%d\n' "$?"
set -e
Enter fullscreen mode Exit fullscreen mode

Expected: status 127. This simulates a shell where explorer.exe is not on PATH, for example because interop is disabled or Windows PATH is not appended. A bare || true would have turned this into success.

Test 4: Explorer returns a different error

tmp=$(mktemp -d)
printf '#!/bin/sh\nexit 3\n' >"$tmp/explorer.exe"
chmod +x "$tmp/explorer.exe"
set +e
PATH="$tmp:$PATH" "$BROWSER" https://example.com
status=$?
set -e
printf 'status=%d\n' "$status"
rm -rf "$tmp"
Enter fullscreen mode Exit fullscreen mode

Expected: status 3. This proves the wrapper does not hide every failure. The MCP console report’s own test plan uses the same idea: a /bin/sh stub named explorer.exe that exits 1, plus a second stub with a different name that exits 1 and must still be reported as a failure.

Run the real-browser test manually. For CI, use stubs so no test opens a real browser.

Export BROWSER Safely and Verify It

Use an absolute path, because BROWSER values are split on spaces and some tools do not search an interactive shell’s PATH.

# ~/.bashrc or ~/.zshrc
export BROWSER="$HOME/.local/bin/wsl-browser"
Enter fullscreen mode Exit fullscreen mode

Reload the file and confirm the value:

source ~/.bashrc   # or source ~/.zshrc
printf '%s\n' "$BROWSER"
command -v "$BROWSER"
Enter fullscreen mode Exit fullscreen mode

Then test the wrapper directly:

"$BROWSER" https://example.com
Enter fullscreen mode Exit fullscreen mode

Where to put the export depends on how the CLI is started:

CLI context Where to set BROWSER
Interactive Bash ~/.bashrc
Zsh ~/.zshrc
Login shell ~/.profile
systemd user service Environment=BROWSER=... in the unit
systemd user session ~/.config/environment.d/*.conf

If the CLI still reports no browser:

  • Check printf '%s\n' "$BROWSER" in the same shell or service environment that launches the CLI.
  • Make sure the CLI actually honors BROWSER; some tools call xdg-open or a platform opener directly.
  • Restart the CLI after changing the variable. Many tools read BROWSER only at startup.
  • Remember that non-interactive shells may not read ~/.bashrc. Put the export in ~/.profile or the service environment.
  • If the CLI runs under sudo, check whether BROWSER is preserved; sudo often strips environment variables.

What the Wrapper Cannot Fix

The wrapper fixes a false negative, not every launch problem. Exit 1 is ambiguous: it means Explorer returned 1, not that a page rendered. If Explorer opens a folder instead of a browser, the wrapper still returns 0. A Microsoft WSL issue about explorer.exe and URLs with query strings reports that some URLs with query arguments opened a generic File Explorer window instead of the browser on older WSL builds. If that happens, test with a simple https://example.com first and consider cmd.exe /c start or wslview.

The wrapper also does not:

  • Make a tool honor BROWSER.
  • Check which browser opened or whether the page loaded.
  • Fix broken interop where Explorer returns 1 but nothing opens.

Those are separate problems. Use the wrapper only after you have confirmed that the browser actually opens and Explorer’s status is the only thing misleading the CLI. For an authentication flow, the reliable success signal is the redirect callback, not the browser launcher’s exit code.

Conclusion

explorer.exe exit code 1 under WSL is usually a hand-off status, not a browser failure. When BROWSER=explorer.exe opens the page but the CLI says it did not, the CLI is trusting a status that does not describe the browser. set -e and short settle windows make that false negative worse.

A status-aware wrapper fixes the mismatch without || true: it validates the URL, checks for explorer.exe, accepts 0 and 1, and propagates 2, 126, 127, and other errors. Test it under set -e, with an invalid target, with Explorer missing, and with a stub that returns an unexpected status. Export the wrapper by absolute path in the environment that actually launches the CLI, and remember that exit 1 still cannot prove a page rendered.

Top comments (0)