rust-lang/cargo · error
destination ` ` already exists Use `cargo init` to…
Error message
destination `{}` already exists
Use `cargo init` to initialize the directory What it means
`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.
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`.
Example fix
# before $ ls myproject/ $ cargo new myproject # error: destination already exists # after $ cargo init myproject # initialize the existing directory # or $ rm -rf myproject && cargo new myproject
Defensive patterns
Strategy: validation
Validate before calling
use std::path::Path;
fn is_new_target_clear(path: &Path) -> bool { !path.exists() }
// if !is_new_target_clear(path) { use `cargo init` instead } Type guard
fn should_use_init(path: &Path) -> bool { path.exists() } Prevention
- 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`.
When it happens
Trigger: 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.
Common situations: 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).
Related errors
- can't specify both lib and binary outputs
- cannot create package in the home directory help: use…
- `cargo init` cannot be run on existing Cargo packages help…
- unknown vcs specification
- can only edit absolute paths, got
AI-assisted analysis of rust-lang/cargo@98a09e7e7d (2026-08-11).
Data as JSON: /api/errors/08c046285e8193d2.
Report an issue: GitHub.
Appendix: source
Thrown at src/ops/cargo_new.rs:411
} else {
NewProjectKind::Bin
};
if auto_detect_kind {
return kind_from_files;
}
requested_kind
}
pub fn new(opts: &NewOptions, gctx: &GlobalContext) -> CargoResult<()> {
let path = &opts.path;
let name = get_name(path, opts)?;
gctx.shell()
.status("Creating", format!("{} `{}` package", opts.kind, name))?;
if path.exists() {
anyhow::bail!(
"destination `{}` already exists\n\n\
Use `cargo init` to initialize the directory",
path.display()
)
}
check_path(path, &mut gctx.shell())?;
let is_bin = opts.kind.is_bin();
check_name(name, opts.name.is_none(), is_bin, &mut gctx.shell())?;
let mkopts = MkOptions {
version_control: opts.version_control,
path,
name,
source_files: vec![plan_new_source_file(opts.kind.is_bin())],
edition: opts.edition.as_deref(),
registry: opts.registry.as_deref(),View on GitHub (pinned to 98a09e7e7d)