cabinpkg-workspace 0.14.0

Workspace and package-graph loader for Cabin
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
//! `WorkspacePackage` selection across a [`PackageGraph`].
//!
//! `cabin` translates user flags (`--workspace`, `--package`,
//! `--exclude`, `--default-members`) into a [`PackageSelection`]
//! and hands it to [`resolve_package_selection`], which validates
//! the request against the graph and returns the deterministic
//! ordered list of selected primary-package indices. Centralizing
//! this here keeps CLI code free of workspace-graph algorithms.

use std::collections::{BTreeMap, BTreeSet};

use crate::error::WorkspaceError;
use crate::graph::PackageGraph;

/// Selection mode the user requested.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SelectionMode {
    /// Default behavior:
    ///
    /// - inside a single-package package (no `[workspace]`), select
    ///   the root package;
    /// - at a workspace root, select `[workspace.default-members]`
    ///   when present, otherwise fall back to **all** workspace
    ///   members. The fallback rule is documented in
    ///   [`docs/workspaces.md`](../../../docs/workspaces.md).
    CurrentPackage,
    /// `--default-members`. Errors when the workspace declares no
    /// `[workspace.default-members]`.
    DefaultMembers,
    /// `--workspace`. Selects every workspace member, then applies
    /// `--exclude` filtering.
    WholeWorkspace,
    /// `-p` / `--package`. Selects exactly the named packages (each
    /// must be a workspace member).
    ExplicitPackages(Vec<String>),
}

/// User-facing selection request, before validation against any
/// concrete graph.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PackageSelection {
    pub mode: SelectionMode,
    /// Packages to drop from the resolved selection. Only valid in
    /// combination with `WholeWorkspace` and `DefaultMembers`.
    pub exclude: Vec<String>,
}

impl PackageSelection {
    pub fn current_package() -> Self {
        Self {
            mode: SelectionMode::CurrentPackage,
            exclude: Vec::new(),
        }
    }
}

/// Final, validated selection. Indices are into [`PackageGraph::packages`]
/// and are ordered to match the graph's primary-package ordering.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedSelection {
    pub packages: Vec<usize>,
}

impl ResolvedSelection {
    /// Closure of the selection over local
    /// path-dependency edges. Includes every package reachable from
    /// `self.packages` by walking `WorkspacePackage::deps` transitively, in
    /// deterministic ascending-index order. Workspace siblings that
    /// the selection neither names nor pulls in via path deps are
    /// **not** in the closure — that is the whole point of this
    /// helper.
    pub fn closure(&self, graph: &PackageGraph) -> BTreeSet<usize> {
        let mut closure: BTreeSet<usize> = BTreeSet::new();
        let mut stack: Vec<usize> = self.packages.clone();
        while let Some(idx) = stack.pop() {
            if !closure.insert(idx) {
                continue;
            }
            for edge in &graph.packages[idx].deps {
                if !closure.contains(&edge.index) {
                    stack.push(edge.index);
                }
            }
        }
        closure
    }

    /// Names of every package in the selection's path-dependency
    /// [`closure`](Self::closure), in deterministic order. Convenience
    /// over `closure(graph)` for the common case where a caller needs a
    /// set of package *names* — e.g. to seed a strict registry / port
    /// policy — rather than graph indices.
    pub fn closure_package_names(&self, graph: &PackageGraph) -> BTreeSet<String> {
        self.closure(graph)
            .into_iter()
            .map(|i| graph.packages[i].package.name.as_str().to_owned())
            .collect()
    }
}

