From 6d0ab5fbb90c21a00a93588860dd5dae9d2a2f08 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Wed, 27 Apr 2022 14:15:49 -0500 Subject: [PATCH] Support overwriting a `foo.workspace = true` with a dependency from a different source --- src/cargo/ops/cargo_add/dependency.rs | 11 ++++- src/cargo/ops/cargo_add/manifest.rs | 8 +++- .../add/overwrite_workspace_dep.in/Cargo.toml | 5 ++ .../dependency/Cargo.toml | 3 ++ .../dependency/src/lib.rs | 0 .../primary/Cargo.toml | 8 ++++ .../primary/src/lib.rs | 0 .../overwrite_workspace_dep.out/Cargo.toml | 5 ++ .../dependency/Cargo.toml | 3 ++ .../primary/Cargo.toml | 8 ++++ .../add/overwrite_workspace_dep.stderr | 1 + .../add/overwrite_workspace_dep.stdout | 0 .../Cargo.toml | 5 ++ .../dependency/Cargo.toml | 14 ++++++ .../dependency/src/lib.rs | 0 .../primary/Cargo.toml | 8 ++++ .../primary/src/lib.rs | 0 .../Cargo.toml | 5 ++ .../dependency/Cargo.toml | 14 ++++++ .../primary/Cargo.toml | 8 ++++ .../overwrite_workspace_dep_features.stderr | 10 ++++ .../overwrite_workspace_dep_features.stdout | 0 tests/testsuite/cargo_add.rs | 46 +++++++++++++++++++ 23 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 tests/snapshots/add/overwrite_workspace_dep.in/Cargo.toml create mode 100644 tests/snapshots/add/overwrite_workspace_dep.in/dependency/Cargo.toml create mode 100644 tests/snapshots/add/overwrite_workspace_dep.in/dependency/src/lib.rs create mode 100644 tests/snapshots/add/overwrite_workspace_dep.in/primary/Cargo.toml create mode 100644 tests/snapshots/add/overwrite_workspace_dep.in/primary/src/lib.rs create mode 100644 tests/snapshots/add/overwrite_workspace_dep.out/Cargo.toml create mode 100644 tests/snapshots/add/overwrite_workspace_dep.out/dependency/Cargo.toml create mode 100644 tests/snapshots/add/overwrite_workspace_dep.out/primary/Cargo.toml create mode 100644 tests/snapshots/add/overwrite_workspace_dep.stderr create mode 100644 tests/snapshots/add/overwrite_workspace_dep.stdout create mode 100644 tests/snapshots/add/overwrite_workspace_dep_features.in/Cargo.toml create mode 100644 tests/snapshots/add/overwrite_workspace_dep_features.in/dependency/Cargo.toml create mode 100644 tests/snapshots/add/overwrite_workspace_dep_features.in/dependency/src/lib.rs create mode 100644 tests/snapshots/add/overwrite_workspace_dep_features.in/primary/Cargo.toml create mode 100644 tests/snapshots/add/overwrite_workspace_dep_features.in/primary/src/lib.rs create mode 100644 tests/snapshots/add/overwrite_workspace_dep_features.out/Cargo.toml create mode 100644 tests/snapshots/add/overwrite_workspace_dep_features.out/dependency/Cargo.toml create mode 100644 tests/snapshots/add/overwrite_workspace_dep_features.out/primary/Cargo.toml create mode 100644 tests/snapshots/add/overwrite_workspace_dep_features.stderr create mode 100644 tests/snapshots/add/overwrite_workspace_dep_features.stdout diff --git a/src/cargo/ops/cargo_add/dependency.rs b/src/cargo/ops/cargo_add/dependency.rs index 560b68302a2..0f297872fcf 100644 --- a/src/cargo/ops/cargo_add/dependency.rs +++ b/src/cargo/ops/cargo_add/dependency.rs @@ -3,6 +3,7 @@ use std::fmt::{Display, Formatter}; use std::path::{Path, PathBuf}; use indexmap::IndexSet; +use toml_edit::KeyMut; use super::manifest::str_or_1_len_table; use crate::core::FeatureMap; @@ -476,10 +477,18 @@ impl Dependency { } /// Modify existing entry to match this dependency - pub fn update_toml(&self, crate_root: &Path, item: &mut toml_edit::Item) { + pub fn update_toml<'k>( + &self, + crate_root: &Path, + key: &mut KeyMut<'k>, + item: &mut toml_edit::Item, + ) { if str_or_1_len_table(item) { // Nothing to preserve *item = self.to_toml(crate_root); + if self.source != Some(Source::Workspace(WorkspaceSource)) { + key.fmt(); + } } else if let Some(table) = item.as_table_like_mut() { match &self.source { Some(Source::Registry(src)) => { diff --git a/src/cargo/ops/cargo_add/manifest.rs b/src/cargo/ops/cargo_add/manifest.rs index 63cab6a701d..b90b7808392 100644 --- a/src/cargo/ops/cargo_add/manifest.rs +++ b/src/cargo/ops/cargo_add/manifest.rs @@ -349,8 +349,12 @@ impl LocalManifest { let dep_key = dep.toml_key(); let table = self.get_table_mut(table_path)?; - if let Some(dep_item) = table.as_table_like_mut().unwrap().get_mut(dep_key) { - dep.update_toml(&crate_root, dep_item); + if let Some((mut dep_key, dep_item)) = table + .as_table_like_mut() + .unwrap() + .get_key_value_mut(dep_key) + { + dep.update_toml(&crate_root, &mut dep_key, dep_item); } else { let new_dependency = dep.to_toml(&crate_root); table[dep_key] = new_dependency; diff --git a/tests/snapshots/add/overwrite_workspace_dep.in/Cargo.toml b/tests/snapshots/add/overwrite_workspace_dep.in/Cargo.toml new file mode 100644 index 00000000000..a80d4994911 --- /dev/null +++ b/tests/snapshots/add/overwrite_workspace_dep.in/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] +members = ["primary", "dependency"] + +[workspace.dependencies] +foo = { version = "0.0.0", path = "./dependency" } \ No newline at end of file diff --git a/tests/snapshots/add/overwrite_workspace_dep.in/dependency/Cargo.toml b/tests/snapshots/add/overwrite_workspace_dep.in/dependency/Cargo.toml new file mode 100644 index 00000000000..2d247d4d26c --- /dev/null +++ b/tests/snapshots/add/overwrite_workspace_dep.in/dependency/Cargo.toml @@ -0,0 +1,3 @@ +[package] +name = "foo" +version = "0.0.0" diff --git a/tests/snapshots/add/overwrite_workspace_dep.in/dependency/src/lib.rs b/tests/snapshots/add/overwrite_workspace_dep.in/dependency/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/snapshots/add/overwrite_workspace_dep.in/primary/Cargo.toml b/tests/snapshots/add/overwrite_workspace_dep.in/primary/Cargo.toml new file mode 100644 index 00000000000..aaadbceb163 --- /dev/null +++ b/tests/snapshots/add/overwrite_workspace_dep.in/primary/Cargo.toml @@ -0,0 +1,8 @@ +cargo-features = ["workspace-inheritance"] + +[package] +name = "bar" +version = "0.0.0" + +[dependencies] +foo.workspace = true \ No newline at end of file diff --git a/tests/snapshots/add/overwrite_workspace_dep.in/primary/src/lib.rs b/tests/snapshots/add/overwrite_workspace_dep.in/primary/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/snapshots/add/overwrite_workspace_dep.out/Cargo.toml b/tests/snapshots/add/overwrite_workspace_dep.out/Cargo.toml new file mode 100644 index 00000000000..a80d4994911 --- /dev/null +++ b/tests/snapshots/add/overwrite_workspace_dep.out/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] +members = ["primary", "dependency"] + +[workspace.dependencies] +foo = { version = "0.0.0", path = "./dependency" } \ No newline at end of file diff --git a/tests/snapshots/add/overwrite_workspace_dep.out/dependency/Cargo.toml b/tests/snapshots/add/overwrite_workspace_dep.out/dependency/Cargo.toml new file mode 100644 index 00000000000..2d247d4d26c --- /dev/null +++ b/tests/snapshots/add/overwrite_workspace_dep.out/dependency/Cargo.toml @@ -0,0 +1,3 @@ +[package] +name = "foo" +version = "0.0.0" diff --git a/tests/snapshots/add/overwrite_workspace_dep.out/primary/Cargo.toml b/tests/snapshots/add/overwrite_workspace_dep.out/primary/Cargo.toml new file mode 100644 index 00000000000..d44f047917c --- /dev/null +++ b/tests/snapshots/add/overwrite_workspace_dep.out/primary/Cargo.toml @@ -0,0 +1,8 @@ +cargo-features = ["workspace-inheritance"] + +[package] +name = "bar" +version = "0.0.0" + +[dependencies] +foo = { version = "0.0.0", path = "../dependency" } diff --git a/tests/snapshots/add/overwrite_workspace_dep.stderr b/tests/snapshots/add/overwrite_workspace_dep.stderr new file mode 100644 index 00000000000..d1bc507572f --- /dev/null +++ b/tests/snapshots/add/overwrite_workspace_dep.stderr @@ -0,0 +1 @@ + Adding foo (local) to dependencies. diff --git a/tests/snapshots/add/overwrite_workspace_dep.stdout b/tests/snapshots/add/overwrite_workspace_dep.stdout new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/snapshots/add/overwrite_workspace_dep_features.in/Cargo.toml b/tests/snapshots/add/overwrite_workspace_dep_features.in/Cargo.toml new file mode 100644 index 00000000000..a80d4994911 --- /dev/null +++ b/tests/snapshots/add/overwrite_workspace_dep_features.in/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] +members = ["primary", "dependency"] + +[workspace.dependencies] +foo = { version = "0.0.0", path = "./dependency" } \ No newline at end of file diff --git a/tests/snapshots/add/overwrite_workspace_dep_features.in/dependency/Cargo.toml b/tests/snapshots/add/overwrite_workspace_dep_features.in/dependency/Cargo.toml new file mode 100644 index 00000000000..ef9ec7701ba --- /dev/null +++ b/tests/snapshots/add/overwrite_workspace_dep_features.in/dependency/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "foo" +version = "0.0.0" + +[features] +default-base = [] +default-test-base = [] +default-merge-base = [] +default = ["default-base", "default-test-base", "default-merge-base"] +test-base = [] +test = ["test-base", "default-test-base"] +merge-base = [] +merge = ["merge-base", "default-merge-base"] +unrelated = [] diff --git a/tests/snapshots/add/overwrite_workspace_dep_features.in/dependency/src/lib.rs b/tests/snapshots/add/overwrite_workspace_dep_features.in/dependency/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/snapshots/add/overwrite_workspace_dep_features.in/primary/Cargo.toml b/tests/snapshots/add/overwrite_workspace_dep_features.in/primary/Cargo.toml new file mode 100644 index 00000000000..7ab3f9dd2c1 --- /dev/null +++ b/tests/snapshots/add/overwrite_workspace_dep_features.in/primary/Cargo.toml @@ -0,0 +1,8 @@ +cargo-features = ["workspace-inheritance"] + +[package] +name = "bar" +version = "0.0.0" + +[dependencies] +foo = { workspace = true, features = ["test"] } \ No newline at end of file diff --git a/tests/snapshots/add/overwrite_workspace_dep_features.in/primary/src/lib.rs b/tests/snapshots/add/overwrite_workspace_dep_features.in/primary/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/snapshots/add/overwrite_workspace_dep_features.out/Cargo.toml b/tests/snapshots/add/overwrite_workspace_dep_features.out/Cargo.toml new file mode 100644 index 00000000000..a80d4994911 --- /dev/null +++ b/tests/snapshots/add/overwrite_workspace_dep_features.out/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] +members = ["primary", "dependency"] + +[workspace.dependencies] +foo = { version = "0.0.0", path = "./dependency" } \ No newline at end of file diff --git a/tests/snapshots/add/overwrite_workspace_dep_features.out/dependency/Cargo.toml b/tests/snapshots/add/overwrite_workspace_dep_features.out/dependency/Cargo.toml new file mode 100644 index 00000000000..ef9ec7701ba --- /dev/null +++ b/tests/snapshots/add/overwrite_workspace_dep_features.out/dependency/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "foo" +version = "0.0.0" + +[features] +default-base = [] +default-test-base = [] +default-merge-base = [] +default = ["default-base", "default-test-base", "default-merge-base"] +test-base = [] +test = ["test-base", "default-test-base"] +merge-base = [] +merge = ["merge-base", "default-merge-base"] +unrelated = [] diff --git a/tests/snapshots/add/overwrite_workspace_dep_features.out/primary/Cargo.toml b/tests/snapshots/add/overwrite_workspace_dep_features.out/primary/Cargo.toml new file mode 100644 index 00000000000..c7301765f5a --- /dev/null +++ b/tests/snapshots/add/overwrite_workspace_dep_features.out/primary/Cargo.toml @@ -0,0 +1,8 @@ +cargo-features = ["workspace-inheritance"] + +[package] +name = "bar" +version = "0.0.0" + +[dependencies] +foo = { features = ["test"], path = "../dependency", version = "0.0.0" } diff --git a/tests/snapshots/add/overwrite_workspace_dep_features.stderr b/tests/snapshots/add/overwrite_workspace_dep_features.stderr new file mode 100644 index 00000000000..18ed7c2d8fa --- /dev/null +++ b/tests/snapshots/add/overwrite_workspace_dep_features.stderr @@ -0,0 +1,10 @@ + Adding foo (local) to dependencies. + Features: + + default-base + + default-merge-base + + default-test-base + + test + + test-base + - merge + - merge-base + - unrelated diff --git a/tests/snapshots/add/overwrite_workspace_dep_features.stdout b/tests/snapshots/add/overwrite_workspace_dep_features.stdout new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add.rs b/tests/testsuite/cargo_add.rs index 67e13378669..c6924b37d43 100644 --- a/tests/testsuite/cargo_add.rs +++ b/tests/testsuite/cargo_add.rs @@ -2066,6 +2066,52 @@ fn overwrite_with_rename() { ); } +#[cargo_test] +fn overwrite_workspace_dep() { + init_registry(); + let project = Project::from_template("tests/snapshots/add/overwrite_workspace_dep.in"); + let project_root = project.root(); + let cwd = &project_root; + + snapbox::cmd::Command::cargo() + .masquerade_as_nightly_cargo() + .arg("add") + .args(["foo", "--path", "./dependency", "-p", "bar"]) + .current_dir(cwd) + .assert() + .success() + .stdout_matches_path("tests/snapshots/add/overwrite_workspace_dep.stdout") + .stderr_matches_path("tests/snapshots/add/overwrite_workspace_dep.stderr"); + + assert().subset_matches( + "tests/snapshots/add/overwrite_workspace_dep.out", + &project_root, + ); +} + +#[cargo_test] +fn overwrite_workspace_dep_features() { + init_registry(); + let project = Project::from_template("tests/snapshots/add/overwrite_workspace_dep_features.in"); + let project_root = project.root(); + let cwd = &project_root; + + snapbox::cmd::Command::cargo() + .masquerade_as_nightly_cargo() + .arg("add") + .args(["foo", "--path", "./dependency", "-p", "bar"]) + .current_dir(cwd) + .assert() + .success() + .stdout_matches_path("tests/snapshots/add/overwrite_workspace_dep_features.stdout") + .stderr_matches_path("tests/snapshots/add/overwrite_workspace_dep_features.stderr"); + + assert().subset_matches( + "tests/snapshots/add/overwrite_workspace_dep_features.out", + &project_root, + ); +} + #[cargo_test] fn preserve_sorted() { init_registry();