Skip to content

Commit 8f49b55

Browse files
committed
Auto merge of #12191 - epage:mut, r=weihanglo
fix(add): Reduce the chance we re-format the user's `[features]` table ### What does this PR try to resolve? #11743 pointed out that we re-format the users `[features]` table when running `cargo add` which was a bug introduced in #11099. This reduces the chance people will run into this problem - Reducing the scope of the `fmt` call - Preserving formatting in a simple case Actually removing the `fmt` case can make some common formatting cases more complex to do "right", so I'm punting on that for now. ### How should we test and review this PR? Look at the individual commits as I show how each change improves the behavior of `cargo add`.
2 parents 0425b11 + dbf9134 commit 8f49b55

File tree

8 files changed

+89
-3
lines changed

8 files changed

+89
-3
lines changed

src/cargo/util/toml_mut/manifest.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,11 @@ fn fix_feature_activations(
496496
for idx in remove_list.iter().rev() {
497497
feature_values.remove(*idx);
498498
}
499+
if !remove_list.is_empty() {
500+
// HACK: Instead of cleaning up the users formatting from having removed a feature, we just
501+
// re-format the whole feature list
502+
feature_values.fmt();
503+
}
499504

500505
if status == DependencyStatus::Required {
501506
for value in feature_values.iter_mut() {
@@ -511,13 +516,13 @@ fn fix_feature_activations(
511516
} = parsed_value
512517
{
513518
if dep_name == dep_key && weak {
514-
*value = format!("{dep_name}/{dep_feature}").into();
519+
let mut new_value = toml_edit::Value::from(format!("{dep_name}/{dep_feature}"));
520+
*new_value.decor_mut() = value.decor().clone();
521+
*value = new_value;
515522
}
516523
}
517524
}
518525
}
519-
520-
feature_values.fmt();
521526
}
522527

523528
pub fn str_or_1_len_table(item: &toml_edit::Item) -> bool {

tests/testsuite/cargo_add/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ mod path_dev;
9595
mod path_inferred_name;
9696
mod path_inferred_name_conflicts_full_feature;
9797
mod path_normalized_name;
98+
mod preserve_features_table;
9899
mod preserve_sorted;
99100
mod preserve_unsorted;
100101
mod quiet;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "xxx"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
your-face = { version = "99999.0.0", optional = true }
10+
11+
[features]
12+
default = [
13+
"a",
14+
"b",
15+
"c",
16+
]
17+
a = [
18+
"your-face?/nose", # but not the mouth and nose
19+
]
20+
b = []
21+
c = []

tests/testsuite/cargo_add/preserve_features_table/in/src/lib.rs

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use cargo_test_support::compare::assert_ui;
2+
use cargo_test_support::prelude::*;
3+
use cargo_test_support::Project;
4+
5+
use cargo_test_support::curr_dir;
6+
7+
#[cargo_test]
8+
fn case() {
9+
cargo_test_support::registry::init();
10+
cargo_test_support::registry::Package::new("your-face", "99999.0.0+my-package")
11+
.feature("nose", &[])
12+
.feature("mouth", &[])
13+
.feature("eyes", &[])
14+
.feature("ears", &[])
15+
.publish();
16+
17+
let project = Project::from_template(curr_dir!().join("in"));
18+
let project_root = project.root();
19+
let cwd = &project_root;
20+
21+
snapbox::cmd::Command::cargo_ui()
22+
.arg("add")
23+
.arg_line("your-face --no-optional")
24+
.current_dir(cwd)
25+
.assert()
26+
.success()
27+
.stdout_matches_path(curr_dir!().join("stdout.log"))
28+
.stderr_matches_path(curr_dir!().join("stderr.log"));
29+
30+
assert_ui().subset_matches(curr_dir!().join("out"), &project_root);
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "xxx"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
your-face = { version = "99999.0.0" }
10+
11+
[features]
12+
default = [
13+
"a",
14+
"b",
15+
"c",
16+
]
17+
a = [
18+
"your-face/nose", # but not the mouth and nose
19+
]
20+
b = []
21+
c = []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Updating `dummy-registry` index
2+
Adding your-face v99999.0.0 to dependencies.
3+
Features:
4+
- ears
5+
- eyes
6+
- mouth
7+
- nose

tests/testsuite/cargo_add/preserve_features_table/stdout.log

Whitespace-only changes.

0 commit comments

Comments
 (0)