/// Validate a [`PackageSelection`] against `graph` and return the
/// concrete list of selected primary-package indices. Errors are
/// emitted with deterministic, actionable messages so the user can
/// fix typos quickly.
///
/// # Errors
/// Returns a [`WorkspaceError`] when the selection is invalid:
/// [`WorkspaceError::ExcludeWithoutWorkspaceSelection`] for
/// `--exclude` outside a workspace selection,
/// [`WorkspaceError::DefaultMembersWithoutWorkspace`] or
/// [`WorkspaceError::DefaultMemberNotInMembers`] for default-member
/// modes that don't apply, [`WorkspaceError::PackageNotInWorkspace`]
/// for an unknown or non-primary named/excluded package, and
/// [`WorkspaceError::AmbiguousPackageSelection`] when the selection
/// resolves to no packages.
pub fn resolve_package_selection(
    graph: &PackageGraph,
    selection: &PackageSelection,
) -> Result<ResolvedSelection, WorkspaceError> {
    // `--exclude` requires an explicit
    // `--workspace` or `--default-members` mode. The
    // implicit-default `CurrentPackage` mode no longer accepts
    // `--exclude`: the user must opt into a multi-package
    // selection before excluding from it. Stricter behavior
    // matches Cargo and stops `cabin <cmd> --exclude foo` from
    // silently doing the wrong thing on a single-package package.
    let exclusion_compatible = matches!(
        selection.mode,
        SelectionMode::WholeWorkspace | SelectionMode::DefaultMembers
    );
    if !selection.exclude.is_empty() && !exclusion_compatible {
        return Err(WorkspaceError::ExcludeWithoutWorkspaceSelection);
    }

    let exclude_indices = exclude_indices(graph, &selection.exclude)?;

    let candidates: Vec<usize> = match &selection.mode {
        SelectionMode::CurrentPackage => current_package_default(graph),
        SelectionMode::DefaultMembers => {
            if !graph.is_workspace_root {
                return Err(WorkspaceError::DefaultMembersWithoutWorkspace);
            }
            if graph.default_members.is_empty() {
                return Err(WorkspaceError::DefaultMemberNotInMembers {
                    member: "<no default-members declared>".to_owned(),
                });
            }
            graph.default_members.clone()
        }
        SelectionMode::WholeWorkspace => {
            if graph.is_workspace_root {
                graph.primary_packages.clone()
            } else {
                // `--workspace` against a single-package package
                // simply selects that package — keeps CI users from
                // having to special-case a non-workspace tree.
                current_package_default(graph)
            }
        }
        SelectionMode::ExplicitPackages(names) => {
            let mut out = Vec::with_capacity(names.len());
            for name in names {
                let idx =
                    graph
                        .index_of(name)
                        .ok_or_else(|| WorkspaceError::PackageNotInWorkspace {
                            name: name.clone(),
                            members: workspace_member_names(graph),
                        })?;
                if !graph.primary_packages.contains(&idx) {
                    return Err(WorkspaceError::PackageNotInWorkspace {
                        name: name.clone(),
                        members: workspace_member_names(graph),
                    });
                }
                if !out.contains(&idx) {
                    out.push(idx);
                }
            }
            out
        }
    };

    let mut packages: Vec<usize> = candidates
        .into_iter()
        .filter(|i| !exclude_indices.contains(i))
        .collect();
    // Stable, deterministic ordering: by package name.
    packages.sort_by(|a, b| {
        graph.packages[*a]
            .package
            .name
            .as_str()
            .cmp(graph.packages[*b].package.name.as_str())
    });
    if packages.is_empty() {
        return Err(WorkspaceError::AmbiguousPackageSelection);
    }
    Ok(ResolvedSelection { packages })
}

fn current_package_default(graph: &PackageGraph) -> Vec<usize> {
    if graph.is_workspace_root {
        if graph.default_members.is_empty() {
            // Documented fallback: all workspace members
            // when default-members is absent.
            graph.primary_packages.clone()
        } else {
            graph.default_members.clone()
        }
    } else if let Some(root) = graph.root_package {
        vec![root]
    } else {
        graph.primary_packages.clone()
    }
}

fn exclude_indices(
    graph: &PackageGraph,
    excludes: &[String],
) -> Result<BTreeSet<usize>, WorkspaceError> {
    let mut out = BTreeSet::new();
    for name in excludes {
        let idx = graph
            .index_of(name)
            .ok_or_else(|| WorkspaceError::PackageNotInWorkspace {
                name: name.clone(),
                members: workspace_member_names(graph),
            })?;
        if !graph.primary_packages.contains(&idx) {
            return Err(WorkspaceError::PackageNotInWorkspace {
                name: name.clone(),
                members: workspace_member_names(graph),
            });
        }
        out.insert(idx);
    }
    Ok(out)
}

/// Combine several version-requirement strings into one
/// [`semver::VersionReq`] by joining them with `, ` (the comma form
/// semver reads as an AND of comparators) and re-parsing. On a parse
/// failure the joined string is returned alongside the error so each
/// caller can build its own diagnostic. This is the single
/// join-on-collision kernel shared by the closure and patch
/// requirement aggregators and the CLI's root-dep merge.
///
/// # Errors
/// Returns `Err((joined, source))` — the comma-joined requirement
/// string paired with the [`semver::Error`] — when the joined form
/// is not a valid [`semver::VersionReq`] (the requirements are
/// mutually incompatible).
pub fn combine_version_reqs(
    reqs: &[String],
) -> Result<semver::VersionReq, (String, semver::Error)> {
    let joined = reqs.join(", ");
    match semver::VersionReq::parse(&joined) {
        Ok(req) => Ok(req),
        Err(source) => Err((joined, source)),
    }
}

