rust-lang/cargo · error · anyhow::Error
the crate `{dependency}` could not be found at `{source}`
Error message
the crate `{dependency}` could not be found at `{source}` What it means
During `select_package`, cargo queries the resolved source (path or git) for packages matching the dependency. If the query returns zero candidates, it bails at mod.rs:1014 — the named crate does not exist at the given path or git source. This is distinct from a registry 404: it means the *local/git* source had no matching package.
Source
Thrown at src/ops/cargo_add/mod.rs:1014
}
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");
anyhow::bail!(View on GitHub (pinned to 0e07a15537)
Solutions
- Verify the package name with `cargo metadata --manifest-path <path>/Cargo.toml` (the `name` field) and use that exact name.
- For git repos with multiple packages, let cargo infer or disambiguate: `cargo add --git <url> <exact-package-name>`.
- Correct the `--path` to point at the directory containing the target crate's `Cargo.toml`.
Example fix
# before cargo add --path ./libs wrong-name # after cargo add --path ./libs actual-package-name
Defensive patterns
Strategy: validation
Validate before calling
// Verify the package exists at the path/git source before calling add().
use std::path::Path;
fn package_exists_at(path: &Path, expected_name: &str) -> bool {
let manifest = path.join("Cargo.toml");
let Ok(text) = std::fs::read_to_string(&manifest) else { return false };
toml_edit::ImDocument::parse(&text)
.ok()
.and_then(|d| d.as_table().get("package")?.as_table()?.get("name")?.as_str().map(|s| s == expected_name))
.unwrap_or(false)
} Prevention
- Resolve the exact package name from `cargo metadata` of the source before adding.
- For git sources, clone-and-inspect or use `cargo add --git <url>` (auto-disambiguation) first.
- Validate paths are crate roots (contain a `Cargo.toml` with a matching `[package] name`).
When it happens
Trigger: `cargo add --path ./wrong-dir mycrate` where that directory has no crate named `mycrate`, or `cargo add --git <url> mycrate` where the repo contains no matching package. The `select_package` match count is 0.
Common situations: Typo in crate name vs. package name, pointing `--path` at a subdirectory that is not a crate root, or a git repo where the package name differs from the crate binary name.
Related errors
- unexpectedly found multiple copies of crate `{dependency}` a
- `{feature}` is unsupported when inferring the crate name, us
- cannot add `{}` as a dependency to itself
- cannot specify a git URL (`{url}`) with a version (`{v}`).
- cannot specify a path (`{raw_path}`) with a version (`{v}`).
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/c3c5a2b2fa2bdf64.json.
Report an issue: GitHub.