midenup 1.0.0

The Miden toolchain manager
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
//! Expanding a persisted selection into an ordered, installable component set.
//!
//! This is the single resolver. Everything that needs to know "which components does this
//! selection mean" goes through [resolve] -- direct installs, toolchain-file activation, updates,
//! and plan construction alike. A second implementation of the same question, however narrow its
//! caller, is a second answer that can disagree with this one.

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

use crate::{
    manifest::{Channel, Component},
    profile::Profile,
};

/// What the user wants installed for a channel.
///
/// There is exactly one representation. A selection carried forward from a v1 migration is simply
/// one with no profiles and explicit roots, which already yields the intended behaviour -- new
/// dependencies of those roots are picked up, unrelated new profile members are not -- without a
/// second concept.
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Intent {
    /// Profiles observed across every activation and direct install for this channel.
    #[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
    pub profiles: BTreeSet<Profile>,
    /// Components named explicitly.
    #[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
    pub roots: BTreeSet<String>,
}

impl Intent {
    pub fn new(profiles: &[Profile], roots: &[&str]) -> Self {
        Self {
            profiles: profiles.iter().copied().collect(),
            roots: roots.iter().map(|r| r.to_string()).collect(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.profiles.is_empty() && self.roots.is_empty()
    }

    /// Merges `other` in, never removing anything.
    ///
    /// This is what activating a project's toolchain file does. Two projects on the same channel
    /// must not fight: one asking for less cannot take components away from the other, so
    /// activation only ever adds.
    ///
    /// `complete` absorbs the other profiles -- it already means every component, so keeping the
    /// rest alongside it would be noise that suggests a distinction that does not exist.
    pub fn union_with(&mut self, other: &Intent) {
        self.profiles.extend(other.profiles.iter().copied());
        self.roots.extend(other.roots.iter().cloned());
        if self.profiles.contains(&Profile::Complete) {
            self.profiles = [Profile::Complete].into_iter().collect();
        }
    }

    /// Replaces this intent outright.
    ///
    /// This is what a direct `midenup install` does: the user is stating what they want for the
    /// channel, which may be *less* than before. Unlike activation, it is allowed to shrink.
    pub fn replace_with(&mut self, other: Intent) {
        *self = other;
    }
}

#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum ResolutionError {
    #[error("component '{component}' was requested but does not exist in channel {channel}")]
    UnknownRoot {
        component: String,
        channel: semver::Version,
    },
    #[error("component '{component}' requires '{requires}', which does not exist in the channel")]
    UnknownRequirement { component: String, requires: String },
    #[error("the component graph contains a cycle: {}", path.join(" -> "))]
    RequirementCycle { path: Vec<String> },
    #[error(
        "component '{component}' has kind '{tag}', which is not supported by this version of \
         midenup; upgrade midenup or deselect it"
    )]
    UnsupportedComponentKind { component: String, tag: String },
}