/// The per-dependency eligibility predicate shared by the versioned-dep
/// aggregators. Returns the [`semver::VersionReq`] when `dep` is an active
/// registry-versioned dependency for this invocation — right kind (normal
/// kinds, plus `Dev` when `dev_active_here`), matches the host platform,
/// optional only if enabled, and not excluded — otherwise `None`. `idx` is
/// the declaring package's closure index, threaded so the optional gate can
/// consult `is_optional_dep_enabled`.
fn versioned_dep_active<'a, F>(
    dep: &'a cabin_core::Dependency,
    idx: usize,
    dev_active_here: bool,
    host: &cabin_core::TargetPlatform,
    is_optional_dep_enabled: &F,
    excluded_names: &BTreeSet<String>,
) -> Option<&'a semver::VersionReq>
where
    F: Fn(usize, &str) -> bool,
{
    use cabin_core::{DependencyKind, DependencySource};
    let kind_active =
        dep.kind.is_resolved_by_default() || (dev_active_here && dep.kind == DependencyKind::Dev);
    if !kind_active {
        return None;
    }
    if !dep.matches_platform(host) {
        return None;
    }
    if dep.optional && !is_optional_dep_enabled(idx, dep.name.as_str()) {
        return None;
    }
    if excluded_names.contains(dep.name.as_str()) {
        return None;
    }
    match &dep.source {
        DependencySource::Version(req) => Some(req),
        _ => None,
    }
}

/// Enumerate the versioned dependencies that drive
/// resolve / fetch / update for a selected package set. Walks the
/// closure (selection + transitive local path deps) so a registry
/// dep declared by a path-dep `lib` is visible when the user
/// selected `app`.
///
/// Only dependency kinds that participate in ordinary resolution
/// (`Normal`, `Build`, `Tool`) are included. Dev dependencies are
/// excluded so a workspace member's dev-only requirement cannot
/// break an ordinary `cabin build` / `cabin fetch`. System
/// dependencies never reach this path because they are never
/// stored as `DependencySource::Version`.
///
/// Optional dependencies are filtered using `is_optional_dep_enabled`:
/// the closure is `(declaring_package_index, dep_name) -> included`.
/// Pass `|_, _| false` to include only non-optional deps; pass a
/// closure backed by a feature resolution to include only optional
/// deps the user asked for.
///
/// Conflicting requirements for the same name (across different
/// packages or kinds) are joined with `, ` — a form
/// `semver::VersionReq` accepts — and re-parsed; truly
/// incompatible requirements surface as a clear parse error
/// rather than silent unification.
///
/// `excluded_names` drops every dependency name in the set —
/// typically used by the artifact pipeline to skip patched
/// packages that ship from a local working copy and never need
/// to be fetched from the index.
///
/// `dev_active_for` opts in `[dev-dependencies]` for the named
/// packages (typically the `cabin test` selection). Dev deps for
/// packages not in this set stay declaration-only, matching the
/// `cabin build` policy.
///
/// # Errors
/// Returns [`WorkspaceError::IncompatibleWorkspaceRequirements`]
/// when the requirements collected for a single dependency name
/// cannot be combined into one [`semver::VersionReq`] (the joined
/// requirement string fails to parse).
///
/// # Panics
/// Panics only if the name-lookup invariant were violated: every
/// dependency name pushed into `combined` is inserted into
/// `name_lookup` in the same loop iteration, so the `.unwrap()` on
/// `name_lookup.remove(&name)` always finds the key.
pub fn collect_closure_versioned_deps_excluding_with_dev<F>(
    graph: &PackageGraph,
    closure: &BTreeSet<usize>,
    is_optional_dep_enabled: F,
    excluded_names: &BTreeSet<String>,
    dev_active_for: &BTreeSet<String>,
) -> Result<BTreeMap<cabin_core::PackageName, semver::VersionReq>, WorkspaceError>
where
    F: Fn(usize, &str) -> bool,
{
    // Conditional dependencies are evaluated against the host
    // platform — Cabin does not yet support cross-compilation.
    let host_platform = cabin_core::TargetPlatform::current();
    let mut combined: BTreeMap<String, Vec<String>> = BTreeMap::new();
    let mut name_lookup: BTreeMap<String, cabin_core::PackageName> = BTreeMap::new();
    for &idx in closure {
        let pkg = &graph.packages[idx];
        // Skip registry packages — their declared deps are already
        // covered by the registry's own metadata, not by the
        // workspace user's manifests.
        if !matches!(pkg.kind, crate::graph::PackageKind::Local) {
            continue;
        }
        let dev_active_here = dev_active_for.contains(pkg.package.name.as_str());
        for dep in &pkg.package.dependencies {
            if let Some(req) = versioned_dep_active(
                dep,
                idx,
                dev_active_here,
                &host_platform,
                &is_optional_dep_enabled,
                excluded_names,
            ) {
                let key = dep.name.as_str().to_owned();
                combined
                    .entry(key.clone())
                    .or_default()
                    .push(req.to_string());
                name_lookup.insert(key, dep.name.clone());
            }
        }
    }
    let mut out = BTreeMap::new();
    for (name, mut reqs) in combined {
        reqs.sort();
        reqs.dedup();
        let parsed = combine_version_reqs(&reqs).map_err(|(requirements, source)| {
            WorkspaceError::IncompatibleWorkspaceRequirements {
                name: name.clone(),
                requirements,
                source,
            }
        })?;
        out.insert(name_lookup.remove(&name).unwrap(), parsed);
    }
    Ok(out)
}

