rust-lang/cargo · error

no path segments on url

Error message

no path segments on url

What it means

Inside the GitHub fast-path optimization (`github_fast_path`), Cargo parses the remote URL's path segments to derive `user/repo` for the GitHub API call. `url.path_segments()` returns `None` when the URL has no path component (e.g. a `cannot-be-a-base` URL or a malformed registry/git URL). This is a sanity check that the URL is a well-formed GitHub HTTP(S) URL.

Solutions

  1. Correct the git dependency URL in `Cargo.toml` to a full `https://github.com/<user>/<repo>` form.
  2. If the host is not GitHub, ensure the URL does not match GitHub fast-path preconditions, or switch to a non-fast-path reference like a full commit `rev`.
  3. Validate the URL with `url::Url::parse` and `path_segments()` in a pre-build script.

Example fix

# before
[dependencies]
x = { git = "github.com/user/repo" }   # missing scheme/host

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

Strategy: validation

Validate before calling

use url::Url;
fn validate_git_url(raw: &str) -> Result<Url, anyhow::Error> {
    let u = Url::parse(raw)?;
    if u.path_segments().is_none() {
        anyhow::bail!("git url has no path segments: {raw}");
    }
    Ok(u)
}

Type guard

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

Try / catch

let pieces = match url.path_segments() {
    Some(p) => p,
    None => return Ok(FastPathRev::Indeterminate), // skip fast path instead of erroring
};

Prevention

When it happens

Trigger: A git dependency whose URL is non-hierarchical (scheme without a `/`-delimited path) or otherwise yields `None` from `Url::path_segments`. Only reached after earlier guards confirm the reference is resolvable via the fast path, so it implies the URL itself is structurally odd.

Common situations: Typo in a git dependency URL (missing host/path); using a `file://` or odd scheme URL that the fast path mistakenly enters; custom registry misconfiguration pointing at a non-GitHub URL that still got routed into the GitHub fast path.

Related errors


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

Appendix: source

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

                // either. (This ensures that we always attempt to fetch the
                // commit directly even if we can't reach the GitHub API.)
                if let Some(oid) = rev_to_oid(rev) {
                    debug!("github fast path is already a full commit hash {rev}");
                    return Ok(FastPathRev::NeedsFetch(oid));
                }
                rev
            } else {
                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,
    );

View on GitHub (pinned to 495c385d08)