Hmbown/CodeWhale · error · anyhow::Error

gh pr checkout # failed

Error message

gh pr checkout #{number} failed: {stderr}

What it means

The wrapper running `gh pr checkout <number>` found the command exited non-zero. The gh stderr is trimmed and embedded in the error so the underlying GitHub failure (auth, missing PR, dirty worktree) is visible. Spawn failures surface separately as "Failed to run `gh pr checkout`".

Solutions

  1. Read the embedded stderr and fix the gh-reported cause (wrong PR number, dirty worktree, auth)
  2. Run `gh auth status` / `gh auth login` to confirm authentication and repo access
  3. Commit, stash, or clean local changes before checkout (`git stash`)
  4. Verify the PR exists and is visible: `gh pr view <number>`

Example fix

// before
codewhale pr 999999   # gh: no pull requests found
// after
codewhale pr 123      # valid, accessible PR number
Defensive patterns

Strategy: try-catch

Validate before calling

gh pr view "$PR" --json number >/dev/null 2>&1 || { echo "PR $PR not accessible"; exit 1; }
git diff --quiet || { echo 'worktree dirty; commit or stash first'; exit 1; }

Try / catch

// run the command, capture stderr; the codewhale error embeds gh's stderr — surface it verbatim
const { status, stderr } = spawnSync('gh', ['pr', 'checkout', String(n)]);
if (status !== 0) throw new Error(`gh pr checkout #${n} failed: ${stderr.trim()}`);

Prevention

When it happens

Trigger: Calling the PR checkout path when `gh pr checkout` fails: PR number does not exist or is inaccessible, `gh` is not authenticated, or the current git worktree has local changes blocking checkout.

Common situations: Typo in the PR number; private-repo PR without `gh auth login`; checking out while the working directory has uncommitted changes that would be overwritten; detached fork setups gh cannot resolve.

Understand the failure class

Background: "git command failed": what it means when a tool shells out to git and git exits non-zero — this error's family across 21 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/70ddb8cfdb104528. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/lib.rs:9291

}

fn run_gh_pr_diff(number: u32, repo: Option<&str>, view: &GhPullRequest) -> Result<String> {
    crate::tools::review_pr::fetch_diff(number, repo, &std::env::current_dir()?, view)
}

fn run_gh_pr_checkout(number: u32, repo: Option<&str>) -> Result<()> {
    let mut cmd = crate::dependencies::Gh::command()
        .ok_or_else(|| anyhow::anyhow!("gh not found on PATH"))?;
    cmd.arg("pr").arg("checkout").arg(number.to_string());
    if let Some(r) = repo {
        cmd.arg("--repo").arg(r);
    }
    let output = cmd
        .output()
        .map_err(|e| anyhow::anyhow!("Failed to run `gh pr checkout`: {e}"))?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
        bail!("gh pr checkout #{number} failed: {stderr}");
    }
    Ok(())
}

/// Resolve the `owner/name` repository `gh` believes the current workspace
/// belongs to. `gh api` needs an explicit repository path, unlike `gh pr`
/// which infers it from the working directory.
fn run_gh_repo_name() -> Result<String> {
    let mut cmd = crate::dependencies::Gh::command()
        .ok_or_else(|| anyhow::anyhow!("gh not found on PATH"))?;
    cmd.arg("repo")
        .arg("view")
        .arg("--json")
        .arg("nameWithOwner")
        .arg("--jq")
        .arg(".nameWithOwner");
    let output = cmd
        .output()

View on GitHub (pinned to 73e0f67d83)