From 59e8cdd74a88c4c4f68614a52dc5d22c4a5e3365 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 5 Feb 2024 16:50:59 -0600 Subject: [PATCH] fix: Don't duplicate comments when editing TOML `toml_edit` <0.22 has a bug that will cause ```toml [lints] rust.unsafe_op_in_unsafe_fn = "deny" rust.explicit_outlives_requirements = "warn" clippy.cast_lossless = "warn" clippy.doc_markdown = "warn" clippy.exhaustive_enums = "warn" ``` to be written out as [lints] rust.unsafe_op_in_unsafe_fn = "deny" rust.explicit_outlives_requirements = "warn" clippy.cast_lossless = "warn" clippy.doc_markdown = "warn" clippy.exhaustive_enums = "warn" ``` when it is parsed and then saved. See toml-rs/toml#675 This affects any format-preserving edits we do, including: - `cargo add` - `cargo remove` - `cargo init` / `cargo new` editing the workspace --- Cargo.lock | 8 ++++---- Cargo.toml | 4 ++-- src/cargo/util/config/mod.rs | 5 ++++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bc1f4872e9b0..15b863da890f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3360,9 +3360,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "toml" -version = "0.8.9" +version = "0.8.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6a4b9e8023eb94392d3dca65d717c53abc5dad49c07cb65bb8fcd87115fa325" +checksum = "9a9aad4a3066010876e8dcf5a8a06e70a558751117a145c6ce2b82c2e2054290" dependencies = [ "serde", "serde_spanned", @@ -3381,9 +3381,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.21.1" +version = "0.22.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" +checksum = "0c9ffdf896f8daaabf9b66ba8e77ea1ed5ed0f72821b398aba62352e95062951" dependencies = [ "indexmap", "serde", diff --git a/Cargo.toml b/Cargo.toml index 488f53cedc1a..7869c7fcd47c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -96,8 +96,8 @@ tar = { version = "0.4.40", default-features = false } tempfile = "3.9.0" thiserror = "1.0.56" time = { version = "0.3", features = ["parsing", "formatting", "serde"] } -toml = "0.8.9" -toml_edit = { version = "0.21.1", features = ["serde"] } +toml = "0.8.10" +toml_edit = { version = "0.22.4", features = ["serde"] } tracing = "0.1.40" # be compatible with rustc_log: https://github.com/rust-lang/rust/blob/e51e98dde6a/compiler/rustc_log/Cargo.toml#L9 tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } unicase = "2.7.0" diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 901357dbc595..056686cba2ee 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -1355,6 +1355,9 @@ impl Config { || d.suffix() .map_or(false, |s| !s.as_str().unwrap_or_default().trim().is_empty()) } + fn non_empty_key_decor(k: &toml_edit::Key) -> bool { + non_empty_decor(k.leaf_decor()) || non_empty_decor(k.dotted_decor()) + } let ok = { let mut got_to_value = false; let mut table = doc.as_table(); @@ -1367,7 +1370,7 @@ impl Config { let (k, n) = table.iter().next().expect("len() == 1 above"); match n { Item::Table(nt) => { - if table.key_decor(k).map_or(false, non_empty_decor) + if table.key(k).map_or(false, non_empty_key_decor) || non_empty_decor(nt.decor()) { bail!(