rust-lang/cargo · error

couldn't find username

Error message

couldn't find username

What it means

In the GitHub fast path, after `url.path_segments()` returns an iterator, Cargo expects the first segment to be the GitHub username/org. If `pieces.next()` returns `None` (the URL path is empty, e.g. `https://github.com/`), this error fires. It indicates a GitHub URL truncated before the `user` segment.

Solutions

  1. Set the git dependency URL to the full `https://github.com/<user>/<repo>` form.
  2. Check any env-var or CI template that generates the URL for truncation.
  3. Lint dependency URLs in a pre-commit hook.

Example fix

# before
[dependencies]
x = { git = "https://github.com/" }

# after
[dependencies]
x = { git = "https://github.com/rust-lang/cargo" }
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

fn url_has_user_segment(u: &url::Url) -> bool {
    u.path_segments().and_then(|mut s| s.next()).filter(|s| !s.is_empty()).is_some()
}

Try / catch

let username = match pieces.next() { Some(u) if !u.is_empty() => u, _ => return Ok(FastPathRev::Indeterminate) };

Prevention

When it happens

Trigger: A git dependency URL like `https://github.com/` or `https://github.com` (no user/repo segments) reaching the fast path. The URL parsed successfully and had path segments (so 142 did not fire) but the first segment is missing.

Common situations: Truncated git URL in `Cargo.toml` from a bad copy-paste; environment variable substitution (`$REPO_URL`) producing an empty user; templated config that dropped the path.

Related errors


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

Appendix: source

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

                    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,
    );
    debug!("attempting GitHub fast path for {}", url);
    let mut request =
        Request::get(url).header(http::header::ACCEPT, "application/vnd.github.3.sha");

View on GitHub (pinned to 495c385d08)