{"id":"c11d3823680bfab9","repo":"rust-lang/cargo","slug":"cannot-add-as-a-dependency-to-itself","errorCode":null,"errorMessage":"cannot add `{}` as a dependency to itself","messagePattern":"cannot add `(.+?)` as a dependency to itself","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ops/cargo_add/mod.rs","lineNumber":136,"sourceCode":"        .get_table(&dep_table)\n        .map(TomlItem::as_table)\n        .map_or(true, |table_option| {\n            table_option.map_or(true, |table| {\n                table\n                    .get_values()\n                    .iter_mut()\n                    .map(|(key, _)| {\n                        // get_values key paths always have at least one key.\n                        key.remove(0)\n                    })\n                    .is_sorted()\n            })\n        });\n    for dep in deps {\n        print_action_msg(&mut options.gctx.shell(), &dep, &dep_table)?;\n        if let Some(Source::Path(src)) = dep.source() {\n            if src.path == manifest.path.parent().unwrap_or_else(|| Path::new(\"\")) {\n                anyhow::bail!(\n                    \"cannot add `{}` as a dependency to itself\",\n                    manifest.package_name()?\n                )\n            }\n        }\n\n        let available_features = dep\n            .available_features\n            .keys()\n            .map(|s| s.as_ref())\n            .collect::<BTreeSet<&str>>();\n        let mut unknown_features: Vec<&str> = Vec::new();\n        if let Some(req_feats) = dep.features.as_ref() {\n            let req_feats: BTreeSet<_> = req_feats.iter().map(|s| s.as_str()).collect();\n            unknown_features.extend(req_feats.difference(&available_features).copied());\n        }\n        if let Some(inherited_features) = dep.inherited_features.as_ref() {\n            let inherited_features: BTreeSet<_> =","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_add/mod.rs#L118-L154","documentation":"`cargo add` rejects adding a crate as a dependency of itself. After resolving a `--path` source, the code compares the resolved path's parent against the manifest's own directory; if they are equal the dependency resolves to the very package being edited, which would create a self-referential (cyclic) dependency declaration. This is a hard error because it cannot be serialized meaningfully into the manifest.","triggerScenarios":"Running `cargo add --path .` (or `--path` pointing at the package's own directory) from inside the crate you are editing, or `cargo add <self-name>` where path resolution lands on the current crate. The check at mod.rs:135 compares `src.path == manifest.path.parent()`.","commonSituations":"Workspace scaffolding scripts that run `cargo add <crate> --path .` against the current member, or mistaken automation that adds a crate to its own manifest. Also triggered when a crate name collides with a sibling directory that resolves back to itself.","solutions":["Do not add the crate to itself; if you meant to add it to a *different* member, run `cargo add` from (or with `-p`) that other member.","If you intended a dev/test cycle within the same crate, remove the `--path .` argument and restructure the code instead of declaring a self dependency.","Verify the directory the path points to with `cargo metadata` to confirm it is not the current manifest's parent."],"exampleFix":"# before (wrong, run inside my-crate)\ncargo add --path .\n\n# after (add to a different workspace member)\ncargo add --path . -p other-crate","handlingStrategy":"validation","validationCode":"// Confirm the path source is not the manifest's own directory before calling add().\nuse std::path::Path;\n\nfn is_self_add(manifest_path: &Path, dep_path: &Path) -> bool {\n    let manifest_dir = manifest_path.parent().unwrap_or_else(|| Path::new(\"\"));\n    let canonical = std::fs::canonicalize(dep_path).unwrap_or_else(|_| dep_path.to_path_buf());\n    canonical == manifest_dir\n}\n\n// if let Some(p) = &dep_op.path {\n//     assert!(!is_self_add(&manifest_path, Path::new(p)), \"refusing self-add\");\n// }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When scripting `cargo add --path` across workspace members, skip the member whose directory equals the path.","Pass `-p <target-member>` explicitly so the add targets the intended package.","Resolve paths via `cargo metadata` rather than relative `.` to avoid accidental self-references."],"tags":["cargo-add","self-dependency","path","cycle"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}