{"record":{"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":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_add/mod.rs","lineNumber":138,"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":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/rust-lang/cargo/blob/98a09e7e7d62850f14e5b6132101fc1edd19a16f/src/ops/cargo_add/mod.rs#L120-L156","documentation":"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.","triggerScenarios":"Running `cargo add .` or `cargo add --path .` (or any path that resolves to the current package's root directory) inside a crate.","commonSituations":"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.","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"],"exampleFix":"# before — running inside crate 'my-crate'\ncargo add --path .\n# error: cannot add `my-crate` as a dependency to itself\n\n# after — add from a registry or correct local path\ncargo add serde\ncargo add --path ../sibling-crate","handlingStrategy":"validation","validationCode":"// Prevent self-dependency before calling cargo add\nfn check_self_dep(manifest_dir: &Path, dep_path: &Path) -> Result<(), String> {\n    let manifest_canonical = manifest_dir.canonicalize().map_err(|e| e.to_string())?;\n    let dep_canonical = dep_path.canonicalize().map_err(|e| e.to_string())?;\n    if manifest_canonical == dep_canonical {\n        return Err(\"cannot add the current crate as a dependency to itself\".into());\n    }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["cargo-add","dependency","circular","path"],"backgroundTag":null,"analyzedSha":"98a09e7e7d62850f14e5b6132101fc1edd19a16f","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}