Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ pub fn new(opts: &NewOptions, gctx: &GlobalContext) -> CargoResult<()> {

mk(gctx, &mkopts).with_context(|| {
format!(
"Failed to create package `{}` at `{}`",
"failed to create package `{}` at `{}`",
name,
path.display()
)
Expand Down Expand Up @@ -513,7 +513,10 @@ pub fn init(opts: &NewOptions, gctx: &GlobalContext) -> CargoResult<NewProjectKi
.status("Creating", format!("{} package", opts.kind))?;

if path.join("Cargo.toml").exists() {
anyhow::bail!("`cargo init` cannot be run on existing Cargo packages")
anyhow::bail!(
Comment thread
raushan728 marked this conversation as resolved.
"`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())?;

Expand Down Expand Up @@ -593,7 +596,7 @@ pub fn init(opts: &NewOptions, gctx: &GlobalContext) -> CargoResult<NewProjectKi

mk(gctx, &mkopts).with_context(|| {
format!(
"Failed to create package `{}` at `{}`",
"failed to create package `{}` at `{}`",
name,
path.display()
)
Expand Down
23 changes: 23 additions & 0 deletions tests/testsuite/cargo_init/error_on_existing_package.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::prelude::*;
use cargo_test_support::paths;
use cargo_test_support::str;
use std::fs;

#[cargo_test]
fn init_error_on_existing_package() {
let project_root = paths::root().join("foo");
fs::create_dir_all(&project_root).unwrap();
fs::write(project_root.join("Cargo.toml"), "").unwrap();

snapbox::cmd::Command::cargo_ui()
.arg_line("init --color=never")
.current_dir(&project_root)
.assert()
.code(101)
.stderr_eq(str![[r#"
Creating binary (application) package
error: `cargo init` cannot be run on existing Cargo packages
help: use `cargo new` to create a package in a new subdirectory

"#]]);
}
2 changes: 2 additions & 0 deletions tests/testsuite/cargo_init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod confused_by_multiple_lib_files;
mod creates_binary_when_both_binlib_present;
mod creates_binary_when_instructed_and_has_lib_file;
mod creates_library_when_instructed_and_has_bin_file;
mod error_on_existing_package;
mod explicit_bin_with_git;
mod formats_source;
mod fossil_autodetect;
Expand All @@ -35,6 +36,7 @@ mod no_filename;
mod path_contains_separator;
mod pijul_autodetect;
mod reserved_name;
mod reserved_name_core;
mod simple_bin;
mod simple_git;
mod simple_git_ignore_exists;
Expand Down
30 changes: 30 additions & 0 deletions tests/testsuite/cargo_init/reserved_name_core.rs
Comment thread
raushan728 marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::prelude::*;
use cargo_test_support::paths;
use cargo_test_support::str;
use std::fs;

#[cargo_test]
fn init_with_reserved_name_core() {
let project_root = paths::root().join("core");
fs::create_dir_all(&project_root).unwrap();

snapbox::cmd::Command::cargo_ui()
.arg_line("init --color=never")
.current_dir(&project_root)
.assert()
.stderr_eq(str![[r#"
Creating binary (application) package
warning: package name `core` may be confused with the package with that name in Rust's standard library
It is recommended to use a different name to avoid problems.
note: the directory name is used as the package name
help: to override the package name, pass `--name <pkgname>`
help: to name the binary "core", use a valid package name, and set the binary name to be different from the package. This can be done by setting the binary filename to `src/bin/core.rs` or change the name in Cargo.toml with:

[[bin]]
name = "core"
path = "src/main.rs"

note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

"#]]);
}