Skip to content

Commit d7d48e4

Browse files
committed
Auto merge of #13402 - epage:add, r=weihanglo
fix: Don't duplicate comments when editing TOML ### What does this PR try to resolve? `toml_edit` <0.22 has a bug that will cause ```toml [lints] rust.unsafe_op_in_unsafe_fn = "deny" rust.explicit_outlives_requirements = "warn" # rust.unused_crate_dependencies = "warn" clippy.cast_lossless = "warn" clippy.doc_markdown = "warn" clippy.exhaustive_enums = "warn" ``` to be written out as ```toml [lints] rust.unsafe_op_in_unsafe_fn = "deny" rust.explicit_outlives_requirements = "warn" # rust.unused_crate_dependencies = "warn" clippy.cast_lossless = "warn" # rust.unused_crate_dependencies = "warn" clippy.doc_markdown = "warn" # rust.unused_crate_dependencies = "warn" clippy.exhaustive_enums = "warn" ``` when it is parsed and then saved. See toml-rs/toml#673 This affects any format-preserving edits we do, including: - `cargo add` - `cargo remove` - `cargo init` / `cargo new` editing the workspace ### How should we test and review this PR? I didn't add any tests as this is covered by `toml_edit`s tests (we already don't cover a fraction of the edit preserving tests it has) ### Additional information
2 parents ccc84cc + 12cf56a commit d7d48e4

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ tar = { version = "0.4.40", default-features = false }
9696
tempfile = "3.9.0"
9797
thiserror = "1.0.56"
9898
time = { version = "0.3", features = ["parsing", "formatting", "serde"] }
99-
toml = "0.8.9"
100-
toml_edit = { version = "0.21.1", features = ["serde"] }
99+
toml = "0.8.10"
100+
toml_edit = { version = "0.22.4", features = ["serde"] }
101101
tracing = "0.1.40" # be compatible with rustc_log: https://github.com/rust-lang/rust/blob/e51e98dde6a/compiler/rustc_log/Cargo.toml#L9
102102
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
103103
unicase = "2.7.0"

src/cargo/util/config/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,11 +1349,14 @@ impl Config {
13491349
let doc: toml_edit::Document = arg.parse().with_context(|| {
13501350
format!("failed to parse value from --config argument `{arg}` as a dotted key expression")
13511351
})?;
1352+
fn non_empty(d: Option<&toml_edit::RawString>) -> bool {
1353+
d.map_or(false, |p| !p.as_str().unwrap_or_default().trim().is_empty())
1354+
}
13521355
fn non_empty_decor(d: &toml_edit::Decor) -> bool {
1353-
d.prefix()
1354-
.map_or(false, |p| !p.as_str().unwrap_or_default().trim().is_empty())
1355-
|| d.suffix()
1356-
.map_or(false, |s| !s.as_str().unwrap_or_default().trim().is_empty())
1356+
non_empty(d.prefix()) || non_empty(d.suffix())
1357+
}
1358+
fn non_empty_key_decor(k: &toml_edit::Key) -> bool {
1359+
non_empty_decor(k.leaf_decor()) || non_empty_decor(k.dotted_decor())
13571360
}
13581361
let ok = {
13591362
let mut got_to_value = false;
@@ -1367,7 +1370,7 @@ impl Config {
13671370
let (k, n) = table.iter().next().expect("len() == 1 above");
13681371
match n {
13691372
Item::Table(nt) => {
1370-
if table.key_decor(k).map_or(false, non_empty_decor)
1373+
if table.key(k).map_or(false, non_empty_key_decor)
13711374
|| non_empty_decor(nt.decor())
13721375
{
13731376
bail!(
@@ -1384,7 +1387,11 @@ impl Config {
13841387
);
13851388
}
13861389
Item::Value(v) => {
1387-
if non_empty_decor(v.decor()) {
1390+
if table
1391+
.key(k)
1392+
.map_or(false, |k| non_empty(k.leaf_decor().prefix()))
1393+
|| non_empty_decor(v.decor())
1394+
{
13881395
bail!(
13891396
"--config argument `{arg}` \
13901397
includes non-whitespace decoration"

0 commit comments

Comments
 (0)