Skip to content

Commit 3bb02f2

Browse files
committed
Auto merge of #12382 - epage:lock, r=weihanglo
fix: Change the defaults to always check-in `Cargo.lock` ### What does this PR try to resolve? Having libraries leave `Cargo.lock` "on the float" has been serving the ecosystem well, including - Encouraging people to validate that latest dependencies work - Encouraging ecosystem-wide health by acting as a "distributed crater" These benefits are limited though. The policy is inconsistent between workspaces with or without `[[bin]]`s, reducing the affect of testing the latest. This is also subject to when CI last ran; for passively maintained projects, there is little coverage of new dependencies. There are also costs associated with this policy - `git bisect` is using an unpredictable set of dependencies, affecting the ability to identify root cause - This is another potential cause for Red CI / broken local development if version is yanked or a bug is introduced - Impacting the perceived level of quality for a project - Confusing to new contributors who might not recognize why CI failed and assume its their fault - Requiring context switching from maintainers to get fixes in In particular, since this policy was decided, there has been an increased interest in supporting an MSRV (as recently as v1.56.0, cargo gained support for specifying a package's MSRV). This has led to long discussions on *what* MSRV a package should use (e.g. rust-lang/libs-team#72,. time-rs/time#535). Worst, there has been a growing trend for people to set an non-semver upper bound on dependencies, making it so packages can't work well with other packages (see #12323). Tooling support would help with this (#9930) but the sooner we address this, the less entrenched bad practices will be. On the positive side, since the policy was decided - In general, CI became easier to setup and maintain with Github Actions compared to TravisCI - Dependabot went GA on Github in 2021 (https://github.blog/changelog/2021-03-31-dependabot-version-updates-are-now-generally-available) - I believe Dependabot will post security update PRs even when Dependabot is not more generally enabled So to get some of the benefit from not checking in `Cargo.lock`, we can recommend either automatically applying updates or having CI check the latest dependencies in a way to get this out of the critical path of PRs and releases. Since there is no one right answer on how to solve all of these problems, we're documenting these trade offs so people can make the choice that is most appropriate for them. However, we are changing the default to a consistent "always check it in" as the answer for those who don't want to think about it. Prior art - [Yarn (Javascript)](https://yarnpkg.com/getting-started/qa#should-lockfiles-be-committed-to-the-repository) - [Poetry (Python)](https://python-poetry.org/docs/basic-usage/#committing-your-poetrylock-file-to-version-control) - [Bundler (Ruby)](https://bundler.io/guides/faq.html#using-gemfiles-inside-gems) Fixes #8728 ### How should we test and review this PR? Please review per-commit. I tried to minimize changes I made to the structure of the CI document In #8728, I brought up having a CI reference page. I keep going back and forth on whether this is guide-level material or reference-level material. Obviously, right now I'm leaning towards it being guide-level. ### Additional information This changes cargo from telling people what to do to giving them a starting point, or default, and giving them the information to make their own choice, if needed. So the question for defaults is who are we targeting? For a default path in documentation, it would be for new to intermediate users. As for `cargo new`, we've been prioritizing new users over those that run it frequently (boiler plate comment, bin is default, etc). See #8728 for the FCP on this policy change
2 parents 699230f + 54ad4a0 commit 3bb02f2

File tree

17 files changed

+103
-73
lines changed

17 files changed

+103
-73
lines changed

src/cargo/ops/cargo_new.rs

