Skip to content

Commit

Permalink
Make target dir self-ignoring
Browse files Browse the repository at this point in the history
  • Loading branch information
senekor committed Jan 14, 2025
1 parent f15df8f commit 42a9f08
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")],
Expand Down

0 comments on commit 42a9f08

Please sign in to comment.