Skip to content

Commit

Permalink
fix(new): Don't ignore Cargo.lock for all package types
Browse files Browse the repository at this point in the history
Following the default guidance to commit a lockfile, this updates
`cargo new` to do it by default.
  • Loading branch information
demurgos authored and epage committed Jul 19, 2023
1 parent 5b187d7 commit 8f2ff1e
Show file tree
Hide file tree
Showing 15 changed files with 5 additions and 21 deletions.
6 changes: 0 additions & 6 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ struct MkOptions<'a> {
path: &'a Path,
name: &'a str,
source_files: Vec<SourceFileInformation>,
bin: bool,
edition: Option<&'a str>,
registry: Option<&'a str>,
}
Expand Down Expand Up @@ -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(),
};
Expand Down Expand Up @@ -553,7 +551,6 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<NewProjectKind> {
version_control,
path,
name,
bin: has_bin,
source_files: src_paths_types,
edition: opts.edition.as_deref(),
registry: opts.registry.as_deref(),
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 3 additions & 1 deletion src/doc/src/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -137,6 +138,7 @@ For example:
For strategies to verify newer dependencies,
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

Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/init/auto_git/out/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/target
/Cargo.lock
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
target
Cargo.lock
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
target
Cargo.lock
1 change: 0 additions & 1 deletion tests/testsuite/init/git_autodetect/out/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/target
/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
# Added by cargo

/target
/Cargo.lock
1 change: 0 additions & 1 deletion tests/testsuite/init/inferred_lib_with_git/out/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/target
/Cargo.lock
1 change: 0 additions & 1 deletion tests/testsuite/init/mercurial_autodetect/out/.hgignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
^target$
^Cargo.lock$
1 change: 0 additions & 1 deletion tests/testsuite/init/pijul_autodetect/out/.ignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/target
/Cargo.lock
1 change: 0 additions & 1 deletion tests/testsuite/init/simple_git/out/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/target
/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@
# already existing elements were commented out

#/target
/Cargo.lock
1 change: 0 additions & 1 deletion tests/testsuite/init/simple_hg/out/.hgignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
^target$
^Cargo.lock$
1 change: 0 additions & 1 deletion tests/testsuite/init/simple_hg_ignore_exists/out/.hgignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
# Added by cargo

^target$
^Cargo.lock$
4 changes: 2 additions & 2 deletions tests/testsuite/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down

0 comments on commit 8f2ff1e

Please sign in to comment.