/// Whether the supplied closure carries any versioned
/// (registry-bound) dependency that the artifact pipeline would
/// need to fetch. Mirrors
/// [`collect_closure_versioned_deps_excluding_with_dev`] but
/// returns a `bool` so the CLI can short-circuit before opening
/// an index.
///
/// `dev_active_for` follows the same opt-in policy as
/// [`collect_closure_versioned_deps_excluding_with_dev`].
pub fn closure_has_versioned_deps_excluding_with_dev<F>(
    graph: &PackageGraph,
    closure: &BTreeSet<usize>,
    is_optional_dep_enabled: F,
    excluded_names: &BTreeSet<String>,
    dev_active_for: &BTreeSet<String>,
) -> bool
where
    F: Fn(usize, &str) -> bool,
{
    let host_platform = cabin_core::TargetPlatform::current();
    closure.iter().any(|&idx| {
        let pkg = &graph.packages[idx];
        if !matches!(pkg.kind, crate::graph::PackageKind::Local) {
            return false;
        }
        let dev_active_here = dev_active_for.contains(pkg.package.name.as_str());
        pkg.package.dependencies.iter().any(|dep| {
            versioned_dep_active(
                dep,
                idx,
                dev_active_here,
                &host_platform,
                &is_optional_dep_enabled,
                excluded_names,
            )
            .is_some()
        })
    })
}

fn workspace_member_names(graph: &PackageGraph) -> Vec<String> {
    let mut names: Vec<String> = graph
        .primary_packages
        .iter()
        .map(|i| graph.packages[*i].package.name.as_str().to_owned())
        .collect();
    names.sort();
    names
}

#[cfg(test)]
mod tests {
    use std::fmt::Write as _;

    use super::*;
    use crate::loader::load_workspace;
    use assert_fs::TempDir;
    use assert_fs::prelude::*;

    fn workspace_with_two_members(default_members: Option<&str>) -> TempDir {
        let dir = TempDir::new().unwrap();
        let mut root = String::from("[workspace]\nmembers = [\"packages/*\"]\n");
        if let Some(dm) = default_members {
            writeln!(root, "default-members = [\"packages/{dm}\"]").unwrap();
        }
        dir.child("cabin.toml").write_str(&root).unwrap();
        dir.child("packages/a/cabin.toml")
            .write_str("[package]\nname = \"a\"\nversion = \"0.1.0\"\n")
            .unwrap();
        dir.child("packages/b/cabin.toml")
            .write_str("[package]\nname = \"b\"\nversion = \"0.1.0\"\n")
            .unwrap();
        dir
    }

    fn names(graph: &PackageGraph, sel: &ResolvedSelection) -> Vec<String> {
        sel.packages
            .iter()
            .map(|i| graph.packages[*i].package.name.as_str().to_owned())
            .collect()
    }

