cargo-feature-combinations 0.0.50

run cargo commands for all feature combinations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
//! Package-level configuration, feature combination generation, and error types.

use crate::config::Config;
use crate::print_warning;
use crate::{DEFAULT_METADATA_KEY, find_metadata_value, pkg_metadata_section};
use color_eyre::eyre;
use itertools::Itertools;
use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::fmt;

const MAX_FEATURE_COMBINATIONS: u128 = 100_000;

/// Errors that can occur while generating feature combinations.
#[derive(Debug)]
pub enum FeatureCombinationError {
    /// The package declares too many features, which would result in more
    /// combinations than this tool is willing to generate.
    TooManyConfigurations {
        /// Package name from Cargo metadata.
        package: String,
        /// Number of features considered for combination generation.
        num_features: usize,
        /// Total number of configurations implied by `num_features`, if bounded.
        num_configurations: Option<u128>,
        /// Maximum number of configurations allowed before failing.
        limit: u128,
    },
}

impl fmt::Display for FeatureCombinationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::TooManyConfigurations {
                package,
                num_features,
                num_configurations,
                limit,
            } => {
                write!(
                    f,
                    "too many configurations for package `{package}`: {num_features} feature(s) would produce {} combinations (limit: {limit})",
                    num_configurations
                        .map(|v| v.to_string())
                        .unwrap_or_else(|| "an unbounded number of".to_string()),
                )
            }
        }
    }
}

impl std::error::Error for FeatureCombinationError {}

/// Extension trait for [`cargo_metadata::Package`] used by this crate.
pub trait Package {
    /// Parse the configuration for this package if present.
    ///
    /// If the Cargo.toml manifest contains a configuration section,
    /// the latter is parsed.
    /// Otherwise, a default configuration is used.
    ///
    /// # Errors
    ///
    /// If the configuration in the manifest can not be parsed,
    /// an error is returned.
    ///
    fn config(&self) -> eyre::Result<Config>;
    /// Compute all feature combinations for this package based on the
    /// provided [`Config`].
    ///
    /// # Errors
    ///
    /// Returns an error if feature combinations can not be computed, e.g. when
    /// the package declares too many features.
    fn feature_combinations<'a>(&'a self, config: &'a Config)
    -> eyre::Result<Vec<Vec<&'a String>>>;
    /// Convert [`Package::feature_combinations`] into a list of comma-separated
    /// feature strings suitable for passing to `cargo --features`.
    ///
    /// # Errors
    ///
    /// Returns an error if [`Package::feature_combinations`] fails.
    fn feature_matrix(&self, config: &Config) -> eyre::Result<Vec<String>>;
}

impl Package for cargo_metadata::Package {
    fn config(&self) -> eyre::Result<Config> {
        let (mut config, key) = match find_metadata_value(&self.metadata) {
            Some((value, key)) => (serde_json::from_value(value.clone())?, key),
            None => (Config::default(), DEFAULT_METADATA_KEY),
        };

        let section = pkg_metadata_section(key);

        if !config.deprecated.skip_feature_sets.is_empty() {
            print_warning!(
                "{section}.skip_feature_sets in package `{}` is deprecated; use exclude_feature_sets instead",
                self.name,
            );
        }

        if !config.deprecated.denylist.is_empty() {
            print_warning!(
                "{section}.denylist in package `{}` is deprecated; use exclude_features instead",
                self.name,
            );
        }

        if !config.deprecated.exact_combinations.is_empty() {
            print_warning!(
                "{section}.exact_combinations in package `{}` is deprecated; use include_feature_sets instead",
                self.name,
            );
        }

        // Handle deprecated config values
        config
            .exclude_feature_sets
            .append(&mut config.deprecated.skip_feature_sets);
        config
            .exclude_features
            .extend(config.deprecated.denylist.drain());
        config
            .include_feature_sets
            .append(&mut config.deprecated.exact_combinations);

        Ok(config)
    }

