Skip to content

Commit 5789c12

Browse files
committed
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. We are not rolling back on error. - For some errors, the user might want to debug what went wrong - Rollback adds its own complications and risks, including since its non-atomic Fixes #10901
1 parent 323c7bc commit 5789c12

File tree

23 files changed

+126
-1
lines changed

23 files changed

+126
-1
lines changed

src/bin/cargo/commands/add.rs

+7
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

+1
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`
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`
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 dev-dependencies.
3+
Updating git repository `[ROOTURL]/git-package`
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Updating git repository `[ROOTURL]/git-package`
22
Updating git repository `[ROOTURL]/git-package`
33
Adding git-package (git) to dependencies.
4+
Updating git repository `[ROOTURL]/git-package`
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Updating git repository `[ROOTURL]/git-package`
22
Adding my-package1 (git) to dependencies.
33
Adding my-package2 (git) to dependencies.
4+
Updating git repository `[ROOTURL]/git-package`

tests/testsuite/cargo_add/git_registry/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn git_registry() {
3232
])
3333
.current_dir(cwd)
3434
.assert()
35-
.success()
35+
.failure()
3636
.stdout_matches_path(curr_dir!().join("stdout.log"))
3737
.stderr_matches_path(curr_dir!().join("stderr.log"));
3838

Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
Updating git repository `[ROOTURL]/versioned-package`
22
Adding versioned-package (git) to dependencies.
3+
error: failed to parse manifest at `[ROOT]/case/Cargo.toml`
4+
5+
Caused by:
6+
dependency (versioned-package) specification is ambiguous. Only one of `git` or `registry` is allowed.
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`
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`

tests/testsuite/cargo_add/list_features_path/stderr.log

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
+ nose
55
- eyes
66
- optional-dependency
7+
Updating `dummy-registry` index

tests/testsuite/cargo_add/list_features_path_no_default/stderr.log

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
- mouth
55
- nose
66
- optional-dependency
7+
Updating `dummy-registry` index

tests/testsuite/cargo_add/locked_unchanged/in/Cargo.lock

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testsuite/cargo_add/lockfile_updated/in/Cargo.lock

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[workspace]
2+
3+
[package]
4+
name = "cargo-list-test-fixture"
5+
version = "0.0.0"
6+
7+
[dependencies]
8+
unrelateed-crate = "0.2.0"

tests/testsuite/cargo_add/lockfile_updated/in/src/lib.rs

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use cargo_test_support::compare::assert_ui;
2+
use cargo_test_support::prelude::*;
3+
use cargo_test_support::Project;
4+
5+
use crate::cargo_add::init_registry;
6+
use cargo_test_support::curr_dir;
7+
8+
#[cargo_test]
9+
fn lockfile_updated() {
10+
init_registry();
11+
let project = Project::from_template(curr_dir!().join("in"));
12+
let project_root = project.root();
13+
let cwd = &project_root;
14+
15+
snapbox::cmd::Command::cargo_ui()
16+
.arg("add")
17+
.arg_line("my-package")
18+
.current_dir(cwd)
19+
.assert()
20+
.success()
21+
.stdout_matches_path(curr_dir!().join("stdout.log"))
22+
.stderr_matches_path(curr_dir!().join("stderr.log"));
23+
24+
assert_ui().subset_matches(curr_dir!().join("out"), &project_root);
25+
}

tests/testsuite/cargo_add/lockfile_updated/out/Cargo.lock

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[workspace]
2+
3+
[package]
4+
name = "cargo-list-test-fixture"
5+
version = "0.0.0"
6+
7+
[dependencies]
8+
my-package = "99999.0.0"
9+
unrelateed-crate = "0.2.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Updating `dummy-registry` index
2+
Adding my-package v99999.0.0 to dependencies.

tests/testsuite/cargo_add/lockfile_updated/stdout.log

Whitespace-only changes.

tests/testsuite/cargo_add/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ mod list_features_path;
5050
mod list_features_path_no_default;
5151
mod locked_changed;
5252
mod locked_unchanged;
53+
mod lockfile_updated;
5354
mod manifest_path_package;
5455
mod merge_activated_features;
5556
mod multiple_conflicts_with_features;
@@ -133,6 +134,9 @@ fn add_registry_packages(alt: bool) {
133134
cargo_test_support::registry::Package::new(name, "0.1.1+my-package")
134135
.alternative(alt)
135136
.publish();
137+
cargo_test_support::registry::Package::new(name, "0.2.0+my-package")
138+
.alternative(alt)
139+
.publish();
136140
cargo_test_support::registry::Package::new(name, "0.2.3+my-package")
137141
.alternative(alt)
138142
.publish();
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Updating git repository `[ROOTURL]/versioned-package`
22
Adding versioned-package (git) to optional dependencies.
3+
Updating git repository `[ROOTURL]/versioned-package`

0 commit comments

Comments
 (0)