From 3e712402abbca5bbf51542402534df50d170d2f5 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 11 Feb 2022 18:48:25 +0800 Subject: [PATCH 1/2] cargo-new should not add ignore rule on Cargo.lock inside subdirs Have traced down the issue. It feel like the original intent is to ignore `Cargo.lock` and `target` at project root but not subdirectories. 1. The original implementation did ignore root `/Cargo.lock`. https://github.com/rust-lang/cargo/pull/321 2. Someday one wanted to support both gitignore and hgingore's syntax and removed the leading slash. https://github.com/rust-lang/cargo/pull/1247 3. Later, one found that we should not ignore `target` other than under root directory and added `/target` back. https://github.com/rust-lang/cargo/pull/4099 4. It turns out that the syntax is not compatible between gitignore and hgignore. Therefore, one started to use hgignore special syntax to handle `Cargo.lock`. https://github.com/rust-lang/cargo/pull/4342 This commit rollbacks to what original implementation tries to do. --- src/cargo/ops/cargo_new.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index 85d8beaf770..0df39321e3f 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -716,7 +716,7 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> { let mut ignore = IgnoreList::new(); ignore.push("/target", "^target/", "target"); if !opts.bin { - ignore.push("Cargo.lock", "glob:Cargo.lock", "Cargo.lock,*/Cargo.lock"); + ignore.push("/Cargo.lock", "^Cargo.lock$", "Cargo.lock"); } let vcs = opts.version_control.unwrap_or_else(|| { From 552b52a8942ca9e0bef9e008db388148bd77b20d Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 11 Feb 2022 18:49:35 +0800 Subject: [PATCH 2/2] Test for cargo new/init autodetct and ignore file rules --- tests/testsuite/init.rs | 48 +++++++++++++++++++++++++++++++++++++---- tests/testsuite/new.rs | 2 +- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/tests/testsuite/init.rs b/tests/testsuite/init.rs index c3216703974..992ec285d1d 100644 --- a/tests/testsuite/init.rs +++ b/tests/testsuite/init.rs @@ -83,7 +83,7 @@ fn simple_git_ignore_exists() { # already existing elements were commented out\n\ \n\ #/target\n\ - Cargo.lock\n", + /Cargo.lock\n", ); cargo_process("build").cwd(&paths::root().join("foo")).run(); @@ -105,7 +105,7 @@ fn git_ignore_exists_no_conflicting_entries() { # Added by cargo\n\ \n\ /target\n\ - Cargo.lock\n", + /Cargo.lock\n", ); } @@ -337,7 +337,9 @@ fn git_autodetect() { assert!(paths::root().join("Cargo.toml").is_file()); assert!(paths::root().join("src/lib.rs").is_file()); assert!(paths::root().join(".git").is_dir()); - assert!(paths::root().join(".gitignore").is_file()); + let path = paths::root().join(".gitignore"); + assert!(paths::root().join(&path).is_file()); + assert_eq!(fs::read_to_string(&path).unwrap(), "/target\n/Cargo.lock\n",); } #[cargo_test] @@ -349,7 +351,45 @@ fn mercurial_autodetect() { assert!(paths::root().join("Cargo.toml").is_file()); assert!(paths::root().join("src/lib.rs").is_file()); assert!(!paths::root().join(".git").is_dir()); - assert!(paths::root().join(".hgignore").is_file()); + let path = paths::root().join(".hgignore"); + assert!(paths::root().join(&path).is_file()); + assert_eq!( + fs::read_to_string(&path).unwrap(), + "^target/\n^Cargo.lock$\n", + ); +} + +#[cargo_test] +fn fossil_autodetect() { + fs::create_dir(&paths::root().join(".fossil")).unwrap(); + + cargo_process("init --lib").run(); + + assert!(paths::root().join("Cargo.toml").is_file()); + assert!(paths::root().join("src/lib.rs").is_file()); + assert!(!paths::root().join(".git").is_dir()); + for path in [ + ".fossil-settings/ignore-glob", + ".fossil-settings/clean-glob", + ] { + let path = paths::root().join(path); + assert!(paths::root().join(&path).is_file()); + assert_eq!(fs::read_to_string(&path).unwrap(), "target\nCargo.lock\n",); + } +} + +#[cargo_test] +fn pijul_autodetect() { + fs::create_dir(&paths::root().join(".pijul")).unwrap(); + + cargo_process("init --lib").run(); + + assert!(paths::root().join("Cargo.toml").is_file()); + assert!(paths::root().join("src/lib.rs").is_file()); + assert!(!paths::root().join(".git").is_dir()); + let path = paths::root().join(".ignore"); + assert!(paths::root().join(&path).is_file()); + assert_eq!(fs::read_to_string(&path).unwrap(), "/target\n/Cargo.lock\n",); } #[cargo_test] diff --git a/tests/testsuite/new.rs b/tests/testsuite/new.rs index 89a3d47e0e3..ae23842c14f 100644 --- a/tests/testsuite/new.rs +++ b/tests/testsuite/new.rs @@ -89,7 +89,7 @@ fn simple_git() { let fp = paths::root().join("foo/.gitignore"); let contents = fs::read_to_string(&fp).unwrap(); - assert_eq!(contents, "/target\nCargo.lock\n",); + assert_eq!(contents, "/target\n/Cargo.lock\n",); cargo_process("build").cwd(&paths::root().join("foo")).run(); }