    fn feature_combinations<'a>(
        &'a self,
        config: &'a Config,
    ) -> eyre::Result<Vec<Vec<&'a String>>> {
        // Short-circuit: if an explicit allowlist of feature sets is configured,
        // interpret it as the complete matrix.
        //
        // This is intentionally *not* combined with the normal powerset-based
        // generation and its filters: the user is declaring the exact sets they
        // care about (e.g. SSR vs hydrate), and we should not implicitly add
        // `[]` or any other combinations.
        if !config.allow_feature_sets.is_empty() {
            let mut allowed = config
                .allow_feature_sets
                .iter()
                .map(|proposed_allowed_set| {
                    // Normalize to this package by dropping unknown feature
                    // names and switching to references into `self.features`.
                    proposed_allowed_set
                        .iter()
                        .filter_map(|maybe_feature| {
                            self.features.get_key_value(maybe_feature).map(|(k, _v)| k)
                        })
                        .collect::<BTreeSet<_>>()
                })
                .collect::<BTreeSet<_>>();

            if config.no_empty_feature_set {
                // In exact-matrix mode, `[]` is only included if explicitly
                // listed. This option makes it easy to forbid `[]` entirely.
                allowed.retain(|set| !set.is_empty());
            }

            return Ok(allowed
                .into_iter()
                .map(|set| set.into_iter().sorted().collect::<Vec<_>>())
                .sorted()
                .collect::<Vec<_>>());
        }

        // Derive the effective exclude set for this package.
        //
        // When `skip_optional_dependencies` is enabled, extend the configured
        // `exclude_features` with implicit features that correspond to optional
        // dependencies for this package.
        //
        // This mirrors the behaviour in `cargo-all-features`: only the
        // *implicit* features generated by Cargo for optional dependencies are
        // skipped, i.e. features of the form
        //
        //   foo = ["dep:foo"]
        //
        // that are not also referenced via `dep:foo` in any other feature.
        let mut effective_exclude_features = config.exclude_features.clone();

        if config.skip_optional_dependencies {
            let mut implicit_features: HashSet<String> = HashSet::new();
            let mut optional_dep_used_with_dep_syntax_outside: HashSet<String> = HashSet::new();

            // Classify implicit optional-dependency features and track optional
            // dependencies that are referenced via `dep:NAME` in other
            // features, following the logic from cargo-all-features'
            // features_finder.rs.
            for (feature_name, implied) in &self.features {
                for value in implied.iter().filter(|v| v.starts_with("dep:")) {
                    let dep_name = value.trim_start_matches("dep:");
                    if implied.len() == 1 && dep_name == feature_name {
                        // Feature of the shape `foo = ["dep:foo"]`.
                        implicit_features.insert(feature_name.clone());
                    } else {
                        // The dep is used with `dep:` syntax in another
                        // feature, so Cargo will not generate an implicit
                        // feature for it.
                        optional_dep_used_with_dep_syntax_outside.insert(dep_name.to_string());
                    }
                }
            }

            // If the dep is used with `dep:` syntax in another feature, it is
            // not an implicit feature and should not be skipped purely because
            // it is an optional dependency.
            for dep_name in &optional_dep_used_with_dep_syntax_outside {
                implicit_features.remove(dep_name);
            }

            // Extend the effective exclude list with the remaining implicit
            // optional-dependency features.
            effective_exclude_features.extend(implicit_features);
        }

        // Generate the base powerset from
        // - all features
        // - or from isolated sets, minus excluded features
        let base_powerset = if config.isolated_feature_sets.is_empty() {
            generate_global_base_powerset(
                &self.name,
                &self.features,
                &effective_exclude_features,
                &config.include_features,
                &config.only_features,
            )?
        } else {
            generate_isolated_base_powerset(
                &self.name,
                &self.features,
                &config.isolated_feature_sets,
                &effective_exclude_features,
                &config.include_features,
                &config.only_features,
            )?
        };

        // Filter out feature sets that contain skip sets
        let mut filtered_powerset = base_powerset
            .into_iter()
            .filter(|feature_set| {
                !config.exclude_feature_sets.iter().any(|skip_set| {
                    if skip_set.is_empty() {
                        // Special-case: an empty skip set means "exclude only the empty
                        // feature set".
                        //
                        // Without this, the usual "all()" subset test would treat an empty
                        // set as contained in every feature set (vacuously true), and thus
                        // exclude *everything*.
                        feature_set.is_empty()
                    } else {
                        // Remove feature sets containing any of the skip sets
                        skip_set
                            .iter()
                            // Skip set is contained when all its features are contained
                            .all(|skip_feature| feature_set.contains(skip_feature))
                    }
                })
            })
            .collect::<BTreeSet<_>>();

        // Add back exact combinations
        for proposed_exact_combination in &config.include_feature_sets {
            // Remove non-existent features and switch reference to that pointing to `self`
            let exact_combination = proposed_exact_combination
                .iter()
                .filter_map(|maybe_feature| {
                    self.features.get_key_value(maybe_feature).map(|(k, _v)| k)
                })
                .collect::<BTreeSet<_>>();

            // This exact combination may now be empty, but empty combination is always added anyway
            filtered_powerset.insert(exact_combination);
        }

        if config.no_empty_feature_set {
            // When enabled, drop the empty feature set (`[]`) from the final matrix.
            filtered_powerset.retain(|set| !set.is_empty());
        }

        // Re-collect everything into a vector of vectors
        Ok(filtered_powerset
            .into_iter()
            .map(|set| set.into_iter().sorted().collect::<Vec<_>>())
            .sorted()
            .collect::<Vec<_>>())
    }

    fn feature_matrix(&self, config: &Config) -> eyre::Result<Vec<String>> {
        Ok(self
            .feature_combinations(config)?
            .into_iter()
            .map(|features| features.iter().join(","))
            .collect())
    }
}

