From 42a9f08bcb6e4042c47ac3b4734589a087ff4bb8 Mon Sep 17 00:00:00 2001 From: Remo Senekowitsch Date: Mon, 13 Jan 2025 19:25:26 +0100 Subject: [PATCH] Make target dir self-ignoring --- src/cargo/core/workspace.rs | 11 +++++++++-- src/cargo/ops/cargo_new.rs | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index a81c6b9304e..4ef1d70e9e3 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -402,9 +402,16 @@ impl<'gctx> Workspace<'gctx> { } pub fn target_dir(&self) -> Filesystem { - self.target_dir + let target_dir = self + .target_dir .clone() - .unwrap_or_else(|| self.default_target_dir()) + .unwrap_or_else(|| self.default_target_dir()); + + let path = target_dir.as_path_unlocked(); + paths::create_dir_all(path).unwrap(); + std::fs::write(path.join(".gitignore"), "*\n").unwrap(); + + target_dir } fn default_target_dir(&self) -> Filesystem { diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index 88d2d6bf162..2ea18513285 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -680,6 +680,10 @@ fn write_ignore_file(base_path: &Path, list: &IgnoreList, vcs: VersionControl) - } for fp_ignore in match vcs { + // TODO: Stop writing the .gitignore file by default once enough + // time has passed. The target directory generated by cargo is now + // self-ignoring, but the .gitignore file should still be generated + // while people are still using older toolchains. VersionControl::Git => vec![base_path.join(".gitignore")], VersionControl::Hg => vec![base_path.join(".hgignore")], VersionControl::Pijul => vec![base_path.join(".ignore")],