rust-lang/cargo · error
`cargo init` cannot be run on existing Cargo packages help…
Error message
`cargo init` cannot be run on existing Cargo packages help: use `cargo new` to create a package in a new subdirectory
What it means
`cargo init` is meant for directories that are not yet cargo packages. If `Cargo.toml` already exists in the target directory, init aborts — re-initializing could overwrite or duplicate the existing manifest. The message points to `cargo new` (a fresh subdirectory) as the alternative.
Solutions
- Do nothing — the package already exists; just `cargo build`/`cargo run`.
- If you genuinely need a separate new package, use `cargo new <subdir>` in a non-existent subdirectory.
- If the existing `Cargo.toml` is stale/empty and you want a fresh one, manually back it up and remove it before `cargo init`.
Example fix
# before $ ls Cargo.toml $ cargo init # error: cannot be run on existing Cargo packages # after — it is already a package, just use it: $ cargo build # or create a new package elsewhere: $ cargo new ../other-project
Defensive patterns
Strategy: validation
Validate before calling
use std::path::Path;
fn has_manifest(dir: &Path) -> bool { dir.join("Cargo.toml").exists() }
// if has_manifest(&dir) { skip `cargo init`; it is already a package } Type guard
fn needs_init(dir: &Path) -> bool { !has_manifest(dir) } Prevention
- Check for an existing `Cargo.toml` before `cargo init`.
- Make CI init steps idempotent: skip if already initialized.
- Use `cargo new <subdir>` when you genuinely need a fresh package alongside an existing one.
When it happens
Trigger: Running `cargo init` inside a directory that already contains a `Cargo.toml`. Re-running `cargo init` after it succeeded once. Pointing `cargo init` at an existing crate.
Common situations: Forgetting a folder was already initialized. CI setup scripts that re-init on every run. Cloning a repo and then running `cargo init` out of habit.
Related errors
- cannot create package in the home directory help: use…
- can only edit absolute paths, got
- can't find library ` `, rename file to `src/lib.rs` or…
- could not find ` ` in ` ` or any parent directory, but…
- destination ` ` already exists Use `cargo init` to…
AI-assisted analysis of rust-lang/cargo@98a09e7e7d (2026-08-11).
Data as JSON: /api/errors/651201eee37e6b04.
Report an issue: GitHub.
Appendix: source
Thrown at src/ops/cargo_new.rs:466
let path = &opts.path;
if let Some(home) = home_dir() {
if path == &home {
anyhow::bail!(
"cannot create package in the home directory\n\n\
help: use `cargo init <path>` to create a package in a different directory"
)
}
}
let name = get_name(path, opts)?;
let mut src_paths_types = vec![];
detect_source_paths_and_types(path, name, &mut src_paths_types)?;
let kind = calculate_new_project_kind(opts.kind, opts.auto_detect_kind, &src_paths_types);
gctx.shell()
.status("Creating", format!("{} package", opts.kind))?;
if path.join("Cargo.toml").exists() {
anyhow::bail!(
"`cargo init` cannot be run on existing Cargo packages\n\
help: use `cargo new` to create a package in a new subdirectory"
)
}
check_path(path, &mut gctx.shell())?;
let has_bin = kind.is_bin();
if src_paths_types.is_empty() {
src_paths_types.push(plan_new_source_file(has_bin));
} else if src_paths_types.len() == 1 && !src_paths_types.iter().any(|x| x.bin == has_bin) {
// we've found the only file and it's not the type user wants. Change the type and warn
let file_type = if src_paths_types[0].bin {
NewProjectKind::Bin
} else {
NewProjectKind::Lib
};
gctx.shell().warn(format!(View on GitHub (pinned to 98a09e7e7d)