fn checked_num_combinations(num_features: usize) -> Option<u128> {
    if num_features >= u128::BITS as usize {
        return None;
    }
    let shift: u32 = num_features.try_into().ok()?;
    Some(1u128 << shift)
}

fn ensure_within_combination_limit(
    package_name: &str,
    num_features: usize,
) -> Result<(), FeatureCombinationError> {
    let num_configurations = checked_num_combinations(num_features);
    let exceeds = match num_configurations {
        Some(n) => n > MAX_FEATURE_COMBINATIONS,
        None => true,
    };

    if exceeds {
        return Err(FeatureCombinationError::TooManyConfigurations {
            package: package_name.to_string(),
            num_features,
            num_configurations,
            limit: MAX_FEATURE_COMBINATIONS,
        });
    }

    Ok(())
}

/// Generates the **global** base [powerset](Itertools::powerset) of features.
/// Global features are all features that are defined in the package, except the
/// features from the provided denylist.
///
/// The returned powerset is a two-level [`BTreeSet`], with the strings pointing
/// back to the `package_features`.
fn generate_global_base_powerset<'a>(
    package_name: &str,
    package_features: &'a BTreeMap<String, Vec<String>>,
    exclude_features: &HashSet<String>,
    include_features: &'a HashSet<String>,
    only_features: &HashSet<String>,
) -> Result<BTreeSet<BTreeSet<&'a String>>, FeatureCombinationError> {
    let features = package_features
        .keys()
        .collect::<BTreeSet<_>>()
        .into_iter()
        .filter(|ft| !exclude_features.contains(*ft))
        .filter(|ft| only_features.is_empty() || only_features.contains(*ft))
        .collect::<BTreeSet<_>>();

    ensure_within_combination_limit(package_name, features.len())?;

    Ok(features
        .into_iter()
        .powerset()
        .map(|combination| {
            combination
                .into_iter()
                .chain(include_features)
                .collect::<BTreeSet<&'a String>>()
        })
        .collect())
}

