{"id":"08c046285e8193d2","repo":"rust-lang/cargo","slug":"destination-already-exists-use-cargo-init","errorCode":null,"errorMessage":"destination `{}` already exists\n\nUse `cargo init` to initialize the directory","messagePattern":"destination `(.+?)` already exists\n\nUse `cargo init` to initialize the directory","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_new.rs","lineNumber":462,"sourceCode":"    } else {\n        NewProjectKind::Bin\n    };\n\n    if auto_detect_kind {\n        return kind_from_files;\n    }\n\n    requested_kind\n}\n\npub fn new(opts: &NewOptions, gctx: &GlobalContext) -> CargoResult<()> {\n    let path = &opts.path;\n    let name = get_name(path, opts)?;\n    gctx.shell()\n        .status(\"Creating\", format!(\"{} `{}` package\", opts.kind, name))?;\n\n    if path.exists() {\n        anyhow::bail!(\n            \"destination `{}` already exists\\n\\n\\\n             Use `cargo init` to initialize the directory\",\n            path.display()\n        )\n    }\n    check_path(path, &mut gctx.shell())?;\n\n    let is_bin = opts.kind.is_bin();\n\n    check_name(name, opts.name.is_none(), is_bin, &mut gctx.shell())?;\n\n    let mkopts = MkOptions {\n        version_control: opts.version_control,\n        path,\n        name,\n        source_files: vec![plan_new_source_file(opts.kind.is_bin())],\n        edition: opts.edition.as_deref(),\n        registry: opts.registry.as_deref(),","sourceCodeStart":444,"sourceCodeEnd":480,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_new.rs#L444-L480","documentation":"In the `new` entrypoint, Cargo checks `path.exists()` before creating anything. `cargo new` is meant to scaffold into a fresh directory, so if the destination already exists (file or directory), it bails and points the user to `cargo init`, which is the correct command for an existing directory.","triggerScenarios":"`cargo new myproj` where `myproj` already exists in the cwd; `cargo new ./myproj` after a previous failed run; passing an existing path.","commonSituations":"Re-running cargo new after an interrupted attempt, target directory left over from a deleted project, tab-completion landing on an existing folder, scripting that calls cargo new without checking first.","solutions":["If you want to use the existing directory, run `cargo init myproj` instead","If the directory is stale, remove it (`rm -rf myproj`) and re-run `cargo new myproj`","Pick a different name: `cargo new myproj2`"],"exampleFix":"# before\ncargo new myproj   # myproj already exists\n# after (option A): use the existing dir\ncargo init myproj\n# after (option B): start fresh\nrm -rf myproj && cargo new myproj","handlingStrategy":"validation","validationCode":"use std::path::Path;\nfn ensure_new_destination(path: &Path) -> std::io::Result<()> {\n    if path.exists() {\n        Err(std::io::Error::new(std::io::ErrorKind::AlreadyExists, format!(\"{path:?} exists; use cargo init\")))\n    } else { Ok(()) }\n}\n\nensure_new_destination(Path::new(\"myproj\"))?;","typeGuard":"import { existsSync } from 'fs';\nfunction isNewTarget(path: string): boolean { return !existsSync(path); }","tryCatchPattern":null,"preventionTips":["Always check `!path.exists()` before `cargo new` in scripts","Prefer `cargo init` when reusing an existing directory","Make CI scripts idempotent by cleaning target dirs first"],"tags":["cargo-new","filesystem","destination","cli-usage"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}