rust-lang/cargo · info · anyhow::Error

too many segments on URL

Error message

too many segments on URL

What it means

In `github_fast_path`, a third path segment exists after `user/repo` (e.g. `github.com/user/repo/extra`). Cargo expects exactly two segments for the GitHub API fast path, so it bails. Unlike 162–164 (which use `ok_or_else`/`?`), this one uses `anyhow::bail!`, but it is still caught by the caller at src/sources/git/utils.rs:1039 and swallowed into a `debug!` log + fallback fetch.

Source

Thrown at src/sources/git/utils.rs:1658

                debug!("can't use github fast path with `rev = \"{}\"`", rev);
                return Ok(FastPathRev::Indeterminate);
            }
        }
    };

    // This expects GitHub urls in the form `github.com/user/repo` and nothing
    // else
    let mut pieces = url
        .path_segments()
        .ok_or_else(|| anyhow!("no path segments on url"))?;
    let username = pieces
        .next()
        .ok_or_else(|| anyhow!("couldn't find username"))?;
    let repository = pieces
        .next()
        .ok_or_else(|| anyhow!("couldn't find repository name"))?;
    if pieces.next().is_some() {
        anyhow::bail!("too many segments on URL");
    }

    // Trim off the `.git` from the repository, if present, since that's
    // optional for GitHub and won't work when we try to use the API as well.
    let repository = repository.strip_suffix(".git").unwrap_or(repository);

    let url = format!(
        "https://api.github.com/repos/{}/{}/commits/{}",
        username, repository, github_branch_name,
    );
    debug!("attempting GitHub fast path for {}", url);
    let mut request =
        Request::get(url).header(http::header::ACCEPT, "application/vnd.github.3.sha");
    if let Some(local_object) = local_object {
        request = request.header(http::header::IF_NONE_MATCH, &format!("\"{local_object}\""));
    }
    let response = gctx
        .http_async()?

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Trim the URL to the bare repository: `https://github.com/<user>/<repo>`, and express branch/tag via the `branch`/`tag`/`rev` keys instead.
  2. If seen only in debug logs, it's harmless — Cargo's normal fetch handles it.

Example fix

# before
foo = { git = "https://github.com/org/repo/tree/v1" }

# after
foo = { git = "https://github.com/org/repo", tag = "v1" }
Defensive patterns

Strategy: try-catch

Try / catch

// Same pattern: catch Err, log at debug, proceed to a full fetch.
match github_fast_path(repo, url, reference, gctx) {
    Ok(r) => r,
    Err(e) => { debug!("failed to check github {:?}", e); /* full fetch */ }
}

Prevention

When it happens

Trigger: A git dependency URL with a trailing path component beyond `user/repo`, such as `https://github.com/org/repo/tree/main` or a URL with a trailing `/sub` path. Reached inside the GitHub fast path only.

Common situations: Copy-pasting a browser URL (with `/tree/<branch>` or `/blob/...`) into `Cargo.toml` as the `git` field; trailing-slash or extra path from a mirror config. Usually only visible in debug logs because the caller falls back to a full fetch.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/aeeecb98c8f1113e.json. Report an issue: GitHub.