nikivdev/code · error · anyhow::Error

failed to create private repo {}

Error message

failed to create private repo {}

What it means

Raised while forking/provisioning a private mirror (src/deps.rs:1430). There are two failure layers: a `.context` on the `gh repo create ... --private` subprocess spawn, and this bail when `gh` runs but exits non-zero. Either way, the private GitHub repo was not created, so the subsequent `set_origin_remote` never runs.

Source

Thrown at src/deps.rs:1430

    if let Some(origin_remote) = origin_remote {
        if origin_remote.contains(&format!("github.com:{}/", gh_user))
            || origin_remote.contains(&format!("github.com/{}/", gh_user))
        {
            return Ok(());
        }
    }

    let private_repo = format!("{}/{}", gh_user, repo_ref.repo);
    let private_url = format!("git@github.com:{}.git", private_repo);

    if !gh_repo_exists(&private_repo)? {
        println!("Creating private repo: {}", private_repo);
        let status = Command::new("gh")
            .args(["repo", "create", &private_repo, "--private"])
            .status()
            .context("failed to create private repo")?;
        if !status.success() {
            bail!("failed to create private repo {}", private_repo);
        }
    }

    set_origin_remote(repo_dir, &private_url)?;
    let upstream_remote = git_remote_get(repo_dir, "upstream")?;
    if upstream_remote.is_none() {
        configure_upstream(repo_dir, origin_url)?;
    } else if upstream_remote.as_deref() != Some(origin_url) {
        println!(
            "⚠ upstream already set to {} (expected {})",
            upstream_remote.unwrap_or_default(),
            origin_url
        );
    }
    println!("✓ origin -> {}", private_repo);

    Ok(())
}

View on GitHub (pinned to a747e741ae)

Solutions

  1. Run `gh auth status` and re-authenticate (`gh auth login`) ensuring the repo scope
  2. Run the exact `gh repo create <name> --private` command manually to see the API error
  3. Install/upgrade the GitHub CLI if missing or outdated
  4. Choose a different repo name if the target already exists, or ask an org admin for creation rights

Example fix

// before
gh auth status   # not logged in
error: failed to create private repo acme/foo-mirror
// after
gh auth login && gh repo create acme/foo-mirror --private
# then rerun the f command
Defensive patterns

Strategy: retry

Validate before calling

let gh = std::process::Command::new("gh").args(["auth", "status"]).status();
if !gh.map(|s| s.success()).unwrap_or(false) {
    anyhow::bail!("gh CLI missing or unauthenticated; run gh auth login first");
}

Type guard

fn gh_ready() -> bool {
    std::process::Command::new("gh").args(["auth", "status"])
        .status().map(|s| s.success()).unwrap_or(false)
}

Try / catch

match create_private_mirror(repo) {
    Err(e) if e.to_string().contains("failed to create private repo") => {
        eprintln!("gh auth expired or repo exists; fix and retry once");
        gh_auth_login()?;
        create_private_mirror(repo)
    }
    other => other,
}

Prevention

When it happens

Trigger: Calling the private-mirror creation flow when the `gh` CLI is not installed (spawn context error) or `gh repo create` fails with a non-zero status: unauthenticated session, missing `repo` scope, name already taken, or org policy blocking private repo creation.

Common situations: `gh auth login` never run or token expired, token lacking scopes, creating under an org that restricts repo creation, GitHub API rate limits, or `gh` not installed at all.

Related errors


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