    #[test]
    fn current_package_falls_back_to_all_members_without_defaults() {
        let dir = workspace_with_two_members(None);
        let graph = load_workspace(dir.path().join("cabin.toml")).unwrap();
        let sel = resolve_package_selection(&graph, &PackageSelection::current_package()).unwrap();
        assert_eq!(names(&graph, &sel), vec!["a", "b"]);
    }

    #[test]
    fn current_package_uses_declared_defaults() {
        let dir = workspace_with_two_members(Some("a"));
        let graph = load_workspace(dir.path().join("cabin.toml")).unwrap();
        let sel = resolve_package_selection(&graph, &PackageSelection::current_package()).unwrap();
        assert_eq!(names(&graph, &sel), vec!["a"]);
    }

    #[test]
    fn whole_workspace_selects_all_members() {
        let dir = workspace_with_two_members(Some("a"));
        let graph = load_workspace(dir.path().join("cabin.toml")).unwrap();
        let sel = resolve_package_selection(
            &graph,
            &PackageSelection {
                mode: SelectionMode::WholeWorkspace,
                exclude: Vec::new(),
            },
        )
        .unwrap();
        assert_eq!(names(&graph, &sel), vec!["a", "b"]);
    }

    #[test]
    fn whole_workspace_with_exclude_drops_member() {
        let dir = workspace_with_two_members(None);
        let graph = load_workspace(dir.path().join("cabin.toml")).unwrap();
        let sel = resolve_package_selection(
            &graph,
            &PackageSelection {
                mode: SelectionMode::WholeWorkspace,
                exclude: vec!["b".into()],
            },
        )
        .unwrap();
        assert_eq!(names(&graph, &sel), vec!["a"]);
    }

    #[test]
    fn explicit_package_selects_named_member() {
        let dir = workspace_with_two_members(None);
        let graph = load_workspace(dir.path().join("cabin.toml")).unwrap();
        let sel = resolve_package_selection(
            &graph,
            &PackageSelection {
                mode: SelectionMode::ExplicitPackages(vec!["a".into()]),
                exclude: Vec::new(),
            },
        )
        .unwrap();
        assert_eq!(names(&graph, &sel), vec!["a"]);
    }

    #[test]
    fn explicit_package_unknown_errors() {
        let dir = workspace_with_two_members(None);
        let graph = load_workspace(dir.path().join("cabin.toml")).unwrap();
        let err = resolve_package_selection(
            &graph,
            &PackageSelection {
                mode: SelectionMode::ExplicitPackages(vec!["nope".into()]),
                exclude: Vec::new(),
            },
        )
        .unwrap_err();
        assert!(matches!(err, WorkspaceError::PackageNotInWorkspace { .. }));
    }

    #[test]
    fn default_members_mode_errors_when_none_declared() {
        let dir = workspace_with_two_members(None);
        let graph = load_workspace(dir.path().join("cabin.toml")).unwrap();
        let err = resolve_package_selection(
            &graph,
            &PackageSelection {
                mode: SelectionMode::DefaultMembers,
                exclude: Vec::new(),
            },
        )
        .unwrap_err();
        assert!(matches!(
            err,
            WorkspaceError::DefaultMemberNotInMembers { .. }
        ));
    }

    #[test]
    fn exclude_with_explicit_packages_errors() {
        let dir = workspace_with_two_members(None);
        let graph = load_workspace(dir.path().join("cabin.toml")).unwrap();
        let err = resolve_package_selection(
            &graph,
            &PackageSelection {
                mode: SelectionMode::ExplicitPackages(vec!["a".into()]),
                exclude: vec!["b".into()],
            },
        )
        .unwrap_err();
        assert!(matches!(
            err,
            WorkspaceError::ExcludeWithoutWorkspaceSelection
        ));
    }

    // -----------------------------------------------------------------
    // closure + versioned-deps helpers.
    // -----------------------------------------------------------------

