{"record":{"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":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ops/cargo_new.rs","lineNumber":411,"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":393,"sourceCodeEnd":429,"githubUrl":"https://github.com/rust-lang/cargo/blob/98a09e7e7d62850f14e5b6132101fc1edd19a16f/src/ops/cargo_new.rs#L393-L429","documentation":"`cargo new` is for creating a project in a NEW directory; if the target path already exists on disk, it refuses to avoid clobbering existing content. The message points the user to `cargo init`, which is the correct command for an existing (possibly empty or pre-populated) directory.","triggerScenarios":"Running `cargo new myproject` when `myproject` already exists (file or directory). Re-running `cargo new` after a partial earlier attempt. Pointing `cargo new` at an existing folder.","commonSituations":"Re-running a scaffold script that didn't clean up. Naming collision with an existing folder. Confusing `new` (must not exist) with `init` (must exist).","solutions":["Use `cargo init myproject` to initialize the existing directory instead.","Or remove/rename the existing path first, then `cargo new myproject`.","Pick a different, non-existent target name for `cargo new`."],"exampleFix":"# before\n$ ls\nmyproject/\n$ cargo new myproject   # error: destination already exists\n\n# after\n$ cargo init myproject     # initialize the existing directory\n# or\n$ rm -rf myproject && cargo new myproject","handlingStrategy":"validation","validationCode":"use std::path::Path;\nfn is_new_target_clear(path: &Path) -> bool { !path.exists() }\n// if !is_new_target_clear(path) { use `cargo init` instead }","typeGuard":"fn should_use_init(path: &Path) -> bool { path.exists() }","tryCatchPattern":null,"preventionTips":["Check path existence before `cargo new`; switch to `cargo init` if it exists.","Treat `new` (non-existent target) vs `init` (existing target) as distinct commands in scripts.","Clean up partial scaffolds before retrying `cargo new`."],"tags":["cargo-new","filesystem","cli-usage"],"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"}