Skip to content

Commit 015143c

Browse files
committed
Auto merge of #10902 - epage:lock, r=ehuss
fix(add): Update the lock file This is done in the command, rather than in the op, - To consistently construct the `Workspace` - It is more composable as an API A downside is we update the git dependencies a second time and any sources we didn't initially update. Unlike the proposal in the attached issue, this does not roll back on error. - For some errors, the user might want to debug what went wrong. Losing the intermediate state makes that difficult - Rollback adds its own complications and risks, including since its non-atomic We've already tried to address most potential errors during `cargo add`s processing. To meet this desire, we now error if `--locked` is passed in and we would change the manifest. See that individual commit's message for more details. Fixes #10901
2 parents 85b500c + 5789c12 commit 015143c

File tree

56 files changed

+273
-32
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+273
-32
lines changed

src/bin/cargo/commands/add.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use cargo::ops::cargo_add::add;
77
use cargo::ops::cargo_add::AddOptions;
88
use cargo::ops::cargo_add::DepOp;
99
use cargo::ops::cargo_add::DepTable;
10+
use cargo::ops::resolve_ws;
1011
use cargo::util::command_prelude::*;
1112
use cargo::util::interning::InternedString;
1213
use cargo::CargoResult;
@@ -193,6 +194,12 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
193194
};
194195
add(&ws, &options)?;
195196

197+
if !dry_run {
198+
// Reload the workspace since we've changed dependencies
199+
let ws = args.workspace(config)?;
200+
resolve_ws(&ws)?;
201+
}
202+
196203
Ok(())
197204
}
198205

src/cargo/ops/cargo_add/manifest.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,7 @@ impl str::FromStr for Manifest {
238238

239239
impl std::fmt::Display for Manifest {
240240
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
241-
let s = self.data.to_string();
242-
s.fmt(f)
241+
self.data.fmt(f)
243242
}
244243
}
245244

@@ -433,6 +432,12 @@ impl LocalManifest {
433432
}
434433
}
435434

435+
impl std::fmt::Display for LocalManifest {
436+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
437+
self.manifest.fmt(f)
438+
}
439+
}
440+
436441
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
437442
enum DependencyStatus {
438443
None,

src/cargo/ops/cargo_add/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub fn add(workspace: &Workspace<'_>, options: &AddOptions<'_>) -> CargoResult<(
6262

6363
let manifest_path = options.spec.manifest_path().to_path_buf();
6464
let mut manifest = LocalManifest::try_new(&manifest_path)?;
65+
let original_raw_manifest = manifest.to_string();
6566
let legacy = manifest.get_legacy_sections();
6667
if !legacy.is_empty() {
6768
anyhow::bail!(
@@ -142,6 +143,16 @@ pub fn add(workspace: &Workspace<'_>, options: &AddOptions<'_>) -> CargoResult<(
142143
}
143144
}
144145

146+
if options.config.locked() {
147+
let new_raw_manifest = manifest.to_string();
148+
if original_raw_manifest != new_raw_manifest {
149+
anyhow::bail!(
150+
"the manifest file {} needs to be updated but --locked was passed to prevent this",
151+
manifest.path.display()
152+
);
153+
}
154+
}
155+
145156
if options.dry_run {
146157
options.config.shell().warn("aborting add due to dry run")?;
147158
} else {

tests/testsuite/cargo_add/build_prefer_existing_version/in/dependency/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
[package]
44
name = "cargo-list-test-fixture-dependency"
55
version = "0.0.0"
6+
7+
[features]
8+
one = []
9+
two = []

tests/testsuite/cargo_add/build_prefer_existing_version/out/dependency/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
[package]
44
name = "cargo-list-test-fixture-dependency"
55
version = "0.0.0"
6+
7+
[features]
8+
one = []
9+
two = []
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
Adding cargo-list-test-fixture-dependency (local) to build-dependencies.
2+
Features:
3+
- one
4+
- two

tests/testsuite/cargo_add/dev_prefer_existing_version/in/dependency/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
[package]
44
name = "cargo-list-test-fixture-dependency"
55
version = "0.0.0"
6+
7+
[features]
8+
one = []
9+
two = []

tests/testsuite/cargo_add/dev_prefer_existing_version/out/dependency/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
[package]
44
name = "cargo-list-test-fixture-dependency"
55
version = "0.0.0"
6+
7+
[features]
8+
one = []
9+
two = []
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
Adding cargo-list-test-fixture-dependency (local) to dev-dependencies.
2+
Features:
3+
- one
4+
- two
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Updating git repository `[ROOTURL]/git-package`
22
Adding git-package (git) to dependencies.
3+
Updating git repository `[ROOTURL]/git-package`

0 commit comments

Comments
 (0)