    /// Workspace where `app` depends on `lib` via path. Selecting
    /// `app` must include `lib` in the closure; `unrelated` must
    /// not be in the closure.
    fn three_member_workspace_app_lib_unrelated() -> TempDir {
        let dir = TempDir::new().unwrap();
        dir.child("cabin.toml")
            .write_str(
                r#"[workspace]
members = ["packages/*"]
"#,
            )
            .unwrap();
        dir.child("packages/app/cabin.toml")
            .write_str(
                r#"[package]
name = "app"
version = "0.1.0"

[dependencies]
lib = { path = "../lib" }
"#,
            )
            .unwrap();
        dir.child("packages/lib/cabin.toml")
            .write_str(
                r#"[package]
name = "lib"
version = "0.1.0"

[dependencies]
fmt = ">=10 <11"
"#,
            )
            .unwrap();
        dir.child("packages/unrelated/cabin.toml")
            .write_str(
                r#"[package]
name = "unrelated"
version = "0.1.0"

[dependencies]
spdlog = "^1"
"#,
            )
            .unwrap();
        dir
    }

    #[test]
    fn closure_includes_local_path_dependency() {
        let dir = three_member_workspace_app_lib_unrelated();
        let graph = load_workspace(dir.path().join("cabin.toml")).unwrap();
        let sel = resolve_package_selection(
            &graph,
            &PackageSelection {
                mode: SelectionMode::ExplicitPackages(vec!["app".into()]),
                exclude: Vec::new(),
            },
        )
        .unwrap();
        let closure = sel.closure(&graph);
        let names: Vec<&str> = closure
            .iter()
            .map(|i| graph.packages[*i].package.name.as_str())
            .collect();
        assert!(names.contains(&"app"), "closure missing app: {names:?}");
        assert!(names.contains(&"lib"), "closure missing lib: {names:?}");
        assert!(
            !names.contains(&"unrelated"),
            "closure leaked unrelated: {names:?}"
        );
    }

    #[test]
    fn versioned_deps_walks_path_dep_closure() {
        let dir = three_member_workspace_app_lib_unrelated();
        let graph = load_workspace(dir.path().join("cabin.toml")).unwrap();
        let sel = resolve_package_selection(
            &graph,
            &PackageSelection {
                mode: SelectionMode::ExplicitPackages(vec!["app".into()]),
                exclude: Vec::new(),
            },
        )
        .unwrap();
        let closure = sel.closure(&graph);
        let deps = collect_closure_versioned_deps_excluding_with_dev(
            &graph,
            &closure,
            |_, _| false,
            &BTreeSet::new(),
            &BTreeSet::new(),
        )
        .unwrap();
        let keys: Vec<&str> = deps.keys().map(cabin_core::PackageName::as_str).collect();
        assert_eq!(keys, vec!["fmt"], "expected only fmt, got {keys:?}");
    }

    #[test]
    fn versioned_deps_skip_unrelated_workspace_members() {
        let dir = three_member_workspace_app_lib_unrelated();
        let graph = load_workspace(dir.path().join("cabin.toml")).unwrap();
        let sel = resolve_package_selection(
            &graph,
            &PackageSelection {
                mode: SelectionMode::ExplicitPackages(vec!["app".into()]),
                exclude: Vec::new(),
            },
        )
        .unwrap();
        let closure = sel.closure(&graph);
        let deps = collect_closure_versioned_deps_excluding_with_dev(
            &graph,
            &closure,
            |_, _| false,
            &BTreeSet::new(),
            &BTreeSet::new(),
        )
        .unwrap();
        assert!(
            !deps.contains_key(&cabin_core::PackageName::new("spdlog").unwrap()),
            "unrelated spdlog leaked into closure deps"
        );
    }

    /// Dev dependencies are excluded from ordinary resolution.
    /// The closure walker must respect that policy so a workspace
    /// member's `[dev-dependencies]` requirement cannot block an
    /// ordinary `cabin build` / `cabin fetch`.
    #[test]
    fn versioned_deps_excludes_dev_kind() {
        let dir = TempDir::new().unwrap();
        dir.child("cabin.toml")
            .write_str(
                r#"[workspace]
members = ["packages/app"]
"#,
            )
            .unwrap();
        dir.child("packages/app/cabin.toml")
            .write_str(
                r#"[package]
name = "app"
version = "0.1.0"

[dependencies]
fmt = ">=10"

[dev-dependencies]
gtest = "^1.14"
"#,
            )
            .unwrap();
        let graph = load_workspace(dir.path().join("cabin.toml")).unwrap();
        let sel = resolve_package_selection(
            &graph,
            &PackageSelection {
                mode: SelectionMode::ExplicitPackages(vec!["app".into()]),
                exclude: Vec::new(),
            },
        )
        .unwrap();
        let closure = sel.closure(&graph);
        let deps = collect_closure_versioned_deps_excluding_with_dev(
            &graph,
            &closure,
            |_, _| false,
            &BTreeSet::new(),
            &BTreeSet::new(),
        )
        .unwrap();
        let keys: Vec<&str> = deps.keys().map(cabin_core::PackageName::as_str).collect();
        assert_eq!(keys, vec!["fmt"]);
    }

