Skip to content

Commit 8f5500b

Browse files
committed
Auto merge of #10705 - Muscraft:workspace-source-fmt-key, r=epage
fix key formatting when switching to a dotted `WorkspaceSource` This fell out of #10697 see [this comment](#10697 (comment)) There was a small issue where changing the source of a `cargo_add::Dependency` to a `WorkspaceSource` would cause the dotted version to have extra space. ```toml dep .workspace = true dep.workspace = true ``` This makes sure the key is formatted as well as adds a unit test to make sure this doesn't come back up in the future. r? `@epage`
2 parents 39ad103 + 706e5b2 commit 8f5500b

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/cargo/ops/cargo_add/dependency.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,7 @@ impl Dependency {
486486
if str_or_1_len_table(item) {
487487
// Nothing to preserve
488488
*item = self.to_toml(crate_root);
489-
if self.source != Some(Source::Workspace(WorkspaceSource)) {
490-
key.fmt();
491-
}
489+
key.fmt();
492490
} else if let Some(table) = item.as_table_like_mut() {
493491
match &self.source {
494492
Some(Source::Registry(src)) => {
@@ -940,6 +938,7 @@ impl Display for WorkspaceSource {
940938
mod tests {
941939
use std::path::Path;
942940

941+
use crate::ops::cargo_add::manifest::LocalManifest;
943942
use cargo_util::paths;
944943

945944
use super::*;
@@ -1123,6 +1122,25 @@ mod tests {
11231122
verify_roundtrip(&crate_root, key, &item);
11241123
}
11251124

1125+
#[test]
1126+
fn overwrite_with_workspace_source_fmt_key() {
1127+
let crate_root =
1128+
paths::normalize_path(&std::env::current_dir().unwrap().join(Path::new("./")));
1129+
let toml = "dep = \"1.0\"\n";
1130+
let manifest = toml.parse().unwrap();
1131+
let mut local = LocalManifest {
1132+
path: crate_root.clone(),
1133+
manifest,
1134+
};
1135+
assert_eq!(local.manifest.to_string(), toml);
1136+
for (key, item) in local.data.clone().iter() {
1137+
let dep = Dependency::from_toml(&crate_root, key, item).unwrap();
1138+
let dep = dep.set_source(WorkspaceSource::new());
1139+
local.insert_into_table(&vec![], &dep).unwrap();
1140+
assert_eq!(local.data.to_string(), "dep.workspace = true\n");
1141+
}
1142+
}
1143+
11261144
#[test]
11271145
#[cfg(windows)]
11281146
fn normalise_windows_style_paths() {

0 commit comments

Comments
 (0)