-
-
Notifications
You must be signed in to change notification settings - Fork 3k
init: improve error message and add tests #16643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
weihanglo
merged 2 commits into
rust-lang:master
from
raushan728:feature/init-diagnostic-fix
Feb 15, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
| "#]]); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
raushan728 marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
| "#]]); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.