rust-lang/cargo · error · anyhow::Error
cannot add ` ` as a dependency to itself
Error message
cannot add `{}` as a dependency to itself What it means
Thrown by cargo add when the path source of the dependency being added resolves to the same directory as the Cargo.toml being modified. This would create a circular self-dependency where a crate depends on itself.
Solutions
- Specify the actual crate name from a registry: `cargo add <crate-name>`
- If adding a local path dependency, point to a different directory: `cargo add --path ../sibling-crate`
- If adding a workspace member, use the correct relative path to that member
Example fix
# before — running inside crate 'my-crate' cargo add --path . # error: cannot add `my-crate` as a dependency to itself # after — add from a registry or correct local path cargo add serde cargo add --path ../sibling-crate
Defensive patterns
Strategy: validation
Validate before calling
// Prevent self-dependency before calling cargo add
fn check_self_dep(manifest_dir: &Path, dep_path: &Path) -> Result<(), String> {
let manifest_canonical = manifest_dir.canonicalize().map_err(|e| e.to_string())?;
let dep_canonical = dep_path.canonicalize().map_err(|e| e.to_string())?;
if manifest_canonical == dep_canonical {
return Err("cannot add the current crate as a dependency to itself".into());
}
Ok(())
} Prevention
- Never use '.' or the current directory as a --path argument to cargo add
- Verify the target path points to a different crate before running cargo add --path
- Use cargo add <crate-name> for registry dependencies instead of --path for local crates
When it happens
Trigger: Running `cargo add .` or `cargo add --path .` (or any path that resolves to the current package's root directory) inside a crate.
Common situations: Running cargo add in the wrong directory. Accidentally using '.' as the crate path. Intending to add a sub-crate but pointing at the workspace root instead.
Related errors
- cannot specify a path
- cannot override workspace dependency with
- cannot specify a git URL
- dependency ( ) specified without providing a local path…
- no executable for ` ` found in PATH
AI-assisted analysis of rust-lang/cargo@98a09e7e7d (2026-08-11).
Data as JSON: /api/errors/c11d3823680bfab9.
Report an issue: GitHub.
Appendix: source
Thrown at src/ops/cargo_add/mod.rs:138
.get_table(&dep_table)
.map(TomlItem::as_table)
.map_or(true, |table_option| {
table_option.map_or(true, |table| {
table
.get_values()
.iter_mut()
.map(|(key, _)| {
// get_values key paths always have at least one key.
key.remove(0)
})
.is_sorted()
})
});
for dep in deps {
print_action_msg(&mut options.gctx.shell(), &dep, &dep_table)?;
if let Some(Source::Path(src)) = dep.source() {
if src.path == manifest.path.parent().unwrap_or_else(|| Path::new("")) {
anyhow::bail!(
"cannot add `{}` as a dependency to itself",
manifest.package_name()?
)
}
}
let available_features = dep
.available_features
.keys()
.map(|s| s.as_ref())
.collect::<BTreeSet<&str>>();
let mut unknown_features: Vec<&str> = Vec::new();
if let Some(req_feats) = dep.features.as_ref() {
let req_feats: BTreeSet<_> = req_feats.iter().map(|s| s.as_str()).collect();
unknown_features.extend(req_feats.difference(&available_features).copied());
}
if let Some(inherited_features) = dep.inherited_features.as_ref() {
let inherited_features: BTreeSet<_> =View on GitHub (pinned to 98a09e7e7d)