/// Expands `intent` into the exact set of components to install, dependencies first.
///
/// The returned order is a topological sort of the transitive `requires` closure, deduplicated.
pub fn resolve<'a>(
    channel: &'a Channel,
    intent: &Intent,
) -> Result<Vec<&'a Component>, ResolutionError> {
    let index: HashMap<&str, usize> = channel
        .components
        .iter()
        .enumerate()
        .map(|(i, c)| (c.name.as_ref(), i))
        .collect();

    let install_everything = intent.profiles.contains(&Profile::Complete);

    // Roots from profiles. An unsupported component belongs to no profile regardless of what its
    // `profiles` field claims: this build cannot install it, so selecting it implicitly would turn
    // an ordinary `--profile minimal` into a hard failure.
    let mut roots: BTreeSet<usize> = BTreeSet::new();
    for (i, component) in channel.components.iter().enumerate() {
        if !component.is_supported() {
            continue;
        }
        if install_everything || component.profiles.iter().any(|p| intent.profiles.contains(p)) {
            roots.insert(i);
        }
    }

    // Roots named explicitly. Naming an unsupported component *is* an error -- the user asked for
    // something specific and silently omitting it would be the worst outcome.
    for name in intent.roots.iter() {
        let Some(&i) = index.get(name.as_str()) else {
            return Err(ResolutionError::UnknownRoot {
                component: name.clone(),
                channel: channel.name.clone(),
            });
        };
        let component = &channel.components[i];
        if !component.is_supported() {
            return Err(ResolutionError::UnsupportedComponentKind {
                component: component.name.to_string(),
                tag: component.kind().tag().to_string(),
            });
        }
        roots.insert(i);
    }

    // Transitive closure over `requires`, not a single level of expansion.
    let mut selected: BTreeSet<usize> = BTreeSet::new();
    let mut worklist: Vec<usize> = roots.iter().copied().collect();
    let mut graph = petgraph::graphmap::DiGraphMap::<usize, ()>::new();

    while let Some(i) = worklist.pop() {
        if !selected.insert(i) {
            continue;
        }
        graph.add_node(i);

        let component = &channel.components[i];
        for required in component.requires.iter() {
            let Some(&j) = index.get(required.as_str()) else {
                return Err(ResolutionError::UnknownRequirement {
                    component: component.name.to_string(),
                    requires: required.clone(),
                });
            };

            let dependency = &channel.components[j];
            if !dependency.is_supported() {
                return Err(ResolutionError::UnsupportedComponentKind {
                    component: dependency.name.to_string(),
                    tag: dependency.kind().tag().to_string(),
                });
            }

            graph.add_node(j);
            // Edge points dependency -> dependent, so a topological sort yields dependencies
            // first, which is the order installation needs.
            graph.add_edge(j, i, ());
            worklist.push(j);
        }
    }

    match petgraph::algo::toposort(&graph, None) {
        Ok(order) => Ok(order.into_iter().map(|i| &channel.components[i]).collect()),
        Err(_) => Err(ResolutionError::RequirementCycle { path: find_cycle(&graph, channel) }),
    }
}

/// Names the components on a cycle.
///
/// `toposort` only reports one node in the cycle, which is not enough to act on. The strongly
/// connected component containing it is the cycle, so report all of its members.
fn find_cycle(graph: &petgraph::graphmap::DiGraphMap<usize, ()>, channel: &Channel) -> Vec<String> {
    for scc in petgraph::algo::tarjan_scc(graph) {
        let is_cycle =
            scc.len() > 1 || scc.first().is_some_and(|&n| graph.neighbors(n).any(|m| m == n));
        if is_cycle {
            let mut names: Vec<String> =
                scc.iter().map(|&i| channel.components[i].name.to_string()).collect();
            names.sort();
            return names;
        }
    }
    Vec::new()
}

#[cfg(test)]
mod tests {
    use std::borrow::Cow;

    use super::*;
    use crate::{
        artifact::Artifacts,
        manifest::{ComponentKind, OpaqueBody},
        version::Authority,
    };

