Skip to content

Commit

Permalink
fix(add): Update the lock file
Browse files Browse the repository at this point in the history
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
  • Loading branch information
epage committed Jul 26, 2022
1 parent 323c7bc commit 5789c12
Show file tree
Hide file tree
Showing 23 changed files with 126 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/bin/cargo/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cargo::ops::cargo_add::add;
use cargo::ops::cargo_add::AddOptions;
use cargo::ops::cargo_add::DepOp;
use cargo::ops::cargo_add::DepTable;
use cargo::ops::resolve_ws;
use cargo::util::command_prelude::*;
use cargo::util::interning::InternedString;
use cargo::CargoResult;
Expand Down Expand Up @@ -193,6 +194,12 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
};
add(&ws, &options)?;

if !dry_run {
// Reload the workspace since we've changed dependencies
let ws = args.workspace(config)?;
resolve_ws(&ws)?;
}

Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_add/git/stderr.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Updating git repository `[ROOTURL]/git-package`
Adding git-package (git) to dependencies.
Updating git repository `[ROOTURL]/git-package`
1 change: 1 addition & 0 deletions tests/testsuite/cargo_add/git_branch/stderr.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Updating git repository `[ROOTURL]/git-package`
Adding git-package (git) to dependencies.
Updating git repository `[ROOTURL]/git-package`
1 change: 1 addition & 0 deletions tests/testsuite/cargo_add/git_dev/stderr.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Updating git repository `[ROOTURL]/git-package`
Adding git-package (git) to dev-dependencies.
Updating git repository `[ROOTURL]/git-package`
1 change: 1 addition & 0 deletions tests/testsuite/cargo_add/git_inferred_name/stderr.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Updating git repository `[ROOTURL]/git-package`
Updating git repository `[ROOTURL]/git-package`
Adding git-package (git) to dependencies.
Updating git repository `[ROOTURL]/git-package`
1 change: 1 addition & 0 deletions tests/testsuite/cargo_add/git_multiple_names/stderr.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Updating git repository `[ROOTURL]/git-package`
Adding my-package1 (git) to dependencies.
Adding my-package2 (git) to dependencies.
Updating git repository `[ROOTURL]/git-package`
2 changes: 1 addition & 1 deletion tests/testsuite/cargo_add/git_registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn git_registry() {
])
.current_dir(cwd)
.assert()
.success()
.failure()
.stdout_matches_path(curr_dir!().join("stdout.log"))
.stderr_matches_path(curr_dir!().join("stderr.log"));

Expand Down
4 changes: 4 additions & 0 deletions tests/testsuite/cargo_add/git_registry/stderr.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
Updating git repository `[ROOTURL]/versioned-package`
Adding versioned-package (git) to dependencies.
error: failed to parse manifest at `[ROOT]/case/Cargo.toml`

Caused by:
dependency (versioned-package) specification is ambiguous. Only one of `git` or `registry` is allowed.
1 change: 1 addition & 0 deletions tests/testsuite/cargo_add/git_rev/stderr.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Updating git repository `[ROOTURL]/git-package`
Adding git-package (git) to dependencies.
Updating git repository `[ROOTURL]/git-package`
1 change: 1 addition & 0 deletions tests/testsuite/cargo_add/git_tag/stderr.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Updating git repository `[ROOTURL]/git-package`
Adding git-package (git) to dependencies.
Updating git repository `[ROOTURL]/git-package`
1 change: 1 addition & 0 deletions tests/testsuite/cargo_add/list_features_path/stderr.log
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
+ nose
- eyes
- optional-dependency
Updating `dummy-registry` index
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
- mouth
- nose
- optional-dependency
Updating `dummy-registry` index
16 changes: 16 additions & 0 deletions tests/testsuite/cargo_add/locked_unchanged/in/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tests/testsuite/cargo_add/lockfile_updated/in/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testsuite/cargo_add/lockfile_updated/in/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[workspace]

[package]
name = "cargo-list-test-fixture"
version = "0.0.0"

[dependencies]
unrelateed-crate = "0.2.0"
Empty file.
25 changes: 25 additions & 0 deletions tests/testsuite/cargo_add/lockfile_updated/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::prelude::*;
use cargo_test_support::Project;

use crate::cargo_add::init_registry;
use cargo_test_support::curr_dir;

#[cargo_test]
fn lockfile_updated() {
init_registry();
let project = Project::from_template(curr_dir!().join("in"));
let project_root = project.root();
let cwd = &project_root;

snapbox::cmd::Command::cargo_ui()
.arg("add")
.arg_line("my-package")
.current_dir(cwd)
.assert()
.success()
.stdout_matches_path(curr_dir!().join("stdout.log"))
.stderr_matches_path(curr_dir!().join("stderr.log"));

assert_ui().subset_matches(curr_dir!().join("out"), &project_root);
}
23 changes: 23 additions & 0 deletions tests/testsuite/cargo_add/lockfile_updated/out/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/testsuite/cargo_add/lockfile_updated/out/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[workspace]

[package]
name = "cargo-list-test-fixture"
version = "0.0.0"

[dependencies]
my-package = "99999.0.0"
unrelateed-crate = "0.2.0"
2 changes: 2 additions & 0 deletions tests/testsuite/cargo_add/lockfile_updated/stderr.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Updating `dummy-registry` index
Adding my-package v99999.0.0 to dependencies.
Empty file.
4 changes: 4 additions & 0 deletions tests/testsuite/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ mod list_features_path;
mod list_features_path_no_default;
mod locked_changed;
mod locked_unchanged;
mod lockfile_updated;
mod manifest_path_package;
mod merge_activated_features;
mod multiple_conflicts_with_features;
Expand Down Expand Up @@ -133,6 +134,9 @@ fn add_registry_packages(alt: bool) {
cargo_test_support::registry::Package::new(name, "0.1.1+my-package")
.alternative(alt)
.publish();
cargo_test_support::registry::Package::new(name, "0.2.0+my-package")
.alternative(alt)
.publish();
cargo_test_support::registry::Package::new(name, "0.2.3+my-package")
.alternative(alt)
.publish();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Updating git repository `[ROOTURL]/versioned-package`
Adding versioned-package (git) to optional dependencies.
Updating git repository `[ROOTURL]/versioned-package`

0 comments on commit 5789c12

Please sign in to comment.