    #[test]
    fn excluded_names_are_dropped_from_versioned_deps() {
        let dir = TempDir::new().unwrap();
        dir.child("cabin.toml")
            .write_str(
                r#"[workspace]
members = ["packages/app"]
"#,
            )
            .unwrap();
        dir.child("packages/app/cabin.toml")
            .write_str(
                r#"[package]
name = "app"
version = "0.1.0"

[dependencies]
fmt = ">=10"
spdlog = "^1"
"#,
            )
            .unwrap();
        let graph = load_workspace(dir.path().join("cabin.toml")).unwrap();
        let sel = resolve_package_selection(
            &graph,
            &PackageSelection {
                mode: SelectionMode::ExplicitPackages(vec!["app".into()]),
                exclude: Vec::new(),
            },
        )
        .unwrap();
        let closure = sel.closure(&graph);
        let mut excluded: BTreeSet<String> = BTreeSet::new();
        excluded.insert("fmt".into());
        let deps = collect_closure_versioned_deps_excluding_with_dev(
            &graph,
            &closure,
            |_, _| false,
            &excluded,
            &BTreeSet::new(),
        )
        .unwrap();
        let keys: Vec<&str> = deps.keys().map(cabin_core::PackageName::as_str).collect();
        assert_eq!(keys, vec!["spdlog"]);
    }

    #[test]
    fn closure_has_versioned_deps_excluding_returns_false_when_only_dep_is_excluded() {
        let dir = TempDir::new().unwrap();
        dir.child("cabin.toml")
            .write_str(
                r#"[workspace]
members = ["packages/app"]
"#,
            )
            .unwrap();
        dir.child("packages/app/cabin.toml")
            .write_str(
                r#"[package]
name = "app"
version = "0.1.0"

[dependencies]
fmt = ">=10"
"#,
            )
            .unwrap();
        let graph = load_workspace(dir.path().join("cabin.toml")).unwrap();
        let sel = resolve_package_selection(
            &graph,
            &PackageSelection {
                mode: SelectionMode::ExplicitPackages(vec!["app".into()]),
                exclude: Vec::new(),
            },
        )
        .unwrap();
        let closure = sel.closure(&graph);
        let mut excluded: BTreeSet<String> = BTreeSet::new();
        excluded.insert("fmt".into());
        assert!(!closure_has_versioned_deps_excluding_with_dev(
            &graph,
            &closure,
            |_, _| false,
            &excluded,
            &BTreeSet::new(),
        ));
        // Empty exclusion set leaves the original positive
        // result in place.
        assert!(closure_has_versioned_deps_excluding_with_dev(
            &graph,
            &closure,
            |_, _| false,
            &BTreeSet::new(),
            &BTreeSet::new(),
        ));
    }

    #[test]
    fn versioned_deps_excludes_dev_dependencies() {
        let dir = TempDir::new().unwrap();
        dir.child("cabin.toml")
            .write_str(
                r#"[workspace]
members = ["packages/app"]
"#,
            )
            .unwrap();
        dir.child("packages/app/cabin.toml")
            .write_str(
                r#"[package]
name = "app"
version = "0.1.0"

[dependencies]
fmt = ">=10"

[dev-dependencies]
gtest = "^1.14"
"#,
            )
            .unwrap();
        let graph = load_workspace(dir.path().join("cabin.toml")).unwrap();
        let sel = resolve_package_selection(
            &graph,
            &PackageSelection {
                mode: SelectionMode::ExplicitPackages(vec!["app".into()]),
                exclude: Vec::new(),
            },
        )
        .unwrap();
        let closure = sel.closure(&graph);
        let deps = collect_closure_versioned_deps_excluding_with_dev(
            &graph,
            &closure,
            |_, _| false,
            &BTreeSet::new(),
            &BTreeSet::new(),
        )
        .unwrap();
        let keys: Vec<&str> = deps.keys().map(cabin_core::PackageName::as_str).collect();
        assert_eq!(keys, vec!["fmt"]);
        assert!(
            !deps.contains_key(&cabin_core::PackageName::new("gtest").unwrap()),
            "dev-dep gtest must not enter ordinary resolution"
        );
    }
}