    /// Builds a channel from `(name, profiles, requires)` triples.
    fn channel(spec: &[(&'static str, &[Profile], &[&str])]) -> Channel {
        channel_with(spec, None)
    }

    /// As [channel], plus an optional component whose kind this build does not recognize.
    fn channel_with(
        spec: &[(&'static str, &[Profile], &[&str])],
        unsupported: Option<(&'static str, &[Profile])>,
    ) -> Channel {
        let mut components: Vec<Component> = spec
            .iter()
            .map(|(name, profiles, requires)| Component {
                name: Cow::Borrowed(name),
                version: Authority::Registry { version: semver::Version::new(0, 1, 0) },
                kind: ComponentKind::Package,
                profiles: profiles.to_vec(),
                requires: requires.iter().map(|r| r.to_string()).collect(),
                artifacts: Artifacts::default(),
                extra: Default::default(),
            })
            .collect();

        if let Some((name, profiles)) = unsupported {
            components.push(Component {
                name: Cow::Borrowed(name),
                version: Authority::Registry { version: semver::Version::new(1, 0, 0) },
                kind: ComponentKind::Unsupported {
                    tag: "wasm-module".to_string(),
                    body: OpaqueBody(Default::default()),
                },
                profiles: profiles.to_vec(),
                requires: vec![],
                artifacts: Artifacts::default(),
                extra: Default::default(),
            });
        }

        Channel::new(semver::Version::new(0, 15, 0), components)
    }

    fn names(resolved: &[&Component]) -> Vec<String> {
        resolved.iter().map(|c| c.name.to_string()).collect()
    }

    /// The closure follows `requires` all the way down: in `A -> B -> C`, selecting `A` must pull
    /// in `C` too, not just the one level below it.
    #[test]
    fn closure_is_fully_transitive() {
        let ch =
            channel(&[("a", &[Profile::Minimal], &["b"]), ("b", &[], &["c"]), ("c", &[], &[])]);
        let got = names(&resolve(&ch, &Intent::new(&[Profile::Minimal], &[])).unwrap());
        assert_eq!(got, vec!["c", "b", "a"], "dependencies must precede dependents");
    }

    #[test]
    fn diamond_dependencies_are_deduplicated_and_ordered() {
        let ch = channel(&[
            ("top", &[Profile::Minimal], &["l", "r"]),
            ("l", &[], &["base"]),
            ("r", &[], &["base"]),
            ("base", &[], &[]),
        ]);
        let got = names(&resolve(&ch, &Intent::new(&[Profile::Minimal], &[])).unwrap());

        assert_eq!(got.len(), 4, "no duplicates: {got:?}");
        let pos = |n: &str| got.iter().position(|g| g == n).unwrap();
        assert!(pos("base") < pos("l") && pos("base") < pos("r"));
        assert!(pos("l") < pos("top") && pos("r") < pos("top"));
    }

    #[test]
    fn empty_plus_explicit_root_resolves_to_that_root_and_its_closure() {
        let ch = channel(&[("a", &[Profile::Minimal], &[]), ("b", &[], &["c"]), ("c", &[], &[])]);
        let got = names(&resolve(&ch, &Intent::new(&[Profile::Empty], &["b"])).unwrap());
        assert_eq!(got, vec!["c", "b"], "must not pull in minimal-profile members");
    }

    #[test]
    fn minimal_plus_explicit_root_unions_them() {
        let ch = channel(&[("a", &[Profile::Minimal], &[]), ("b", &[], &[])]);
        let mut got = names(&resolve(&ch, &Intent::new(&[Profile::Minimal], &["b"])).unwrap());
        got.sort();
        assert_eq!(got, vec!["a", "b"]);
    }

    #[test]
    fn complete_selects_every_component_regardless_of_profile_tags() {
        let ch = channel(&[("a", &[], &[]), ("b", &[Profile::Minimal], &[])]);
        assert_eq!(resolve(&ch, &Intent::new(&[Profile::Complete], &[])).unwrap().len(), 2);
    }

    #[test]
    fn complete_dominates_other_profiles_in_the_same_intent() {
        let ch = channel(&[("a", &[], &[]), ("b", &[Profile::Minimal], &[])]);
        let intent = Intent::new(&[Profile::Minimal, Profile::Complete], &[]);
        assert_eq!(resolve(&ch, &intent).unwrap().len(), 2);
    }

    #[test]
    fn an_empty_intent_resolves_to_nothing() {
        let ch = channel(&[("a", &[Profile::Minimal], &[])]);
        assert!(resolve(&ch, &Intent::default()).unwrap().is_empty());
    }

    #[test]
    fn a_missing_explicit_root_is_an_error() {
        let ch = channel(&[("a", &[Profile::Minimal], &[])]);
        assert!(matches!(
            resolve(&ch, &Intent::new(&[], &["nope"])),
            Err(ResolutionError::UnknownRoot { .. })
        ));
    }

    #[test]
    fn a_dangling_requirement_is_an_error() {
        let ch = channel(&[("a", &[Profile::Minimal], &["ghost"])]);
        assert!(matches!(
            resolve(&ch, &Intent::new(&[Profile::Minimal], &[])),
            Err(ResolutionError::UnknownRequirement { .. })
        ));
    }

    #[test]
    fn a_cycle_is_reported_with_its_members() {
        let ch = channel(&[("a", &[Profile::Minimal], &["b"]), ("b", &[], &["a"])]);
        match resolve(&ch, &Intent::new(&[Profile::Minimal], &[])) {
            Err(ResolutionError::RequirementCycle { path }) => {
                assert_eq!(path, vec!["a", "b"], "the cycle members must be named");
            },
            other => panic!("expected a cycle error, got {other:?}"),
        }
    }

    #[test]
    fn a_self_requirement_is_a_cycle() {
        let ch = channel(&[("a", &[Profile::Minimal], &["a"])]);
        assert!(matches!(
            resolve(&ch, &Intent::new(&[Profile::Minimal], &[])),
            Err(ResolutionError::RequirementCycle { .. })
        ));
    }

    /// An unsupported component must never be dragged in by a profile -- that would turn an
    /// ordinary `--profile minimal` into a hard failure on any channel that ships a new kind.
    #[test]
    fn unsupported_components_are_never_implicitly_selected() {
        let ch = channel_with(
            &[("a", &[Profile::Minimal], &[])],
            Some(("futurething", &[Profile::Minimal])),
        );
        let got = names(&resolve(&ch, &Intent::new(&[Profile::Minimal], &[])).unwrap());
        assert_eq!(got, vec!["a"]);

        // `complete` means every component this build can install, not "fail on anything new".
        let got = names(&resolve(&ch, &Intent::new(&[Profile::Complete], &[])).unwrap());
        assert_eq!(got, vec!["a"]);
    }

    /// ...but asking for one by name is an error, not a silent omission.
    #[test]
    fn naming_an_unsupported_component_explicitly_is_an_error() {
        let ch = channel_with(&[("a", &[Profile::Minimal], &[])], Some(("futurething", &[])));
        assert!(matches!(
            resolve(&ch, &Intent::new(&[], &["futurething"])),
            Err(ResolutionError::UnsupportedComponentKind { .. })
        ));
    }

    /// Reaching one through a dependency edge is equally an error: the selected component cannot
    /// work without it.
    #[test]
    fn depending_on_an_unsupported_component_is_an_error() {
        let ch = channel_with(
            &[("a", &[Profile::Minimal], &["futurething"])],
            Some(("futurething", &[])),
        );
        assert!(matches!(
            resolve(&ch, &Intent::new(&[Profile::Minimal], &[])),
            Err(ResolutionError::UnsupportedComponentKind { .. })
        ));
    }
}

#[cfg(test)]
mod intent_tests {
    use super::*;

    #[test]
    fn activation_unions_and_never_shrinks() {
        let mut intent = Intent::new(&[Profile::Minimal], &["client"]);
        intent.union_with(&Intent::new(&[Profile::Minimal], &["debug"]));

        assert!(
            intent.roots.contains("client"),
            "another project must not remove this one's root"
        );
        assert!(intent.roots.contains("debug"));
    }

    #[test]
    fn union_with_a_smaller_intent_keeps_everything() {
        let mut intent = Intent::new(&[Profile::Minimal], &["client", "debug"]);
        intent.union_with(&Intent::new(&[Profile::Empty], &[]));

        assert!(intent.roots.contains("client") && intent.roots.contains("debug"));
        assert!(intent.profiles.contains(&Profile::Minimal));
    }

    #[test]
    fn complete_absorbs_other_profiles() {
        let mut intent = Intent::new(&[Profile::Minimal], &["client"]);
        intent.union_with(&Intent::new(&[Profile::Complete], &[]));

        assert_eq!(intent.profiles, [Profile::Complete].into_iter().collect());
        assert!(
            intent.roots.contains("client"),
            "explicit roots survive; only profiles collapse"
        );
    }

    /// Direct installation states what the user wants *now*, so it may shrink -- even when the
    /// resolved set happens to be unchanged, the recorded intent must still be replaced.
    #[test]
    fn direct_install_replaces_even_when_the_resolved_set_is_unchanged() {
        let mut intent = Intent::new(&[Profile::Complete], &["client"]);
        intent.replace_with(Intent::new(&[Profile::Minimal], &[]));

        assert!(intent.roots.is_empty());
        assert_eq!(intent.profiles, [Profile::Minimal].into_iter().collect());
    }

    #[test]
    fn unioning_into_an_empty_intent_adopts_it() {
        let mut intent = Intent::default();
        intent.union_with(&Intent::new(&[Profile::Minimal], &["vm"]));
        assert_eq!(intent, Intent::new(&[Profile::Minimal], &["vm"]));
    }
}