Skip to content

Conversation

@terror
Copy link
Contributor

@terror terror commented Jan 28, 2026

Resolves #16562

Running cargo init in the home directory can lead to confusing states, particularly for new users experimenting with Cargo. A Cargo.toml in the home directory causes issues with file discovery, as Cargo will treat the entire home directory as a package root when running commands from any subdirectory.

This diff adds a check to error out when cargo init is run in the home directory, with a message suggesting to use cargo init <path> instead.

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

rustbot commented Jan 28, 2026

r? @ehuss

rustbot has assigned @ehuss.
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

@terror
Copy link
Contributor Author

terror commented Jan 28, 2026

r? @epage

@rustbot rustbot assigned epage and unassigned ehuss Jan 28, 2026
@epage
Copy link
Contributor

epage commented Jan 28, 2026

Thanks for working on this!

As a heads up, we''ve found it really helpful for contributors and reviewers to split the tests out into their own commit before the change with them passing, showing the existing behavior you wish to change. Then you fix commit will update the test to reflect the new behavior with the diff between them showing how your changes affected behavior. For more on this, including example PRs, see https://doc.crates.io/contrib/process/working-on-cargo.html#submitting-a-pull-request.

@terror terror force-pushed the cargo-init-home-directory branch from b17eb3d to a3621f6 Compare January 28, 2026 05:57
@terror terror force-pushed the cargo-init-home-directory branch from a3621f6 to fd59dc2 Compare January 28, 2026 06:08
Copy link
Contributor

@epage epage left a comment

Choose a reason for hiding this comment

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

Thanks!

As this was approved during a Cargo Team meeting and this is a UX command, I'll directly merge rather than do an FCP.

View changes since this review

@epage epage added this pull request to the merge queue Jan 28, 2026
Comment on lines +500 to +507
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"
)
}
}
Copy link
Member

@ranger-ross ranger-ross Jan 28, 2026

Choose a reason for hiding this comment

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

nit: you can flatten these if statements

if let Some(home) = home_dir() && path == &home { ... }

#[cargo_test]
fn case() {
snapbox::cmd::Command::cargo_ui()
.arg_line("init --vcs none")
Copy link
Member

@ranger-ross ranger-ross Jan 28, 2026

Choose a reason for hiding this comment

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

why is --vcs none removed in the second commit?
(Or added in the first commit?)

Merged via the queue into rust-lang:master with commit fc2cea5 Jan 28, 2026
29 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 28, 2026
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.

cargo init in the home directory can lead to confusing states

5 participants