Hmbown/CodeWhale · error · anyhow::Error

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

Error message

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

What it means

run_gh_pr_diff shells out to `gh pr diff <number>` (optionally with `--repo owner/name`) and bails with gh's trimmed stderr when gh exits non-zero. The failure comes from the GitHub CLI itself -- authentication, network, repository, or PR-number problems -- not from codewhale. A missing gh binary is a separate, earlier error ("gh not found on PATH").

Source

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

        base: pick("baseRefName"),
        head: pick("headRefName"),
        url: pick("url"),
    })
}

fn run_gh_pr_diff(number: u32, repo: Option<&str>) -> Result<String> {
    let mut cmd = crate::dependencies::Gh::command()
        .ok_or_else(|| anyhow::anyhow!("gh not found on PATH"))?;
    cmd.arg("pr").arg("diff").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 diff`: {e}"))?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
        bail!("gh pr diff #{number} failed: {stderr}");
    }
    Ok(String::from_utf8_lossy(&output.stdout).to_string())
}

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}");
    }

View on GitHub (pinned to 0c42157ee5)

Solutions

  1. Run `gh auth status` (then `gh auth login` if needed) and retry
  2. Verify the PR resolves: `gh pr view <number> [--repo owner/name]`
  3. Read the stderr embedded in the message -- gh states the exact reason (not found, unauthorized, rate limit)
  4. If behind a proxy, export HTTPS_PROXY/HTTP_PROXY and confirm `gh api user` works
  5. Confirm gh is on PATH and current (`gh --version`)

Example fix

# before
codewhale review --pr 4242 --repo owner/private-repo
# gh pr diff #4242 failed: Could not resolve to a PullRequest

# after
gh auth status && gh pr view 4242 --repo owner/private-repo   # pre-flight passes
codewhale review --pr 4242 --repo owner/private-repo
Defensive patterns

Strategy: validation

Validate before calling

gh auth status >/dev/null 2>&1 || { echo 'gh not authenticated'; exit 1; }
gh pr view "$PR" ${REPO:+--repo "$REPO"} --json number >/dev/null 2>&1 || { echo "PR $PR not visible"; exit 1; }

Try / catch

match run_gh_pr_diff(number, repo.as_deref()) {
    Ok(diff) => { /* use diff */ }
    Err(e) => eprintln!("gh pr diff failed: {e:#}"), // gh's stderr is embedded; act on it, do not blind-retry
}

Prevention

When it happens

Trigger: Invoking the PR review flow that calls run_gh_pr_diff when `gh auth` is expired or absent; `--repo` pointing at a repo that has no PR with that number; a typo'd or cross-repo PR number; gh unable to reach api.github.com (offline, DNS, proxy).

Common situations: Fresh machine or CI runner where gh is installed but never authenticated; expired GH_TOKEN/GITHUB_TOKEN; --repo slug typo; corporate proxy blocking HTTPS; very old gh versions.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@0c42157ee5 (2026-08-20). Data as JSON: /api/errors/8313013144e7d0c9. Report an issue: GitHub.