rust-lang/cargo · error

too many segments on URL

Error message

too many segments on URL

What it means

GitHub fast path requires exactly `user/repo` (two segments). A third `pieces.next()` returned `Some`, so the URL has extra path components beyond the repository (e.g. `https://github.com/user/repo/extra` or `https://github.com/user/repo/tree/main`). GitHub API URL construction would be ambiguous, so Cargo bails.

Solutions

  1. Trim the URL to the repo root: `https://github.com/<user>/<repo>` (optionally with `.git`).
  2. Move subdir/branch info into the `path` / `branch` / `rev` keys of the dependency instead of the URL.
  3. Validate that the git URL path has exactly two segments before commit.

Example fix

# before
[dependencies]
x = { git = "https://github.com/user/repo/tree/main/sub" }

# after
[dependencies]
x = { git = "https://github.com/user/repo", branch = "main", path = "sub" }
Defensive patterns

Strategy: validation

Validate before calling

fn require_exactly_two_segments(u: &url::Url) -> Result<(&str, &str), anyhow::Error> {
    let mut s = u.path_segments().ok_or_else(|| anyhow!("no path"))?;
    let user = s.next().ok_or_else(|| anyhow!("no user"))?;
    let repo = s.next().ok_or_else(|| anyhow!("no repo"))?;
    if s.next().is_some() { anyhow::bail!("too many segments"); }
    Ok((user, repo))
}

Type guard

fn url_has_exactly_two_segments(u: &url::Url) -> bool {
    u.path_segments().map_or(false, |mut s| s.next().is_some() && s.next().is_some() && s.next().is_none())
}

Try / catch

if pieces.next().is_some() { return Ok(FastPathRev::Indeterminate); } // skip fast path gracefully

Prevention

When it happens

Trigger: A git dependency URL with more than two path segments reaches the fast path. Commonly a URL to a subdirectory, branch, or file on GitHub pasted into `Cargo.toml` instead of the repo root.

Common situations: Pasting a deep GitHub URL (to a subfolder, tag page, or `tree/<branch>` view) as the `git` value; trailing `/something` left after editing.

Related errors


AI-assisted analysis of rust-lang/cargo@495c385d08 (2026-08-11). Data as JSON: /api/errors/aeeecb98c8f1113e. Report an issue: GitHub.

Appendix: source

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

                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 495c385d08)