/// Generates the **isolated** base [powerset](Itertools::powerset) of features.
/// Isolated features are features from the provided isolated feature sets,
/// except non-existent features and except the features from the provided
/// denylist.
///
/// The returned powerset is a two-level [`BTreeSet`], with the strings pointing
/// back to the `package_features`.
fn generate_isolated_base_powerset<'a>(
    package_name: &str,
    package_features: &'a BTreeMap<String, Vec<String>>,
    isolated_feature_sets: &[HashSet<String>],
    exclude_features: &HashSet<String>,
    include_features: &'a HashSet<String>,
    only_features: &HashSet<String>,
) -> Result<BTreeSet<BTreeSet<&'a String>>, FeatureCombinationError> {
    // Collect known package features for easy querying
    let known_features = package_features.keys().collect::<HashSet<_>>();

    let mut worst_case_total: u128 = 0;
    for isolated_feature_set in isolated_feature_sets {
        let num_features = isolated_feature_set
            .iter()
            .filter(|ft| known_features.contains(*ft))
            .filter(|ft| !exclude_features.contains(*ft))
            .filter(|ft| only_features.is_empty() || only_features.contains(*ft))
            .count();

        let Some(n) = checked_num_combinations(num_features) else {
            return Err(FeatureCombinationError::TooManyConfigurations {
                package: package_name.to_string(),
                num_features,
                num_configurations: None,
                limit: MAX_FEATURE_COMBINATIONS,
            });
        };

        worst_case_total = worst_case_total.saturating_add(n);
        if worst_case_total > MAX_FEATURE_COMBINATIONS {
            return Err(FeatureCombinationError::TooManyConfigurations {
                package: package_name.to_string(),
                num_features,
                num_configurations: Some(worst_case_total),
                limit: MAX_FEATURE_COMBINATIONS,
            });
        }
    }

    Ok(isolated_feature_sets
        .iter()
        .flat_map(|isolated_feature_set| {
            isolated_feature_set
                .iter()
                .filter(|ft| known_features.contains(*ft)) // remove non-existent features
                .filter(|ft| !exclude_features.contains(*ft)) // remove features from denylist
                .filter(|ft| only_features.is_empty() || only_features.contains(*ft))
                .powerset()
                .map(|combination| {
                    combination
                        .into_iter()
                        .filter_map(|feature| known_features.get(feature).copied())
                        .chain(include_features)
                        .collect::<BTreeSet<_>>()
                })
        })
        .collect())
}

#[cfg(test)]
pub(crate) mod test {
    use super::{FeatureCombinationError, Package};
    use crate::config::Config;
    use color_eyre::eyre;
    use similar_asserts::assert_eq as sim_assert_eq;
    use std::collections::HashSet;

    static INIT: std::sync::Once = std::sync::Once::new();

    fn init() {
        INIT.call_once(|| {
            color_eyre::install().ok();
        });
    }

    pub(crate) fn package_with_features(
        features: &[&str],
    ) -> eyre::Result<cargo_metadata::Package> {
        use cargo_metadata::{PackageBuilder, PackageId, PackageName};
        use semver::Version;
        use std::str::FromStr as _;

        let mut package = PackageBuilder::new(
            PackageName::from_str("test")?,
            Version::parse("0.1.0")?,
            PackageId {
                repr: "test".to_string(),
            },
            "",
        )
        .build()?;
        package.features = features
            .iter()
            .map(|feature| ((*feature).to_string(), vec![]))
            .collect();
        Ok(package)
    }

    #[test]
    fn combinations() -> eyre::Result<()> {
        init();
        let package = package_with_features(&["foo-c", "foo-a", "foo-b"])?;
        let config = Config::default();
        let want = vec![
            vec![],
            vec!["foo-a"],
            vec!["foo-a", "foo-b"],
            vec!["foo-a", "foo-b", "foo-c"],
            vec!["foo-a", "foo-c"],
            vec!["foo-b"],
            vec!["foo-b", "foo-c"],
            vec!["foo-c"],
        ];
        let have = package.feature_combinations(&config)?;

        sim_assert_eq!(have: have, want: want);
        Ok(())
    }

