nikivdev/code · error

failed to open browser

Error message

failed to open browser

What it means

`open_in_browser` (src/commit.rs:8927) on macOS runs `open <url>`; if the spawned process exits non-zero this bail reports the URL could not be opened in the system browser.

Source

Thrown at src/commit.rs:8927

            .ok_or_else(|| anyhow::anyhow!("failed to parse PR number from URL {}", url))?;
        return Ok((number, url));
    }

    if let Some(found) = gh_find_open_pr_by_head(repo_root, repo, head)? {
        return Ok(found);
    }

    bail!(
        "failed to determine PR URL after creation (gh output had no URL and PR lookup by head returned empty)"
    );
}

fn open_in_browser(url: &str) -> Result<()> {
    #[cfg(target_os = "macos")]
    {
        let status = Command::new("open").arg(url).status()?;
        if !status.success() {
            bail!("failed to open browser");
        }
        return Ok(());
    }

    #[cfg(not(target_os = "macos"))]
    {
        let status = Command::new("xdg-open").arg(url).status()?;
        if !status.success() {
            bail!("failed to open browser");
        }
        Ok(())
    }
}

fn commit_message_title_body(message: &str) -> (String, String) {
    let mut lines = message.lines();
    let title = lines.next().unwrap_or("no title").trim().to_string();
    let rest = lines.collect::<Vec<_>>().join("\n").trim().to_string();

View on GitHub (pinned to a747e741ae)

Solutions

  1. Set a valid default browser in macOS System Settings > Desktop & Dock > Default web browser.
  2. Test `open <url>` in a terminal to confirm the environment can open URLs.
  3. Run in a GUI session instead of headless/SSH/CI, or use the PR URL already printed.
  4. Verify `/usr/bin/open` works (`open -a 'Safari' <url>`).
Defensive patterns

Strategy: fallback

Validate before calling

let check = Command::new("open").arg("-g").arg("https://example.com").status();
if check.map(|s| !s.success()).unwrap_or(true) {
    eprintln!("`open` cannot launch a browser; print the URL instead");
}

Type guard

fn can_open_browser() -> bool {
    Command::new("open").arg("-g").arg("https://example.com")
        .output().map(|o| o.status.success()).unwrap_or(false)
}

Try / catch

if let Err(e) = open_in_browser(&url) {
    eprintln!("could not open browser ({e}); open this URL manually:\n{url}");
    // do not fail the whole PR-creation flow because browser launch failed
}

Prevention

When it happens

Trigger: `Command::new("open").arg(url).status()` spawns successfully but `open` returns non-zero: no default browser set, Launch Services failure, dangling default handler after a browser uninstall, or headless/sandboxed macOS environment.

Common situations: CI runner or SSH session on a Mac with no GUI session; default-browser registration pointing at an uninstalled app; container/sandbox where LaunchServices is unavailable.

Related errors


AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/754a00a04171ea72. Report an issue: GitHub.