{"record":{"id":"651201eee37e6b04","repo":"rust-lang/cargo","slug":"cargo-init-cannot-be-run-on-existing-cargo-packa","errorCode":null,"errorMessage":"`cargo init` cannot be run on existing Cargo packages\nhelp: use `cargo new` to create a package in a new subdirectory","messagePattern":"`cargo init` cannot be run on existing Cargo packages\nhelp: use `cargo new` to create a package in a new subdirectory","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ops/cargo_new.rs","lineNumber":466,"sourceCode":"    let path = &opts.path;\n\n    if let Some(home) = home_dir() {\n        if path == &home {\n            anyhow::bail!(\n                \"cannot create package in the home directory\\n\\n\\\n                 help: use `cargo init <path>` to create a package in a different directory\"\n            )\n        }\n    }\n    let name = get_name(path, opts)?;\n    let mut src_paths_types = vec![];\n    detect_source_paths_and_types(path, name, &mut src_paths_types)?;\n    let kind = calculate_new_project_kind(opts.kind, opts.auto_detect_kind, &src_paths_types);\n    gctx.shell()\n        .status(\"Creating\", format!(\"{} package\", opts.kind))?;\n\n    if path.join(\"Cargo.toml\").exists() {\n        anyhow::bail!(\n            \"`cargo init` cannot be run on existing Cargo packages\\n\\\n             help: use `cargo new` to create a package in a new subdirectory\"\n        )\n    }\n    check_path(path, &mut gctx.shell())?;\n\n    let has_bin = kind.is_bin();\n\n    if src_paths_types.is_empty() {\n        src_paths_types.push(plan_new_source_file(has_bin));\n    } else if src_paths_types.len() == 1 && !src_paths_types.iter().any(|x| x.bin == has_bin) {\n        // we've found the only file and it's not the type user wants. Change the type and warn\n        let file_type = if src_paths_types[0].bin {\n            NewProjectKind::Bin\n        } else {\n            NewProjectKind::Lib\n        };\n        gctx.shell().warn(format!(","sourceCodeStart":448,"sourceCodeEnd":484,"githubUrl":"https://github.com/rust-lang/cargo/blob/98a09e7e7d62850f14e5b6132101fc1edd19a16f/src/ops/cargo_new.rs#L448-L484","documentation":"`cargo init` is meant for directories that are not yet cargo packages. If `Cargo.toml` already exists in the target directory, init aborts — re-initializing could overwrite or duplicate the existing manifest. The message points to `cargo new` (a fresh subdirectory) as the alternative.","triggerScenarios":"Running `cargo init` inside a directory that already contains a `Cargo.toml`. Re-running `cargo init` after it succeeded once. Pointing `cargo init` at an existing crate.","commonSituations":"Forgetting a folder was already initialized. CI setup scripts that re-init on every run. Cloning a repo and then running `cargo init` out of habit.","solutions":["Do nothing — the package already exists; just `cargo build`/`cargo run`.","If you genuinely need a separate new package, use `cargo new <subdir>` in a non-existent subdirectory.","If the existing `Cargo.toml` is stale/empty and you want a fresh one, manually back it up and remove it before `cargo init`."],"exampleFix":"# before\n$ ls\nCargo.toml\n$ cargo init   # error: cannot be run on existing Cargo packages\n\n# after — it is already a package, just use it:\n$ cargo build\n# or create a new package elsewhere:\n$ cargo new ../other-project","handlingStrategy":"validation","validationCode":"use std::path::Path;\nfn has_manifest(dir: &Path) -> bool { dir.join(\"Cargo.toml\").exists() }\n// if has_manifest(&dir) { skip `cargo init`; it is already a package }","typeGuard":"fn needs_init(dir: &Path) -> bool { !has_manifest(dir) }","tryCatchPattern":null,"preventionTips":["Check for an existing `Cargo.toml` before `cargo init`.","Make CI init steps idempotent: skip if already initialized.","Use `cargo new <subdir>` when you genuinely need a fresh package alongside an existing one."],"tags":["cargo-init","manifest","cli-usage","filesystem"],"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"}