    #[test]
    fn combinations_only_features() -> eyre::Result<()> {
        init();
        let package = package_with_features(&["foo", "bar", "baz"])?;
        let config = Config {
            exclude_features: HashSet::from(["default".to_string()]),
            only_features: HashSet::from(["foo".to_string(), "bar".to_string()]),
            ..Default::default()
        };

        let want = vec![vec![], vec!["bar"], vec!["bar", "foo"], vec!["foo"]];
        let have = package.feature_combinations(&config)?;

        sim_assert_eq!(have: have, want: want);
        Ok(())
    }

    #[test]
    fn combinations_isolated() -> eyre::Result<()> {
        init();
        let package =
            package_with_features(&["foo-a", "foo-b", "bar-b", "bar-a", "car-b", "car-a"])?;
        let config = Config {
            isolated_feature_sets: vec![
                HashSet::from(["foo-a".to_string(), "foo-b".to_string()]),
                HashSet::from(["bar-a".to_string(), "bar-b".to_string()]),
            ],
            ..Default::default()
        };
        let want = vec![
            vec![],
            vec!["bar-a"],
            vec!["bar-a", "bar-b"],
            vec!["bar-b"],
            vec!["foo-a"],
            vec!["foo-a", "foo-b"],
            vec!["foo-b"],
        ];
        let have = package.feature_combinations(&config)?;

        sim_assert_eq!(have: have, want: want);
        Ok(())
    }

    #[test]
    fn combinations_isolated_non_existent() -> eyre::Result<()> {
        init();
        let package =
            package_with_features(&["foo-a", "foo-b", "bar-a", "bar-b", "car-a", "car-b"])?;
        let config = Config {
            isolated_feature_sets: vec![
                HashSet::from(["foo-a".to_string(), "non-existent".to_string()]),
                HashSet::from(["bar-a".to_string(), "bar-b".to_string()]),
            ],
            ..Default::default()
        };
        let want = vec![
            vec![],
            vec!["bar-a"],
            vec!["bar-a", "bar-b"],
            vec!["bar-b"],
            vec!["foo-a"],
        ];
        let have = package.feature_combinations(&config)?;

        sim_assert_eq!(have: have, want: want);
        Ok(())
    }

    #[test]
    fn combinations_isolated_denylist() -> eyre::Result<()> {
        init();
        let package =
            package_with_features(&["foo-a", "foo-b", "bar-b", "bar-a", "car-a", "car-b"])?;
        let config = Config {
            isolated_feature_sets: vec![
                HashSet::from(["foo-a".to_string(), "foo-b".to_string()]),
                HashSet::from(["bar-a".to_string(), "bar-b".to_string()]),
            ],
            exclude_features: HashSet::from(["bar-a".to_string()]),
            ..Default::default()
        };
        let want = vec![
            vec![],
            vec!["bar-b"],
            vec!["foo-a"],
            vec!["foo-a", "foo-b"],
            vec!["foo-b"],
        ];
        let have = package.feature_combinations(&config)?;

        sim_assert_eq!(have: have, want: want);
        Ok(())
    }

    #[test]
    fn combinations_isolated_non_existent_denylist() -> eyre::Result<()> {
        init();
        let package =
            package_with_features(&["foo-b", "foo-a", "bar-a", "bar-b", "car-a", "car-b"])?;
        let config = Config {
            isolated_feature_sets: vec![
                HashSet::from(["foo-a".to_string(), "non-existent".to_string()]),
                HashSet::from(["bar-a".to_string(), "bar-b".to_string()]),
            ],
            exclude_features: HashSet::from(["bar-a".to_string()]),
            ..Default::default()
        };
        let want = vec![vec![], vec!["bar-b"], vec!["foo-a"]];
        let have = package.feature_combinations(&config)?;

        sim_assert_eq!(have: have, want: want);
        Ok(())
    }

