From 6650e5d6720c215530ca1fdccd19bd2948dd6ca3 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Tue, 21 Jun 2022 20:14:46 -0700 Subject: [PATCH] fix(cli): preserve Cargo manifest formatting when possible (#4431) --- .changes/keep-manifest-fmt.md | 6 ++++++ tooling/cli/src/helpers/manifest.rs | 22 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 .changes/keep-manifest-fmt.md diff --git a/.changes/keep-manifest-fmt.md b/.changes/keep-manifest-fmt.md new file mode 100644 index 000000000..1efa0d389 --- /dev/null +++ b/.changes/keep-manifest-fmt.md @@ -0,0 +1,6 @@ +--- +"cli.rs": patch +"cli.js": patch +--- + +Preserve the `Cargo.toml` formatting when the features array is not changed. diff --git a/tooling/cli/src/helpers/manifest.rs b/tooling/cli/src/helpers/manifest.rs index ad7125475..b29196010 100644 --- a/tooling/cli/src/helpers/manifest.rs +++ b/tooling/cli/src/helpers/manifest.rs @@ -125,7 +125,27 @@ fn write_features( } } } - *manifest_features = Item::Value(Value::Array(toml_array(features))); + if let Some(features_array) = manifest_features.as_array_mut() { + // add features that aren't in the manifest + for feature in features.iter() { + if !features_array.iter().any(|f| f.as_str() == Some(feature)) { + features_array.insert(0, feature.as_str()); + } + } + + // remove features that shouldn't be in the manifest anymore + let mut i = 0; + while i < features_array.len() { + if let Some(f) = features_array.get(i).and_then(|f| f.as_str()) { + if !features.contains(f) { + features_array.remove(i); + } + } + i += 1; + } + } else { + *manifest_features = Item::Value(Value::Array(toml_array(features))); + } Ok(true) } else if let Some(dep) = item.as_value_mut() { match dep {