rust-lang/cargo · error · anyhow::Error

destination `{}` already exists Use `cargo init` to initial

Error message

destination `{}` already exists

Use `cargo init` to initialize the directory

What it means

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.

Source

Thrown at src/ops/cargo_new.rs:462

    } 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 0e07a15537)

Solutions

  1. If you want to use the existing directory, run `cargo init myproj` instead
  2. If the directory is stale, remove it (`rm -rf myproj`) and re-run `cargo new myproj`
  3. Pick a different name: `cargo new myproj2`

Example fix

# before
cargo new myproj   # myproj already exists
# after (option A): use the existing dir
cargo init myproj
# after (option B): start fresh
rm -rf myproj && cargo new myproj
Defensive patterns

Strategy: validation

Validate before calling

use std::path::Path;
fn ensure_new_destination(path: &Path) -> std::io::Result<()> {
    if path.exists() {
        Err(std::io::Error::new(std::io::ErrorKind::AlreadyExists, format!("{path:?} exists; use cargo init")))
    } else { Ok(()) }
}

ensure_new_destination(Path::new("myproj"))?;

Type guard

import { existsSync } from 'fs';
function isNewTarget(path: string): boolean { return !existsSync(path); }

Prevention

When it happens

Trigger: `cargo new myproj` where `myproj` already exists in the cwd; `cargo new ./myproj` after a previous failed run; passing an existing path.

Common situations: 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.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/08c046285e8193d2.json. Report an issue: GitHub.