Browse Source

fix(cli): preserve Cargo manifest formatting when possible (#4431)

Lucas Fernandes Nogueira 3 years ago
parent
commit
6650e5d672
2 changed files with 27 additions and 1 deletions
  1. 6 0
      .changes/keep-manifest-fmt.md
  2. 21 1
      tooling/cli/src/helpers/manifest.rs

+ 6 - 0
.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.

+ 21 - 1
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 {