Skip to content

Commit aa7d116

Browse files
Muscraftepage
authored andcommitted
Support overwriting a foo.workspace = true with a dependency from a different source
1 parent cf2202c commit aa7d116

File tree

23 files changed

+158
-3
lines changed

23 files changed

+158
-3
lines changed

src/cargo/ops/cargo_add/dependency.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fmt::{Display, Formatter};
33
use std::path::{Path, PathBuf};
44

55
use indexmap::IndexSet;
6+
use toml_edit::KeyMut;
67

78
use super::manifest::str_or_1_len_table;
89
use crate::core::FeatureMap;
@@ -476,10 +477,18 @@ impl Dependency {
476477
}
477478

478479
/// Modify existing entry to match this dependency
479-
pub fn update_toml(&self, crate_root: &Path, item: &mut toml_edit::Item) {
480+
pub fn update_toml<'k>(
481+
&self,
482+
crate_root: &Path,
483+
key: &mut KeyMut<'k>,
484+
item: &mut toml_edit::Item,
485+
) {
480486
if str_or_1_len_table(item) {
481487
// Nothing to preserve
482488
*item = self.to_toml(crate_root);
489+
if self.source != Some(Source::Workspace(WorkspaceSource)) {
490+
key.fmt();
491+
}
483492
} else if let Some(table) = item.as_table_like_mut() {
484493
match &self.source {
485494
Some(Source::Registry(src)) => {

src/cargo/ops/cargo_add/manifest.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,12 @@ impl LocalManifest {
349349
let dep_key = dep.toml_key();
350350

351351
let table = self.get_table_mut(table_path)?;
352-
if let Some(dep_item) = table.as_table_like_mut().unwrap().get_mut(dep_key) {
353-
dep.update_toml(&crate_root, dep_item);
352+
if let Some((mut dep_key, dep_item)) = table
353+
.as_table_like_mut()
354+
.unwrap()
355+
.get_key_value_mut(dep_key)
356+
{
357+
dep.update_toml(&crate_root, &mut dep_key, dep_item);
354358
} else {
355359
let new_dependency = dep.to_toml(&crate_root);
356360
table[dep_key] = new_dependency;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[workspace]
2+
members = ["primary", "dependency"]
3+
4+
[workspace.dependencies]
5+
foo = { version = "0.0.0", path = "./dependency" }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[package]
2+
name = "foo"
3+
version = "0.0.0"

tests/snapshots/add/overwrite_workspace_dep.in/dependency/src/lib.rs

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cargo-features = ["workspace-inheritance"]
2+
3+
[package]
4+
name = "bar"
5+
version = "0.0.0"
6+
7+
[dependencies]
8+
foo.workspace = true

tests/snapshots/add/overwrite_workspace_dep.in/primary/src/lib.rs

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[workspace]
2+
members = ["primary", "dependency"]
3+
4+
[workspace.dependencies]
5+
foo = { version = "0.0.0", path = "./dependency" }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[package]
2+
name = "foo"
3+
version = "0.0.0"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cargo-features = ["workspace-inheritance"]
2+
3+
[package]
4+
name = "bar"
5+
version = "0.0.0"
6+
7+
[dependencies]
8+
foo = { version = "0.0.0", path = "../dependency" }

0 commit comments

Comments
 (0)