init: improve error message and add tests#16643
Conversation
|
r? @epage rustbot has assigned @epage. Use Why was this reviewer chosen?The reviewer was selected based on:
|
There was a problem hiding this comment.
Note that we prefer atomic commit patterns.
init: improve error message and add tests
The commit summary contains "and" indicating that it might be able to split into multiple. And I think reserved_name_core test case has nothing to do we the the cargo init improvement hence splitting is better.
There was a problem hiding this comment.
My bad. I've split the changes into two atomic commits now
|
|
||
| if path.join("Cargo.toml").exists() { | ||
| anyhow::bail!("`cargo init` cannot be run on existing Cargo packages") | ||
| anyhow::bail!( |
There was a problem hiding this comment.
We are migrating to annotate-snippets #15944. While this case we can defer the migration, we also start following rustc diagnostics style guide closer recently: https://rustc-dev-guide.rust-lang.org/diagnostics.html?highlight=diag#diagnostic-output-style-guide. That means this should have help: and be more direct to action and sometimes more succinct.
There was a problem hiding this comment.
Updated the message to follow the rustc diagnostic style
src/cargo/ops/cargo_new.rs
Outdated
| anyhow::bail!("`cargo init` cannot be run on existing Cargo packages") | ||
| anyhow::bail!( | ||
| "`cargo init` cannot be run on existing Cargo packages\n\ | ||
| If you want to create a new package here, please remove `Cargo.toml` first, \ |
There was a problem hiding this comment.
I am not sure if this is a good suggestion. Removing Cargo.toml just fixes the cargo init validation logic, but other files may still linger. I don't think we should give that suggestion. We could probably give some for certain circumstances though. What is in your mind that is reasonable suggestion?
There was a problem hiding this comment.
Agreed. I've removed the 'remove Cargo.toml' suggestion and replaced it with a more helpful pointer to 'cargo new'
8beff23 to
1b71665
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
Hi @weihanglo i have updated the err msg style and split the changes into two atomic commits as suggested also cleaned up the merge history. Plz take a look |
Update cargo submodule 10 commits in ce69df6f72a3b6a2b5c722ba68ddef255344b31c..8cc0cb136772b8f54eafe0d163fcb7226a06af0c 2026-02-12 12:39:45 +0000 to 2026-02-17 12:16:26 +0000 - docs(layout): Updated layout module docs to document new layout (rust-lang/cargo#16502) - fix(host-config): host.linker should not apply to non host unit (rust-lang/cargo#16641) - init: improve error message and add tests (rust-lang/cargo#16643) - Corrected doc comment for build script root_output path (rust-lang/cargo#16645) - Changed build script run `output` dir to `stdout` in new build-dir layout (rust-lang/cargo#16644) - test: add test case for verify-project with invalid TOML (rust-lang/cargo#16640) - test(script): Show remaining workspace behavors (rust-lang/cargo#16633) - fix(host-config): `host.runner` should not apply to `cargo run` (rust-lang/cargo#16638) - refactor(help): simplify code structure (rust-lang/cargo#16627) - test: Remove unused docker ip_address (rust-lang/cargo#16636)
Update cargo submodule 10 commits in ce69df6f72a3b6a2b5c722ba68ddef255344b31c..8cc0cb136772b8f54eafe0d163fcb7226a06af0c 2026-02-12 12:39:45 +0000 to 2026-02-17 12:16:26 +0000 - docs(layout): Updated layout module docs to document new layout (rust-lang/cargo#16502) - fix(host-config): host.linker should not apply to non host unit (rust-lang/cargo#16641) - init: improve error message and add tests (rust-lang/cargo#16643) - Corrected doc comment for build script root_output path (rust-lang/cargo#16645) - Changed build script run `output` dir to `stdout` in new build-dir layout (rust-lang/cargo#16644) - test: add test case for verify-project with invalid TOML (rust-lang/cargo#16640) - test(script): Show remaining workspace behavors (rust-lang/cargo#16633) - fix(host-config): `host.runner` should not apply to `cargo run` (rust-lang/cargo#16638) - refactor(help): simplify code structure (rust-lang/cargo#16627) - test: Remove unused docker ip_address (rust-lang/cargo#16636)
Update cargo submodule 10 commits in ce69df6f72a3b6a2b5c722ba68ddef255344b31c..8cc0cb136772b8f54eafe0d163fcb7226a06af0c 2026-02-12 12:39:45 +0000 to 2026-02-17 12:16:26 +0000 - docs(layout): Updated layout module docs to document new layout (rust-lang/cargo#16502) - fix(host-config): host.linker should not apply to non host unit (rust-lang/cargo#16641) - init: improve error message and add tests (rust-lang/cargo#16643) - Corrected doc comment for build script root_output path (rust-lang/cargo#16645) - Changed build script run `output` dir to `stdout` in new build-dir layout (rust-lang/cargo#16644) - test: add test case for verify-project with invalid TOML (rust-lang/cargo#16640) - test(script): Show remaining workspace behavors (rust-lang/cargo#16633) - fix(host-config): `host.runner` should not apply to `cargo run` (rust-lang/cargo#16638) - refactor(help): simplify code structure (rust-lang/cargo#16627) - test: Remove unused docker ip_address (rust-lang/cargo#16636)
Improves the error message when Cargo.toml exists and adds tests for reserved names