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
10 changes: 10 additions & 0 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::util::{GlobalContext, restricted_names};
use anyhow::{Context as _, anyhow};
use cargo_util::paths::{self, write_atomic};
use cargo_util_schemas::manifest::PackageName;
use home::home_dir;
use serde::Deserialize;
use serde::de;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -495,6 +496,15 @@ pub fn init(opts: &NewOptions, gctx: &GlobalContext) -> CargoResult<NewProjectKi
}

let path = &opts.path;

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"
)
}
}
Comment on lines +500 to +507
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 { ... }

let name = get_name(path, opts)?;
let mut src_paths_types = vec![];
detect_source_paths_and_types(path, name, &mut src_paths_types)?;
Expand Down
15 changes: 15 additions & 0 deletions tests/testsuite/cargo_init/in_home_directory/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::prelude::*;
use cargo_test_support::file;
use cargo_test_support::paths;
use cargo_test_support::str;

#[cargo_test]
fn case() {
snapbox::cmd::Command::cargo_ui()
.arg_line("init")
.current_dir(paths::home())
.assert()
.code(101)
.stdout_eq(str![""])
.stderr_eq(file!["stderr.term.svg"]);
}
31 changes: 31 additions & 0 deletions tests/testsuite/cargo_init/in_home_directory/stderr.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/testsuite/cargo_init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod git_autodetect;
mod git_ignore_exists_no_conflicting_entries;
mod help;
mod ignores_failure_to_format_source;
mod in_home_directory;
mod inferred_bin_with_git;
mod inferred_lib_with_git;
mod inherit_workspace_package_table;
Expand Down
11 changes: 5 additions & 6 deletions tests/testsuite/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,16 +556,15 @@ fn git_default_branch() {

#[cargo_test]
fn non_utf8_str_in_ignore_file() {
let gitignore = paths::home().join(".gitignore");
File::create(gitignore).unwrap();
let dir = paths::home().join("foo");
fs::create_dir(&dir).unwrap();
fs::write(dir.join(".gitignore"), &[0xFF, 0xFE]).unwrap();

fs::write(paths::home().join(".gitignore"), &[0xFF, 0xFE]).unwrap();

cargo_process(&format!("init {} --vcs git", paths::home().display()))
cargo_process(&format!("init {} --vcs git", dir.display()))
.with_status(101)
.with_stderr_data(str![[r#"
[CREATING] binary (application) package
[ERROR] Failed to create package `home` at `[ROOT]/home`
[ERROR] Failed to create package `foo` at `[ROOT]/home/foo`

Caused by:
Character at line 0 is invalid. Cargo only supports UTF-8.
Expand Down