Skip to content

Commit

Permalink
fix(add): Preserve comments when updating simple deps
Browse files Browse the repository at this point in the history
A case the tests showed but isn't covered here is when a `[features]`
table is created, the dependencies-end comment gets attached to that,
e.g. see cargo_add/overwrite_optional

Fixes #13645
  • Loading branch information
epage committed Mar 26, 2024
1 parent 57944a7 commit f72d73b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 11 deletions.
15 changes: 12 additions & 3 deletions src/cargo/util/toml_mut/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,18 @@ impl Dependency {
item: &mut toml_edit::Item,
) {
if str_or_1_len_table(item) {
// Nothing to preserve
*item = self.to_toml(crate_root);
key.fmt();
// Little to preserve
let mut new_item = self.to_toml(crate_root);
match (&item, &mut new_item) {
(toml_edit::Item::Value(old), toml_edit::Item::Value(new)) => {
*new.decor_mut() = old.decor().clone();
}
(toml_edit::Item::Table(old), toml_edit::Item::Table(new)) => {
*new.decor_mut() = old.decor().clone();
}
(_, _) => {}
}
*item = new_item;
} else if let Some(table) = item.as_table_like_mut() {
match &self.source {
Some(Source::Registry(src)) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ version = "0.0.0"
edition = "2015"

[dependencies]
my-package1 = "99999.0.0"
my-package2 = "0.4.1"
# Before my-package1
my-package1 = "99999.0.0" # After my-package1
# Before my-package2
my-package2 = "0.4.1" # After my-package2
# End
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ version = "0.0.0"
edition = "2015"

[dependencies]
my-package1 = { version = "99999.0.0", default-features = false }
my-package2 = { version = "0.4.1", default-features = false }
# Before my-package1
my-package1 = { version = "99999.0.0", default-features = false } # After my-package1
# Before my-package2
my-package2 = { version = "0.4.1", default-features = false } # After my-package2
# End
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ version = "0.0.0"
edition = "2015"

[dependencies]
my-package = "0.1.0"
# Before my-package
my-package = "0.1.0" # After my-package
# End
3 changes: 2 additions & 1 deletion tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ version = "0.0.0"
edition = "2015"

[dependencies]
my-package = "0.1.0"
# Before my-package
my-package = "0.1.0" # After my-package
# End
3 changes: 2 additions & 1 deletion tests/testsuite/cargo_add/overwrite_optional/out/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ version = "0.0.0"
edition = "2015"

[dependencies]
my-package = { version = "0.1.0", optional = true }
# Before my-package
my-package = { version = "0.1.0", optional = true } # After my-package

[features]
my-package = ["dep:my-package"]
Expand Down
3 changes: 2 additions & 1 deletion tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ version = "0.0.0"
edition = "2015"

[dependencies]
my-package = { version = "0.1.0", public = true }
# Before my-package
my-package = { version = "0.1.0", public = true } # After my-package
# End

0 comments on commit f72d73b

Please sign in to comment.