    #[test]
    fn combinations_isolated_non_existent_denylist_exact() -> eyre::Result<()> {
        init();
        let package =
            package_with_features(&["foo-a", "foo-b", "bar-a", "bar-b", "car-a", "car-b"])?;
        let config = Config {
            isolated_feature_sets: vec![
                HashSet::from(["foo-a".to_string(), "non-existent".to_string()]),
                HashSet::from(["bar-a".to_string(), "bar-b".to_string()]),
            ],
            exclude_features: HashSet::from(["bar-a".to_string()]),
            include_feature_sets: vec![HashSet::from([
                "car-a".to_string(),
                "bar-a".to_string(),
                "non-existent".to_string(),
            ])],
            ..Default::default()
        };
        let want = vec![vec![], vec!["bar-a", "car-a"], vec!["bar-b"], vec!["foo-a"]];
        let have = package.feature_combinations(&config)?;

        sim_assert_eq!(have: have, want: want);
        Ok(())
    }

    #[test]
    fn combinations_allow_feature_sets_exact() -> eyre::Result<()> {
        init();
        let package = package_with_features(&["hydrate", "ssr", "other"])?;
        let config = Config {
            allow_feature_sets: vec![
                HashSet::from(["ssr".to_string()]),
                HashSet::from(["hydrate".to_string()]),
            ],
            ..Default::default()
        };

        let want = vec![vec!["hydrate"], vec!["ssr"]];
        let have = package.feature_combinations(&config)?;

        sim_assert_eq!(have: have, want: want);
        Ok(())
    }

    #[test]
    fn combinations_allow_feature_sets_ignores_other_options() -> eyre::Result<()> {
        init();
        let package = package_with_features(&["hydrate", "ssr"])?;
        let config = Config {
            allow_feature_sets: vec![HashSet::from(["hydrate".to_string()])],
            exclude_features: HashSet::from(["hydrate".to_string()]),
            exclude_feature_sets: vec![HashSet::from(["hydrate".to_string()])],
            include_feature_sets: vec![HashSet::from(["ssr".to_string()])],
            only_features: HashSet::from(["ssr".to_string()]),
            ..Default::default()
        };

        let want = vec![vec!["hydrate"]];
        let have = package.feature_combinations(&config)?;

        sim_assert_eq!(have: have, want: want);
        Ok(())
    }

    #[test]
    fn combinations_no_empty_feature_set_filters_generated_empty() -> eyre::Result<()> {
        init();
        let package = package_with_features(&["foo", "bar"])?;
        let config = Config {
            no_empty_feature_set: true,
            ..Default::default()
        };

        let want = vec![vec!["bar"], vec!["bar", "foo"], vec!["foo"]];
        let have = package.feature_combinations(&config)?;

        sim_assert_eq!(have: have, want: want);
        Ok(())
    }

    #[test]
    fn combinations_no_empty_feature_set_filters_included_empty() -> eyre::Result<()> {
        init();
        let package = package_with_features(&["foo"])?;
        let config = Config {
            include_feature_sets: vec![HashSet::new()],
            no_empty_feature_set: true,
            ..Default::default()
        };

        let want = vec![vec!["foo"]];
        let have = package.feature_combinations(&config)?;

        sim_assert_eq!(have: have, want: want);
        Ok(())
    }

    #[test]
    fn combinations_exclude_empty_feature_set_only() -> eyre::Result<()> {
        init();
        let package = package_with_features(&["foo", "bar"])?;
        let config = Config {
            exclude_feature_sets: vec![HashSet::new()],
            ..Default::default()
        };

        let want = vec![vec!["bar"], vec!["bar", "foo"], vec!["foo"]];
        let have = package.feature_combinations(&config)?;

        sim_assert_eq!(have: have, want: want);
        Ok(())
    }

