From 46b861499a2f36ac5d1247746ca76eddb20a9271 Mon Sep 17 00:00:00 2001 From: Charles Samborski Date: Fri, 16 Jun 2023 03:45:43 +0200 Subject: [PATCH] fix(new): Don't ignore `Cargo.lock` for all package types Following the default guidance to commit a lockfile, this updates `cargo new` to do it by default. --- src/cargo/ops/cargo_new.rs | 6 ------ src/doc/src/faq.md | 4 +++- tests/testsuite/init/auto_git/out/.gitignore | 1 - .../init/fossil_autodetect/out/.fossil-settings/clean-glob | 1 - .../init/fossil_autodetect/out/.fossil-settings/ignore-glob | 1 - tests/testsuite/init/git_autodetect/out/.gitignore | 1 - .../git_ignore_exists_no_conflicting_entries/out/.gitignore | 1 - tests/testsuite/init/inferred_lib_with_git/out/.gitignore | 1 - tests/testsuite/init/mercurial_autodetect/out/.hgignore | 1 - tests/testsuite/init/pijul_autodetect/out/.ignore | 1 - tests/testsuite/init/simple_git/out/.gitignore | 1 - .../testsuite/init/simple_git_ignore_exists/out/.gitignore | 1 - tests/testsuite/init/simple_hg/out/.hgignore | 1 - tests/testsuite/init/simple_hg_ignore_exists/out/.hgignore | 1 - tests/testsuite/new.rs | 4 ++-- 15 files changed, 5 insertions(+), 21 deletions(-) diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index b113671b078e..6a9f71455ad9 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -93,7 +93,6 @@ struct MkOptions<'a> { path: &'a Path, name: &'a str, source_files: Vec, - bin: bool, edition: Option<&'a str>, registry: Option<&'a str>, } @@ -448,7 +447,6 @@ pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> { path, name, source_files: vec![plan_new_source_file(opts.kind.is_bin(), name.to_string())], - bin: is_bin, edition: opts.edition.as_deref(), registry: opts.registry.as_deref(), }; @@ -553,7 +551,6 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult { version_control, path, name, - bin: has_bin, source_files: src_paths_types, edition: opts.edition.as_deref(), registry: opts.registry.as_deref(), @@ -745,9 +742,6 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> { // for all mutually-incompatible VCS in terms of syntax are in sync. let mut ignore = IgnoreList::new(); ignore.push("/target", "^target$", "target"); - if !opts.bin { - ignore.push("/Cargo.lock", "^Cargo.lock$", "Cargo.lock"); - } let vcs = opts.version_control.unwrap_or_else(|| { let in_existing_vcs = existing_vcs_repo(path.parent().unwrap_or(path), config.cwd()); diff --git a/src/doc/src/faq.md b/src/doc/src/faq.md index 17eb2cdee384..7b0255a74cc5 100644 --- a/src/doc/src/faq.md +++ b/src/doc/src/faq.md @@ -104,7 +104,8 @@ issue][cargo-issues]. ### Why have `Cargo.lock` in version control? -Whether you do is dependent on the needs of your package. +While [`cargo new`] defaults to tracking `Cargo.lock` in version control, +whether you do is dependent on the needs of your package. The purpose of a `Cargo.lock` lockfile is to describe the state of the world at the time of a successful build. @@ -137,6 +138,7 @@ For example: For strategies to verify newer versions of dependencies via CI, see [Verifying Latest Dependencies](guide/continuous-integration.md#verifying-latest-dependencies). +[`cargo new`]: commands/cargo-new.md [`cargo add`]: commands/cargo-add.md [`cargo install`]: commands/cargo-install.md diff --git a/tests/testsuite/init/auto_git/out/.gitignore b/tests/testsuite/init/auto_git/out/.gitignore index 4fffb2f89cbd..ea8c4bf7f35f 100644 --- a/tests/testsuite/init/auto_git/out/.gitignore +++ b/tests/testsuite/init/auto_git/out/.gitignore @@ -1,2 +1 @@ /target -/Cargo.lock diff --git a/tests/testsuite/init/fossil_autodetect/out/.fossil-settings/clean-glob b/tests/testsuite/init/fossil_autodetect/out/.fossil-settings/clean-glob index a9d37c560c6a..eb5a316cbd19 100644 --- a/tests/testsuite/init/fossil_autodetect/out/.fossil-settings/clean-glob +++ b/tests/testsuite/init/fossil_autodetect/out/.fossil-settings/clean-glob @@ -1,2 +1 @@ target -Cargo.lock diff --git a/tests/testsuite/init/fossil_autodetect/out/.fossil-settings/ignore-glob b/tests/testsuite/init/fossil_autodetect/out/.fossil-settings/ignore-glob index a9d37c560c6a..eb5a316cbd19 100644 --- a/tests/testsuite/init/fossil_autodetect/out/.fossil-settings/ignore-glob +++ b/tests/testsuite/init/fossil_autodetect/out/.fossil-settings/ignore-glob @@ -1,2 +1 @@ target -Cargo.lock diff --git a/tests/testsuite/init/git_autodetect/out/.gitignore b/tests/testsuite/init/git_autodetect/out/.gitignore index 4fffb2f89cbd..ea8c4bf7f35f 100644 --- a/tests/testsuite/init/git_autodetect/out/.gitignore +++ b/tests/testsuite/init/git_autodetect/out/.gitignore @@ -1,2 +1 @@ /target -/Cargo.lock diff --git a/tests/testsuite/init/git_ignore_exists_no_conflicting_entries/out/.gitignore b/tests/testsuite/init/git_ignore_exists_no_conflicting_entries/out/.gitignore index e2e02f22f3c4..914d4ecb721a 100644 --- a/tests/testsuite/init/git_ignore_exists_no_conflicting_entries/out/.gitignore +++ b/tests/testsuite/init/git_ignore_exists_no_conflicting_entries/out/.gitignore @@ -3,4 +3,3 @@ # Added by cargo /target -/Cargo.lock diff --git a/tests/testsuite/init/inferred_lib_with_git/out/.gitignore b/tests/testsuite/init/inferred_lib_with_git/out/.gitignore index 4fffb2f89cbd..ea8c4bf7f35f 100644 --- a/tests/testsuite/init/inferred_lib_with_git/out/.gitignore +++ b/tests/testsuite/init/inferred_lib_with_git/out/.gitignore @@ -1,2 +1 @@ /target -/Cargo.lock diff --git a/tests/testsuite/init/mercurial_autodetect/out/.hgignore b/tests/testsuite/init/mercurial_autodetect/out/.hgignore index 0cc56e7ff73a..3b11056a1295 100644 --- a/tests/testsuite/init/mercurial_autodetect/out/.hgignore +++ b/tests/testsuite/init/mercurial_autodetect/out/.hgignore @@ -1,2 +1 @@ ^target$ -^Cargo.lock$ diff --git a/tests/testsuite/init/pijul_autodetect/out/.ignore b/tests/testsuite/init/pijul_autodetect/out/.ignore index 4fffb2f89cbd..ea8c4bf7f35f 100644 --- a/tests/testsuite/init/pijul_autodetect/out/.ignore +++ b/tests/testsuite/init/pijul_autodetect/out/.ignore @@ -1,2 +1 @@ /target -/Cargo.lock diff --git a/tests/testsuite/init/simple_git/out/.gitignore b/tests/testsuite/init/simple_git/out/.gitignore index 4fffb2f89cbd..ea8c4bf7f35f 100644 --- a/tests/testsuite/init/simple_git/out/.gitignore +++ b/tests/testsuite/init/simple_git/out/.gitignore @@ -1,2 +1 @@ /target -/Cargo.lock diff --git a/tests/testsuite/init/simple_git_ignore_exists/out/.gitignore b/tests/testsuite/init/simple_git_ignore_exists/out/.gitignore index 4447742e0eb5..5d097ae696c9 100644 --- a/tests/testsuite/init/simple_git_ignore_exists/out/.gitignore +++ b/tests/testsuite/init/simple_git_ignore_exists/out/.gitignore @@ -6,4 +6,3 @@ # already existing elements were commented out #/target -/Cargo.lock diff --git a/tests/testsuite/init/simple_hg/out/.hgignore b/tests/testsuite/init/simple_hg/out/.hgignore index 0cc56e7ff73a..3b11056a1295 100644 --- a/tests/testsuite/init/simple_hg/out/.hgignore +++ b/tests/testsuite/init/simple_hg/out/.hgignore @@ -1,2 +1 @@ ^target$ -^Cargo.lock$ diff --git a/tests/testsuite/init/simple_hg_ignore_exists/out/.hgignore b/tests/testsuite/init/simple_hg_ignore_exists/out/.hgignore index dd9ddffeb23a..a23820d48fff 100644 --- a/tests/testsuite/init/simple_hg_ignore_exists/out/.hgignore +++ b/tests/testsuite/init/simple_hg_ignore_exists/out/.hgignore @@ -3,4 +3,3 @@ # Added by cargo ^target$ -^Cargo.lock$ diff --git a/tests/testsuite/new.rs b/tests/testsuite/new.rs index b9ddcf2d7c9a..91a2871e97ea 100644 --- a/tests/testsuite/new.rs +++ b/tests/testsuite/new.rs @@ -95,7 +95,7 @@ fn simple_git() { let fp = paths::root().join("foo/.gitignore"); let contents = fs::read_to_string(&fp).unwrap(); - assert_eq!(contents, "/target\n/Cargo.lock\n",); + assert_eq!(contents, "/target\n",); cargo_process("build").cwd(&paths::root().join("foo")).run(); } @@ -112,7 +112,7 @@ fn simple_hg() { let fp = paths::root().join("foo/.hgignore"); let contents = fs::read_to_string(&fp).unwrap(); - assert_eq!(contents, "^target$\n^Cargo.lock$\n",); + assert_eq!(contents, "^target$\n",); cargo_process("build").cwd(&paths::root().join("foo")).run(); }