Skip to content

Commit

Permalink
Auto merge of #11805 - jofas:fix-for-11802, r=weihanglo
Browse files Browse the repository at this point in the history
Breaking endless loop on cyclic features in added dependency in cargo-add

Fixes #11802.

Breaks an endless loop in `cargo add` when features in the added dependency enable each other, i.e. they are cyclic. Followed `@weihanglo's` mentoring advice and AFAICT it works like a charm. I added a test that was stalling before I made the bug fix and afterwards the test executes successfully.
  • Loading branch information
bors committed Mar 6, 2023
2 parents eb5baa9 + feef14e commit 189ac0e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,8 @@ impl DependencyUI {
.get(next)
.into_iter()
.flatten()
.map(|s| s.as_str()),
.map(|s| s.as_str())
.filter(|s| !activated.contains(s)),
);
activated.extend(
self.available_features
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_add/cyclic_features/in
25 changes: 25 additions & 0 deletions tests/testsuite/cargo_add/cyclic_features/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::prelude::*;
use cargo_test_support::Project;

use crate::cargo_add::init_registry;
use cargo_test_support::curr_dir;

#[cargo_test]
fn case() {
init_registry();
let project = Project::from_template(curr_dir!().join("in"));
let project_root = project.root();
let cwd = &project_root;

snapbox::cmd::Command::cargo_ui()
.arg("add")
.arg_line("test_cyclic_features")
.current_dir(cwd)
.assert()
.success()
.stdout_matches_path(curr_dir!().join("stdout.log"))
.stderr_matches_path(curr_dir!().join("stderr.log"));

assert_ui().subset_matches(curr_dir!().join("out"), &project_root);
}
8 changes: 8 additions & 0 deletions tests/testsuite/cargo_add/cyclic_features/out/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[workspace]

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

[dependencies]
test_cyclic_features = "0.1.1"
5 changes: 5 additions & 0 deletions tests/testsuite/cargo_add/cyclic_features/stderr.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Updating `dummy-registry` index
Adding test_cyclic_features v0.1.1 to dependencies.
Features:
+ feature-one
+ feature-two
Empty file.
7 changes: 7 additions & 0 deletions tests/testsuite/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod add_normalized_name_external;
mod build;
mod build_prefer_existing_version;
mod change_rename_target;
mod cyclic_features;
mod default_features;
mod deprecated_default_features;
mod deprecated_section;
Expand Down Expand Up @@ -165,6 +166,12 @@ fn add_registry_packages(alt: bool) {
cargo_test_support::registry::Package::new("test_nonbreaking", "0.1.1")
.alternative(alt)
.publish();
cargo_test_support::registry::Package::new("test_cyclic_features", "0.1.1")
.alternative(alt)
.feature("default", &["feature-one", "feature-two"])
.feature("feature-one", &["feature-two"])
.feature("feature-two", &["feature-one"])
.publish();

// Normalization
cargo_test_support::registry::Package::new("linked-hash-map", "0.5.4")
Expand Down

0 comments on commit 189ac0e

Please sign in to comment.