Skip to content

init: improve error message and add tests#16643

Merged
weihanglo merged 2 commits intorust-lang:masterfrom
raushan728:feature/init-diagnostic-fix
Feb 15, 2026
Merged

init: improve error message and add tests#16643
weihanglo merged 2 commits intorust-lang:masterfrom
raushan728:feature/init-diagnostic-fix

Conversation

@raushan728
Copy link
Contributor

Improves the error message when Cargo.toml exists and adds tests for reserved names

@rustbot rustbot added Command-new S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 14, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 14, 2026

r? @epage

rustbot has assigned @epage.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ehuss, @epage, @weihanglo
  • @ehuss, @epage, @weihanglo expanded to ehuss, epage, weihanglo
  • Random selection from ehuss, epage, weihanglo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the message to follow the rustc diagnostic style

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, \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I've removed the 'remove Cargo.toml' suggestion and replaced it with a more helpful pointer to 'cargo new'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. Thank you!

@raushan728 raushan728 force-pushed the feature/init-diagnostic-fix branch from 8beff23 to 1b71665 Compare February 15, 2026 13:49
@rustbot
Copy link
Collaborator

rustbot commented Feb 15, 2026

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.

@raushan728
Copy link
Contributor Author

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

@weihanglo weihanglo enabled auto-merge February 15, 2026 14:03
@weihanglo weihanglo added this pull request to the merge queue Feb 15, 2026
Merged via the queue into rust-lang:master with commit 592058c Feb 15, 2026
29 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 15, 2026
@raushan728 raushan728 deleted the feature/init-diagnostic-fix branch February 17, 2026 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants