cli/cli · error

error opening Visual Studio Code: %w

Error message

error opening Visual Studio Code: %w

What it means

`gh codespace code` failed at the final step: after building the vscode.dev (or insider) browse URL, the OS-default browser could not be opened via browser.Browse. Everything upstream (codespace resolution) succeeded; only launching the browser failed.

Source

Thrown at pkg/cmd/codespace/code.go:58

	}

	browseURL := vscodeProtocolURL(codespace.Name, useInsiders)
	if useWeb {
		browseURL = codespace.WebURL
		if useInsiders {
			u, err := url.Parse(browseURL)
			if err != nil {
				return err
			}
			q := u.Query()
			q.Set("vscodeChannel", "insiders")
			u.RawQuery = q.Encode()
			browseURL = u.String()
		}
	}

	if err := a.browser.Browse(browseURL); err != nil {
		return fmt.Errorf("error opening Visual Studio Code: %w", err)
	}

	return nil
}

func vscodeProtocolURL(codespaceName string, useInsiders bool) string {
	application := "vscode"
	if useInsiders {
		application = "vscode-insiders"
	}
	return fmt.Sprintf("%s://github.codespaces/connect?name=%s&windowId=_blank", application, url.QueryEscape(codespaceName))
}

View on GitHub (pinned to 0eeec0b92e)

Solutions

  1. Set BROWSER to a working executable, e.g. `BROWSER=firefox gh codespace code -c NAME`, or use `--browser` if a specific browser flag exists for your version.
  2. On headless machines use the web flow elsewhere or forward X; alternatively run `gh codespace ssh` instead.
  3. Check xdg-settings/getters: `xdg-settings get default-web-browser` on Linux.
  4. Print the URL instead of opening: open the vscode.dev URL manually if the command prints it.

Example fix

# before (headless box)
gh codespace code -c monalisa-dotfiles-abc123
# error opening Visual Studio Code: exec: "xdg-open": executable file not found in $PATH

# after
BROWSER=none gh codespace code -c monalisa-dotfiles-abc123   # or copy the URL manually
gh codespace ssh -c monalisa-dotfiles-abc123                  # terminal alternative
Defensive patterns

Strategy: fallback

Validate before calling

# Detect headless environments before invoking the browser flow
if [ -z "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ] && [ -z "$BROWSER" ]; then
  echo "no GUI browser available" >&2; exit 1
fi

Try / catch

gh codespace code -c "$NAME" || {
  # fallback: open the URL manually or use the terminal path
  gh codespace ssh -c "$NAME"
}

Prevention

When it happens

Trigger: No default browser configured on the machine (common on headless Linux/servers/containers), BROWSER env var pointing to a broken executable, or the open/exec of the browser binary returning an error.

Common situations: SSH into a remote box or CI container without a display server or xdg-open; BROWSER set to a nonexistent command; Windows/WSL browser registration issues.

Related errors


AI-assisted analysis of cli/cli@0eeec0b92e (2026-08-15). Data as JSON: /api/errors/01d5312e02ce7dce. Report an issue: GitHub.