    #[test]
    fn too_many_feature_configurations() -> eyre::Result<()> {
        init();
        let features: Vec<String> = (0..25).map(|i| format!("f{i}")).collect();
        let feature_refs: Vec<&str> = features.iter().map(String::as_str).collect();
        let package = package_with_features(&feature_refs)?;

        let config = Config::default();
        let Err(err) = package.feature_combinations(&config) else {
            eyre::bail!("expected too-many-configurations error");
        };
        let Some(err) = err.downcast_ref::<FeatureCombinationError>() else {
            eyre::bail!("expected FeatureCombinationError");
        };
        assert!(
            err.to_string().contains("too many configurations"),
            "expected 'too many configurations' error, got: {err}"
        );
        Ok(())
    }

    /// Build a package whose metadata contains a config under the given alias.
    pub(crate) fn package_with_metadata(
        features: &[&str],
        metadata_key: &str,
        config: &serde_json::Value,
    ) -> eyre::Result<cargo_metadata::Package> {
        let mut package = package_with_features(features)?;
        package.metadata = serde_json::json!({ metadata_key: config });
        Ok(package)
    }

    #[test]
    fn config_from_cargo_fc_alias() -> eyre::Result<()> {
        init();
        let package = package_with_metadata(
            &["foo", "bar"],
            "cargo-fc",
            &serde_json::json!({ "exclude_features": ["foo"] }),
        )?;
        let config = package.config()?;
        assert!(config.exclude_features.contains("foo"));
        assert!(!config.exclude_features.contains("bar"));
        Ok(())
    }

    #[test]
    fn config_from_fc_alias() -> eyre::Result<()> {
        init();
        let package = package_with_metadata(
            &["foo", "bar"],
            "fc",
            &serde_json::json!({ "exclude_features": ["bar"] }),
        )?;
        let config = package.config()?;
        assert!(config.exclude_features.contains("bar"));
        assert!(!config.exclude_features.contains("foo"));
        Ok(())
    }

    #[test]
    fn config_from_feature_combinations_alias() -> eyre::Result<()> {
        init();
        let package = package_with_metadata(
            &["a", "b"],
            "feature-combinations",
            &serde_json::json!({ "no_empty_feature_set": true }),
        )?;
        let config = package.config()?;
        assert!(config.no_empty_feature_set);
        Ok(())
    }

    #[test]
    fn config_from_cargo_feature_combinations_alias() -> eyre::Result<()> {
        init();
        let package = package_with_metadata(
            &["a", "b"],
            "cargo-feature-combinations",
            &serde_json::json!({ "exclude_features": ["a"] }),
        )?;
        let config = package.config()?;
        assert!(config.exclude_features.contains("a"));
        Ok(())
    }

    #[test]
    fn config_default_when_no_metadata() -> eyre::Result<()> {
        init();
        let package = package_with_features(&["foo"])?;
        let config = package.config()?;
        assert!(config.exclude_features.is_empty());
        assert!(!config.no_empty_feature_set);
        Ok(())
    }

    #[test]
    fn config_alias_affects_feature_matrix() -> eyre::Result<()> {
        init();
        let package = package_with_metadata(
            &["foo", "bar"],
            "cargo-fc",
            &serde_json::json!({ "exclude_features": ["foo"] }),
        )?;
        let config = package.config()?;
        let matrix = package.feature_combinations(&config)?;

        // "foo" is excluded, so no combination should contain it
        assert!(
            !matrix.iter().any(|combo| combo.iter().any(|f| *f == "foo")),
            "expected no combination to contain 'foo', got: {matrix:?}"
        );
        // "bar" should still appear
        assert!(
            matrix.iter().any(|combo| combo.iter().any(|f| *f == "bar")),
            "expected 'bar' in at least one combination, got: {matrix:?}"
        );
        Ok(())
    }
}