-6
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ struct MkOptions<'a> {
9393
path: &'a Path,
9494
name: &'a str,
9595
source_files: Vec<SourceFileInformation>,
96-
bin: bool,
9796
edition: Option<&'a str>,
9897
registry: Option<&'a str>,
9998
}
@@ -448,7 +447,6 @@ pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
448447
path,
449448
name,
450449
source_files: vec![plan_new_source_file(opts.kind.is_bin(), name.to_string())],
451-
bin: is_bin,
452450
edition: opts.edition.as_deref(),
453451
registry: opts.registry.as_deref(),
454452
};
@@ -553,7 +551,6 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<NewProjectKind> {
553551
version_control,
554552
path,
555553
name,
556-
bin: has_bin,
557554
source_files: src_paths_types,
558555
edition: opts.edition.as_deref(),
559556
registry: opts.registry.as_deref(),
@@ -745,9 +742,6 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
745742
// for all mutually-incompatible VCS in terms of syntax are in sync.
746743
let mut ignore = IgnoreList::new();
747744
ignore.push("/target", "^target$", "target");
748-
if !opts.bin {
749-
ignore.push("/Cargo.lock", "^Cargo.lock$", "Cargo.lock");
750-
}
751745

752746
let vcs = opts.version_control.unwrap_or_else(|| {
753747
let in_existing_vcs = existing_vcs_repo(path.parent().unwrap_or(path), config.cwd());

src/doc/src/faq.md

+39-25
Original file line numberDiff line numberDiff line change
@@ -102,33 +102,47 @@ issue][cargo-issues].
102102

103103
[cargo-issues]: https://github.com/rust-lang/cargo/issues
104104

105-
### Why do binaries have `Cargo.lock` in version control, but not libraries?
105+
### Why have `Cargo.lock` in version control?
106+
107+
While [`cargo new`] defaults to tracking `Cargo.lock` in version control,
108+
whether you do is dependent on the needs of your package.
106109

107110
The purpose of a `Cargo.lock` lockfile is to describe the state of the world at
108-
the time of a successful build. Cargo uses the lockfile to provide
109-
deterministic builds on different times and different systems, by ensuring that
110-
the exact same dependencies and versions are used as when the `Cargo.lock` file
111-
was originally generated.
112-
113-
This property is most desirable from applications and packages which are at the
114-
very end of the dependency chain (binaries). As a result, it is recommended that
115-
all binaries check in their `Cargo.lock`.
116-
117-
For libraries the situation is somewhat different. A library is not only used by
118-
the library developers, but also any downstream consumers of the library. Users
119-
dependent on the library will not inspect the library’s `Cargo.lock` (even if it
120-
exists). This is precisely because a library should **not** be deterministically
121-
recompiled for all users of the library.
122-
123-
If a library ends up being used transitively by several dependencies, it’s
124-
likely that just a single copy of the library is desired (based on semver
125-
compatibility). If Cargo used all of the dependencies' `Cargo.lock` files,
126-
then multiple copies of the library could be used, and perhaps even a version
127-
conflict.
128-
129-
In other words, libraries specify SemVer requirements for their dependencies but
130-
cannot see the full picture. Only end products like binaries have a full
131-
picture to decide what versions of dependencies should be used.
111+
the time of a successful build.
112+
Cargo uses the lockfile to provide deterministic builds at different times and
113+
on different systems,
114+
by ensuring that the exact same dependencies and versions are used as when the
115+
`Cargo.lock` file was originally generated.
116+
117+
Deterministic builds help with
118+
- Running `git bisect` to find the root cause of a bug
119+
- Ensuring CI only fails due to new commits and not external factors
120+
- Reducing confusion when contributors see different behavior as compared to
121+
other contributors or CI
122+
123+
Having this snapshot of dependencies can also help when projects need to be
124+
verified against consistent versions of dependencies, like when
125+
- Verifying a minimum-supported Rust version (MSRV) that is less than the latest
126+
version of a dependency supports
127+
- Verifying human readable output which won't have compatibility guarantees
128+
(e.g. snapshot testing error messages to ensure they are "understandable", a
129+
metric too fuzzy to automate)
130+
131+
However, this determinism can give a false sense of security because
132+
`Cargo.lock` does not affect the consumers of your package, only `Cargo.toml` does that.
133+
For example:
134+
- [`cargo install`] will select the latest dependencies unless `--locked` is
135+
passed in.
136+
- New dependencies, like those added with [`cargo add`], will be locked to the latest version
137+
138+
The lockfile can also be a source of merge conflicts.
139+
140+
For strategies to verify newer versions of dependencies via CI,
141+
see [Verifying Latest Dependencies](guide/continuous-integration.md#verifying-latest-dependencies).
142+
143+
[`cargo new`]: commands/cargo-new.md
144+
[`cargo add`]: commands/cargo-add.md
145+
[`cargo install`]: commands/cargo-install.md
132146

133147
### Can libraries use `*` as a version for their dependencies?
134148

src/doc/src/guide/cargo-toml-vs-cargo-lock.md

+5-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ about them, here’s a summary:
88
* `Cargo.lock` contains exact information about your dependencies. It is
99
maintained by Cargo and should not be manually edited.
1010

11-
If you’re building a non-end product, such as a rust library that other rust
12-
[packages][def-package] will depend on, put `Cargo.lock` in your
13-
`.gitignore`. If you’re building an end product, which are executable like
14-
command-line tool or an application, or a system library with crate-type of
15-
`staticlib` or `cdylib`, check `Cargo.lock` into `git`. If you're curious
16-
about why that is, see
17-
["Why do binaries have `Cargo.lock` in version control, but not libraries?" in the
18-
FAQ](../faq.md#why-do-binaries-have-cargolock-in-version-control-but-not-libraries).
11+
When in doubt, check `Cargo.lock` into git.
12+
For a better understanding of why and what the alternatives might be, see
13+
[“Why have Cargo.lock in version control?” in the FAQ](../faq.md#why-have-cargolock-in-version-control).
14+
We recommend pairing this with
15+
[Verifying Latest Dependencies](continuous-integration.md#verifying-latest-dependencies)
1916

2017
Let’s dig in a little bit more.
2118

src/doc/src/guide/continuous-integration.md

+57-20
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
11
## Continuous Integration
22

3-
### Travis CI
3+
### Getting Started
44

5-
To test your [package][def-package] on Travis CI, here is a sample
6-
`.travis.yml` file:
7-
8-
```yaml
9-
language: rust
10-
rust:
11-
- stable
12-
- beta
13-
- nightly
14-
matrix:
15-
allow_failures:
16-
- rust: nightly
17-
```
18-
19-
This will test all three release channels, but any breakage in nightly
20-
will not fail your overall build. Please see the [Travis CI Rust
21-
documentation](https://docs.travis-ci.com/user/languages/rust/) for more
22-
information.
5+
A basic CI will build and test your projects:
236

247
### GitHub Actions
258

@@ -122,4 +105,58 @@ channel, but any breakage in nightly will not fail your overall build. Please
122105
see the [builds.sr.ht documentation](https://man.sr.ht/builds.sr.ht/) for more
123106
information.
124107

125-
[def-package]: ../appendix/glossary.md#package '"package" (glossary entry)'
108+
### Verifying Latest Dependencies
109+
110+
When [specifying dependencies](../reference/specifying-dependencies.md) in
111+
`Cargo.toml`, they generally match a range of versions.
112+
Exhaustively testing all version combination would be unwieldy.
113+
Verifying the latest versions would at least test for users who run [`cargo
114+
add`] or [`cargo install`].
115+
116+
When testing the latest versions some considerations are:
117+
- Minimizing external factors affecting local development or CI
118+
- Rate of new dependencies being published
119+
- Level of risk a project is willing to accept
120+
- CI costs, including indirect costs like if a CI service has a maximum for
121+
parallel runners, causing new jobs to be serialized when at the maxium.
122+
123+
Some potential solutions include:
124+
- [Not checking in the `Cargo.lock`](../faq.md#why-have-cargolock-in-version-control)
125+
- Depending on PR velocity, many versions may go untested
126+
- This comes at the cost of determinism
127+
- Have a CI job verify the latest dependencies but mark it to "continue on failure"
128+
- Depending on the CI service, failures might not be obvious
129+
- Depending on PR velocity, may use more resources than necessary
130+
- Have a scheduled CI job to verify latest dependencies
131+
- A hosted CI service may disable scheduled jobs for repositories that
132+
haven't been touched in a while, affecting passively maintained packages
133+
- Depending on the CI service, notifications might not be routed to people
134+
who can act on the failure
135+
- If not balanced with dependency publish rate, may not test enough versions
136+
or may do redundant testing
137+
- Regularly update dependencies through PRs, like with [Dependabot] or [RenovateBot]
138+
- Can isolate dependencies to their own PR or roll them up into a single PR
139+
- Only uses the resources necessary
140+
- Can configure the frequency to balance CI resources and coverage of dependency versions
141+
142+
An example CI job to verify latest dependencies, using Github Actions:
143+
```yaml
144+
jobs:
145+
latest_deps:
146+
name: Latest Dependencies
147+
runs-on: ubuntu-latest
148+
continue-on-error: true
149+
steps:
150+
- uses: actions/checkout@v3
151+
- run: rustup update stable && rustup default stable
152+
- run: cargo update --verbose
153+
- run: cargo build --verbose
154+
- run: cargo test --verbose
155+
```
156+
For projects with higher risks of per-platform or per-Rust version failures,
157+
more combinations may want to be tested.
158+
159+
[`cargo add`]: ../commands/cargo-add.md
160+
[`cargo install`]: ../commands/cargo-install.md
161+
[Dependabot]: https://docs.github.com/en/code-security/dependabot/working-with-dependabot
162+
[RenovateBot]: https://renovatebot.com/
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
/target
2-
/Cargo.lock
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
target
2-
Cargo.lock
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
target
2-
Cargo.lock
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
/target
2-
/Cargo.lock

tests/testsuite/cargo_init/git_ignore_exists_no_conflicting_entries/out/.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
# Added by cargo
44

55
/target
6-
/Cargo.lock
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
/target
2-
/Cargo.lock
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
^target$
2-
^Cargo.lock$
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
/target
2-
/Cargo.lock
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
/target
2-
/Cargo.lock

tests/testsuite/cargo_init/simple_git_ignore_exists/out/.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@
66
# already existing elements were commented out
77

88
#/target
9-
/Cargo.lock
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
^target$
2-
^Cargo.lock$

tests/testsuite/cargo_init/simple_hg_ignore_exists/out/.hgignore

-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
# Added by cargo
44

55
^target$
6-
^Cargo.lock$

tests/testsuite/new.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fn simple_git() {
9595

9696
let fp = paths::root().join("foo/.gitignore");
9797
let contents = fs::read_to_string(&fp).unwrap();
98-
assert_eq!(contents, "/target\n/Cargo.lock\n",);
98+
assert_eq!(contents, "/target\n",);
9999

100100
cargo_process("build").cwd(&paths::root().join("foo")).run();
101101
}
@@ -112,7 +112,7 @@ fn simple_hg() {
112112

113113
let fp = paths::root().join("foo/.hgignore");
114114
let contents = fs::read_to_string(&fp).unwrap();
115-
assert_eq!(contents, "^target$\n^Cargo.lock$\n",);
115+
assert_eq!(contents, "^target$\n",);
116116

117117
cargo_process("build").cwd(&paths::root().join("foo")).run();
118118
}

0 commit comments

Comments
 (0)