{"id":"ce48ddce55dc322a","repo":"rust-lang/cargo","slug":"source-should-be-resolved-before-here","errorCode":null,"errorMessage":"source should be resolved before here","messagePattern":"source should be resolved before here","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ops/cargo_add/mod.rs","lineNumber":1013,"sourceCode":"            unreachable!(\"path or git dependency expected, found workspace dependency\");\n        }\n        MaybeWorkspace::Other(query) => {\n            let possibilities =\n                crate::util::block_on(registry.query_vec(&query, QueryKind::Normalized))?;\n\n            let possibilities: Vec<_> = possibilities\n                .into_iter()\n                .filter_map(|s| match s {\n                    IndexSummary::Candidate(s) => Some(s),\n                    _ => None,\n                })\n                .collect();\n\n            match possibilities.len() {\n                0 => {\n                    let source = dependency\n                        .source()\n                        .expect(\"source should be resolved before here\");\n                    anyhow::bail!(\"the crate `{dependency}` could not be found at `{source}`\")\n                }\n                1 => {\n                    let mut dep = Dependency::from(&possibilities[0]);\n                    if let Some(reg_name) = dependency.registry.as_deref() {\n                        dep = dep.set_registry(reg_name);\n                    }\n                    if let Some(Source::Path(PathSource { base, .. })) = dependency.source() {\n                        if let Some(Source::Path(dep_src)) = &mut dep.source {\n                            dep_src.base = base.clone();\n                        }\n                    }\n                    Ok(dep)\n                }\n                _ => {\n                    let source = dependency\n                        .source()\n                        .expect(\"source should be resolved before here\");","sourceCodeStart":995,"sourceCodeEnd":1031,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_add/mod.rs#L995-L1031","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the crate name is spelled correctly and exists on the target registry.","Check registry configuration in .cargo/config.toml — ensure the registry URL is correct and reachable.","Run cargo search <crate> to confirm the crate exists on the registry.","Update cargo if you suspect an internal pipeline ordering bug."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Verify crate name and registry before cargo add\nfn check_crate_exists(name: &str, registry: &str) -> bool {\n    // Use cargo search or query the registry index\n    std::process::Command::new(\"cargo\")\n        .args([\"search\", name])\n        .output()\n        .map(|o| o.status.success())\n        .unwrap_or(false)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Double-check crate name spelling before cargo add.","Verify registry configuration in .cargo/config.toml.","Run cargo search <name> to confirm the crate exists on the target registry."],"tags":["rust","cargo","panic","invariant","cargo-add","registry","source-resolution"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}