rust-lang/cargo · error

source should be resolved before here

Error message

source should be resolved before here

What it means

This panic is in query_dependency() when the registry query returns zero matching candidates. It calls dependency.source().expect("source should be resolved before here") to produce an error message naming the source. The invariant is that by this point in the cargo add flow, the dependency's source has already been resolved (it went through get_latest_dependency or an explicit source assignment).

Source

Thrown at src/ops/cargo_add/mod.rs:1013

            unreachable!("path or git dependency expected, found workspace dependency");
        }
        MaybeWorkspace::Other(query) => {
            let possibilities =
                crate::util::block_on(registry.query_vec(&query, QueryKind::Normalized))?;

            let possibilities: Vec<_> = possibilities
                .into_iter()
                .filter_map(|s| match s {
                    IndexSummary::Candidate(s) => Some(s),
                    _ => None,
                })
                .collect();

            match possibilities.len() {
                0 => {
                    let source = dependency
                        .source()
                        .expect("source should be resolved before here");
                    anyhow::bail!("the crate `{dependency}` could not be found at `{source}`")
                }
                1 => {
                    let mut dep = Dependency::from(&possibilities[0]);
                    if let Some(reg_name) = dependency.registry.as_deref() {
                        dep = dep.set_registry(reg_name);
                    }
                    if let Some(Source::Path(PathSource { base, .. })) = dependency.source() {
                        if let Some(Source::Path(dep_src)) = &mut dep.source {
                            dep_src.base = base.clone();
                        }
                    }
                    Ok(dep)
                }
                _ => {
                    let source = dependency
                        .source()
                        .expect("source should be resolved before here");

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Verify the crate name is spelled correctly and exists on the target registry.
  2. Check registry configuration in .cargo/config.toml — ensure the registry URL is correct and reachable.
  3. Run cargo search <crate> to confirm the crate exists on the registry.
  4. Update cargo if you suspect an internal pipeline ordering bug.
Defensive patterns

Strategy: validation

Validate before calling

// Verify crate name and registry before cargo add
fn check_crate_exists(name: &str, registry: &str) -> bool {
    // Use cargo search or query the registry index
    std::process::Command::new("cargo")
        .args(["search", name])
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

Prevention

When it happens

Trigger: Running cargo add where the dependency name is not found in the registry and the source was not yet assigned — this could happen if query_dependency is called before source resolution completes, or if the dependency object passed in has no source due to an upstream logic error.

Common situations: cargo add with a typo'd crate name against a registry that returns empty results; using a private registry that is not properly configured; a cargo internal bug where query_dependency is called out of order in the add pipeline.

Related errors


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