{"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":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_new.rs","lineNumber":517,"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":499,"sourceCodeEnd":535,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_new.rs#L499-L535","documentation":"The `init` command is for directories that do NOT yet have a Cargo.toml. If path.join(\"Cargo.toml\").exists() at cargo_new.rs:516 is true, Cargo refuses (running init on an existing package would clobber/dupe the manifest). It points the user to `cargo new` to create a brand-new subdirectory package instead.","triggerScenarios":"`cargo init` inside a directory that already has a Cargo.toml (e.g. a workspace member, an old crate, a vendored dep). The check at cargo_new.rs:516 returns true.","commonSituations":"Wrong terminal tab (already inside a crate); running init in a monorepo root that has a workspace Cargo.toml; trying to 're-scaffold' an existing crate after deleting src/; IDE wizards that invoke init unconditionally.","solutions":["If you want a new crate, run `cargo new <subdir>` from inside the existing package","If you genuinely want to wipe and re-init, delete Cargo.toml first (back it up!)","Verify you are in the intended directory with `pwd` and `ls Cargo.toml`"],"exampleFix":"# before (Cargo.toml already present)\ncargo init\n# after\ncargo new my-new-crate   # creates my-new-crate/ with its own manifest","handlingStrategy":"validation","validationCode":"use std::path::Path;\nfn ensure_no_manifest(dir: &Path) -> Result<(), String> {\n    if dir.join(\"Cargo.toml\").exists() {\n        Err(format!(\"{dir:?} already has a Cargo.toml; use cargo new\"))\n    } else { Ok(()) }\n}\n\nensure_no_manifest(path)?;","typeGuard":"import { existsSync } from 'fs';\nimport { join } from 'path';\nfunction isFreshForInit(dir: string): boolean { return !existsSync(join(dir, 'Cargo.toml')); }","tryCatchPattern":null,"preventionTips":["Run `ls Cargo.toml` before `cargo init`","Use `cargo new <subdir>` to add crates inside an existing package","Teach IDE wizards to detect an existing manifest and skip init"],"tags":["cargo-init","manifest","filesystem","cli-usage"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}