Skip to content

Commit 174d845

Browse files
committed
Make target dir self-ignoring
1 parent f15df8f commit 174d845

File tree

12 files changed

+26
-37
lines changed

12 files changed

+26
-37
lines changed

src/cargo/core/workspace.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,16 @@ impl<'gctx> Workspace<'gctx> {
402402
}
403403

404404
pub fn target_dir(&self) -> Filesystem {
405-
self.target_dir
405+
let target_dir = self
406+
.target_dir
406407
.clone()
407-
.unwrap_or_else(|| self.default_target_dir())
408+
.unwrap_or_else(|| self.default_target_dir());
409+
410+
let path = target_dir.as_path_unlocked();
411+
paths::create_dir_all(path).unwrap();
412+
std::fs::write(path.join(".gitignore"), "*\n").unwrap();
413+
414+
target_dir
408415
}
409416

410417
fn default_target_dir(&self) -> Filesystem {

src/cargo/ops/cargo_new.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ fn write_ignore_file(base_path: &Path, list: &IgnoreList, vcs: VersionControl) -
680680
}
681681

682682
for fp_ignore in match vcs {
683-
VersionControl::Git => vec![base_path.join(".gitignore")],
683+
VersionControl::Git => return Ok(()), // target-dir already contains wildcard gitignore file
684684
VersionControl::Hg => vec![base_path.join(".hgignore")],
685685
VersionControl::Pijul => vec![base_path.join(".ignore")],
686686
// Fossil has a cleaning functionality configured in a separate file.

tests/testsuite/cargo_init/auto_git/out/.gitignore

-1
This file was deleted.

tests/testsuite/cargo_init/explicit_bin_with_git/out/.gitignore

-1
This file was deleted.

tests/testsuite/cargo_init/git_autodetect/out/.gitignore

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
**/some.file
2-
3-
# Added by cargo
4-
5-
/target
1+
**/some.file

tests/testsuite/cargo_init/inferred_bin_with_git/out/.gitignore

-1
This file was deleted.

tests/testsuite/cargo_init/inferred_lib_with_git/out/.gitignore

-1
This file was deleted.

tests/testsuite/cargo_init/simple_git/out/.gitignore

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
/target
2-
**/some.file
3-
4-
# Added by cargo
5-
#
6-
# already existing elements were commented out
7-
8-
#/target
2+
**/some.file

tests/testsuite/clean.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ fn clean_dry_run() {
849849
p.cargo("clean --dry-run")
850850
.with_stdout_data("")
851851
.with_stderr_data(str![[r#"
852-
[SUMMARY] 0 files
852+
[SUMMARY] [FILE_NUM] files, [FILE_SIZE]B total
853853
[WARNING] no files deleted due to --dry-run
854854
855855
"#]])

tests/testsuite/new.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,14 @@ fn simple_git() {
105105
assert!(paths::root().join("foo/Cargo.toml").is_file());
106106
assert!(paths::root().join("foo/src/lib.rs").is_file());
107107
assert!(paths::root().join("foo/.git").is_dir());
108-
assert!(paths::root().join("foo/.gitignore").is_file());
109-
110-
let fp = paths::root().join("foo/.gitignore");
111-
let contents = fs::read_to_string(&fp).unwrap();
112-
assert_eq!(contents, "/target\n",);
113108

114109
cargo_process("build").cwd(&paths::root().join("foo")).run();
110+
111+
assert!(paths::root().join("foo/target/.gitignore").is_file());
112+
113+
let fp = paths::root().join("foo/target/.gitignore");
114+
let contents = fs::read_to_string(&fp).unwrap();
115+
assert_eq!(contents, "*\n",);
115116
}
116117

117118
#[cargo_test(requires = "hg")]
@@ -272,7 +273,7 @@ fn git_prefers_command_line() {
272273
.unwrap();
273274

274275
cargo_process("new foo --vcs git").run();
275-
assert!(paths::root().join("foo/.gitignore").exists());
276+
assert!(!paths::root().join("foo/.gitignore").exists());
276277
assert!(!fs::read_to_string(paths::root().join("foo/Cargo.toml"))
277278
.unwrap()
278279
.contains("authors ="));
@@ -283,7 +284,7 @@ fn subpackage_no_git() {
283284
cargo_process("new foo").run();
284285

285286
assert!(paths::root().join("foo/.git").is_dir());
286-
assert!(paths::root().join("foo/.gitignore").is_file());
287+
assert!(!paths::root().join("foo/.gitignore").is_file());
287288

288289
let subpackage = paths::root().join("foo").join("components");
289290
fs::create_dir(&subpackage).unwrap();
@@ -302,7 +303,7 @@ fn subpackage_git_with_gitignore() {
302303
cargo_process("new foo").run();
303304

304305
assert!(paths::root().join("foo/.git").is_dir());
305-
assert!(paths::root().join("foo/.gitignore").is_file());
306+
assert!(!paths::root().join("foo/.gitignore").is_file());
306307

307308
let gitignore = paths::root().join("foo/.gitignore");
308309
fs::write(gitignore, b"components").unwrap();
@@ -314,7 +315,7 @@ fn subpackage_git_with_gitignore() {
314315
assert!(paths::root()
315316
.join("foo/components/subcomponent/.git")
316317
.is_dir());
317-
assert!(paths::root()
318+
assert!(!paths::root()
318319
.join("foo/components/subcomponent/.gitignore")
319320
.is_file());
320321
}
@@ -330,7 +331,7 @@ fn subpackage_git_with_vcs_arg() {
330331
assert!(paths::root()
331332
.join("foo/components/subcomponent/.git")
332333
.is_dir());
333-
assert!(paths::root()
334+
assert!(!paths::root()
334335
.join("foo/components/subcomponent/.gitignore")
335336
.is_file());
336337
}
@@ -544,13 +545,10 @@ fn non_utf8_str_in_ignore_file() {
544545
fs::write(paths::home().join(".gitignore"), &[0xFF, 0xFE]).unwrap();
545546

546547
cargo_process(&format!("init {} --vcs git", paths::home().display()))
547-
.with_status(101)
548+
.with_status(0)
548549
.with_stderr_data(str![[r#"
549550
[CREATING] binary (application) package
550-
[ERROR] Failed to create package `home` at `[ROOT]/home`
551-
552-
Caused by:
553-
Character at line 0 is invalid. Cargo only supports UTF-8.
551+
[NOTE] see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
554552
555553
"#]])
556554
.run();

0 commit comments

Comments
 (0)