Skip to content

Commit 6650e5d

Browse files
authored
fix(cli): preserve Cargo manifest formatting when possible (#4431)
1 parent 672174b commit 6650e5d

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

.changes/keep-manifest-fmt.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli.rs": patch
3+
"cli.js": patch
4+
---
5+
6+
Preserve the `Cargo.toml` formatting when the features array is not changed.

tooling/cli/src/helpers/manifest.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,27 @@ fn write_features(
125125
}
126126
}
127127
}
128-
*manifest_features = Item::Value(Value::Array(toml_array(features)));
128+
if let Some(features_array) = manifest_features.as_array_mut() {
129+
// add features that aren't in the manifest
130+
for feature in features.iter() {
131+
if !features_array.iter().any(|f| f.as_str() == Some(feature)) {
132+
features_array.insert(0, feature.as_str());
133+
}
134+
}
135+
136+
// remove features that shouldn't be in the manifest anymore
137+
let mut i = 0;
138+
while i < features_array.len() {
139+
if let Some(f) = features_array.get(i).and_then(|f| f.as_str()) {
140+
if !features.contains(f) {
141+
features_array.remove(i);
142+
}
143+
}
144+
i += 1;
145+
}
146+
} else {
147+
*manifest_features = Item::Value(Value::Array(toml_array(features)));
148+
}
129149
Ok(true)
130150
} else if let Some(dep) = item.as_value_mut() {
131151
match dep {

0 commit comments

Comments
 (0)