Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintain sorting of dependency features #13682

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/cargo/util/toml_mut/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ use std::fmt::{Display, Formatter};
use std::path::{Path, PathBuf};

use indexmap::IndexSet;
use itertools::Itertools;
use toml_edit::KeyMut;

use super::manifest::str_or_1_len_table;
use crate::core::GitReference;
use crate::core::SourceId;
use crate::core::Summary;
use crate::util::toml_mut::is_sorted;
use crate::CargoResult;
use crate::GlobalContext;

Expand Down Expand Up @@ -588,8 +590,13 @@ impl Dependency {
.collect::<Option<IndexSet<_>>>()
})
.unwrap_or_default();
let is_already_sorted = is_sorted(features.iter());
features.extend(new_features.iter().map(|s| s.as_str()));
let features = features.into_iter().collect::<toml_edit::Value>();
let features = if is_already_sorted {
features.into_iter().sorted().collect::<toml_edit::Value>()
} else {
features.into_iter().collect::<toml_edit::Value>()
};
table.set_dotted(false);
overwrite_value(table, "features", features);
} else {
Expand Down
2 changes: 2 additions & 0 deletions tests/testsuite/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ mod path_inferred_name;
mod path_inferred_name_conflicts_full_feature;
mod path_normalized_name;
mod preserve_dep_std_table;
mod preserve_features_sorted;
mod preserve_features_table;
mod preserve_features_unsorted;
mod preserve_sorted;
mod preserve_unsorted;
mod public;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ edition = "2015"
[dependencies]
unrelateed-crate = "99999.0.0"
# Before your-face
your-face = { version = "99999.0.0", features = ["eyes", "nose", "mouth", "ears"] } # After your-face
your-face = { version = "99999.0.0", features = ["ears", "eyes", "mouth", "nose"] } # After your-face
# End
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[workspace]

[package]
name = "cargo-list-test-fixture"
version = "0.0.0"
edition = "2015"

[dependencies]
my-package = { version = "99999.0.0", features = ["a", "b", "c", "e"] }
Empty file.
33 changes: 33 additions & 0 deletions tests/testsuite/cargo_add/preserve_features_sorted/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::Project;

#[cargo_test]
fn case() {
cargo_test_support::registry::init();
cargo_test_support::registry::Package::new("my-package", "99999.0.0+my-package")
.feature("a", &[])
.feature("b", &[])
.feature("c", &[])
.feature("d", &[])
.feature("e", &[])
.publish();

let project = Project::from_template(current_dir!().join("in"));
let project_root = project.root();
let cwd = &project_root;

snapbox::cmd::Command::cargo_ui()
.arg("add")
.arg_line("my-package -F d")
.current_dir(cwd)
.assert()
.success()
.stdout_matches(str![""])
.stderr_matches(file!["stderr.term.svg"]);

assert_ui().subset_matches(current_dir!().join("out"), &project_root);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[workspace]

[package]
name = "cargo-list-test-fixture"
version = "0.0.0"
edition = "2015"

[dependencies]
my-package = { version = "99999.0.0", features = ["a", "b", "c", "d", "e"] }
43 changes: 43 additions & 0 deletions tests/testsuite/cargo_add/preserve_features_sorted/stderr.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[workspace]

[package]
name = "cargo-list-test-fixture"
version = "0.0.0"
edition = "2015"

[dependencies]
my-package = { version = "99999.0.0", features = ["b", "a", "d", "c"] }
Empty file.
33 changes: 33 additions & 0 deletions tests/testsuite/cargo_add/preserve_features_unsorted/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::Project;

#[cargo_test]
fn case() {
cargo_test_support::registry::init();
cargo_test_support::registry::Package::new("my-package", "99999.0.0+my-package")
.feature("a", &[])
.feature("b", &[])
.feature("c", &[])
.feature("d", &[])
.feature("e", &[])
.publish();

let project = Project::from_template(current_dir!().join("in"));
let project_root = project.root();
let cwd = &project_root;

snapbox::cmd::Command::cargo_ui()
.arg("add")
.arg_line("my-package -F e")
.current_dir(cwd)
.assert()
.success()
.stdout_matches(str![""])
.stderr_matches(file!["stderr.term.svg"]);

assert_ui().subset_matches(current_dir!().join("out"), &project_root);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[workspace]

[package]
name = "cargo-list-test-fixture"
version = "0.0.0"
edition = "2015"

[dependencies]
my-package = { version = "99999.0.0", features = ["b", "a", "d", "c", "e"] }
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.