mise 2026.9.14

Dev tools, env vars, and tasks in one CLI
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
//! Starting the daemons a task declares with `daemons = [...]`.
//!
//! This replaces hand-written prerequisite tasks that start a background
//! process and then poll it: pitchfork owns both the process and the readiness
//! check, so `pitchfork start` returns only once every requested daemon reports
//! ready.

use super::{DaemonSet, runtime};
use crate::config::{Config, Settings};
use crate::task::Task;
use eyre::{Result, bail};
use indexmap::{IndexMap, IndexSet};
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// Whether a task asks for any daemon. `false` and an empty list ask for none,
/// so such a task never touches daemon configuration and must not be judged
/// against it.
pub(crate) fn declares_daemons(task: &Task) -> bool {
    match &task.daemons {
        None | Some(crate::task::TaskDaemons::All(false)) => false,
        Some(crate::task::TaskDaemons::Names(names)) => !names.is_empty(),
        Some(_) => true,
    }
}

fn project_for_task(project_root: Option<&Path>, task: &Task) -> Result<Option<PathBuf>> {
    if !declares_daemons(task) {
        return Ok(None);
    }
    task.config_root
        .clone()
        .or_else(|| project_root.map(Path::to_path_buf))
        .map(Some)
        .ok_or_else(|| {
            eyre::eyre!(
                "task {} requires daemons but has no project root",
                task.display_name
            )
        })
}

/// The key under which `set` holds the daemon a task asked for.
///
/// A daemon imported from another project is keyed by its qualified ID, and the
/// name this project gave it lives in the alias table, so a bare local name has
/// to be resolved before it can be looked up.
fn key_for(set: &DaemonSet, name: &str) -> Option<String> {
    if set.daemons.contains_key(name) {
        return Some(name.to_string());
    }
    let resolved = set.resolve_alias(name);
    set.daemons.contains_key(&resolved).then_some(resolved)
}

/// Daemons required by `tasks`, in declaration order, as keys into `set`.
pub(crate) fn required(tasks: &[Task], set: &DaemonSet) -> Result<IndexSet<String>> {
    let mut names = IndexSet::new();
    for task in tasks {
        let Some(daemons) = &task.daemons else {
            continue;
        };
        let requested: Vec<String> = match daemons {
            crate::task::TaskDaemons::All(false) => continue,
            // "every daemon" means the ones this project declares. A daemon
            // imported from elsewhere belongs to that project, and starting it
            // because a local task said `true` would reach further than asked.
            crate::task::TaskDaemons::All(true) => set
                .daemons
                .iter()
                .filter(|(_, d)| !d.imported)
                .map(|(key, _)| key.clone())
                .collect(),
            crate::task::TaskDaemons::One(name) => vec![name.clone()],
            crate::task::TaskDaemons::Names(requested) => requested.clone(),
        };
        for name in requested {
            let Some(key) = key_for(set, &name) else {
                bail!(
                    "task {} requires daemon {name:?}, which is not defined in [daemons]",
                    task.display_name
                );
            };
            names.insert(key);
        }
    }
    Ok(names)
}

/// Whether this run has to start daemons at all, erroring when a task declares
/// them without `experimental`. The flag is a parameter because unit tests force
/// `experimental` on, and the gate is the behavior worth pinning: a run whose
/// tasks declare no daemons is never held to the requirement.
pub(crate) fn gate(experimental: bool, tasks: &[Task]) -> Result<bool> {
    let declared = tasks.iter().any(declares_daemons);
    if declared && !experimental {
        bail!("{}", super::EXPERIMENTAL);
    }
    Ok(declared)
}

/// Start every daemon the given tasks require and wait until pitchfork reports
/// them ready. Already-running daemons are left alone, so this is cheap to
/// repeat.
///
/// A dry run still applies the experimental gate, resolves every name, and
/// validates task-backed daemons, so configuration errors fail there as they
/// would on a real run; it just stops before pitchfork.
///
/// Names are resolved per project, not against one merged set. In a monorepo a
/// dependency task can live in a different subproject, and two subprojects may
/// each declare a daemon of the same name; each task's names are therefore
/// looked up in its own configuration hierarchy.
pub(crate) async fn start(
    config: &Arc<Config>,
    tasks: &[Task],
    dry_run: bool,
    install_tools: bool,
) -> Result<()> {
    // This run is the body of a task-backed daemon. Starting daemons again from
    // here is what would recurse, so stop at this one point and leave the rest
    // of the run, including the task's own `depends`, untouched.
    if crate::env::var_is_true(super::DAEMON_TASK_MARKER) {
        return Ok(());
    }
    if !gate(Settings::get().experimental, tasks)? {
        return Ok(());
    }
    Settings::ensure_not_safe("starting task daemons")?;
    let mut by_project: IndexMap<PathBuf, Vec<Task>> = IndexMap::new();
    for task in tasks {
        let Some(project) = project_for_task(config.project_root.as_deref(), task)? else {
            continue;
        };
        by_project.entry(project).or_default().push(task.clone());
    }
    // A daemon root can be shared by several projects, so collect the whole
    // request before touching pitchfork. Only the names travel: each root's
    // configuration is loaded from the invoking config below, not from the
    // config of whichever task happened to name the daemon first.
    // Names travel as the daemon is known inside the project that owns it, which
    // is not the key this project holds it under when it was imported.
    let mut wanted: IndexMap<PathBuf, IndexSet<String>> = IndexMap::new();
    // Roots reached only because a task named a daemon imported from them. Such
    // a root belongs to another project, which owns its profile and the rest of
    // its daemons.
    let mut foreign: IndexSet<PathBuf> = IndexSet::new();
    let mut owned: IndexSet<PathBuf> = IndexSet::new();
    for (project, tasks) in by_project {
        let scoped = runtime::config_for_root(config, &project).await?;
        let set = scoped.daemons()?;
        let keys = required(&tasks, set)?;
        // Pitchfork starts a daemon's dependencies with it, so this covers what
        // comes along and not only what the task named.
        let starting = set.with_dependencies(&keys.iter().cloned().collect::<Vec<_>>());
        super::ensure_not_blocked(set, &starting, None)?;
        // Take the closure, not only the names the task gave. A dependency can
        // live in a project reached by `project =`, and that project has to be
        // registered and started or the daemon comes up without it.
        for daemon in starting.daemons.values() {
            if daemon.imported {
                foreign.insert(daemon.root.clone());
            } else {
                owned.insert(daemon.root.clone());
            }
            wanted
                .entry(daemon.root.clone())
                .or_default()
                .insert(daemon.name.clone());
        }
    }
    // Reaching a root through one project's own daemon settles it, whether or
    // not something else reached the same root through an import.
    foreign.retain(|root| !owned.contains(root));
    if dry_run {
        for (root, names) in &wanted {
            let scoped = runtime::config_for_root(config, root).await?;
            let set = scoped.daemons()?.for_root(root);
            let starting = set.with_dependencies(&names.iter().cloned().collect::<Vec<_>>());
            // Narrowed for another project's root exactly as the real run
            // narrows it, so an unrelated daemon of theirs cannot fail a
            // dry run that the run itself would have completed.
            if foreign.contains(root) {
                starting.validate_tasks(&scoped).await?;
            } else {
                set.validate_tasks(&scoped).await?;
            }
            super::ensure_not_blocked(&set, &starting, Some(root))?;
            for name in names {
                info!("[dry-run] would start daemon {name} in {}", root.display());
            }
        }
        return Ok(());
    }
    // Pitchfork starts a daemon's dependencies with it, and a dependency can
    // live in another project's root. Register every root first and start only
    // once they all exist, so a local daemon listed before an imported one
    // cannot start while that dependency is still unregistered.
    //
    // Holding several project locks at once means the order they are taken in
    // matters: `mise daemons start` sorts its roots, so this takes them in the
    // same order rather than in whatever order the configuration produced, and
    // the two cannot deadlock against each other.
    wanted.sort_keys();
    let mut pending = Vec::new();
    for (root, names) in wanted {
        let scoped = runtime::config_for_root(config, &root).await?;
        // The generated pitchfork configuration describes every daemon in the
        // project, not only the ones this run starts.
        let set = scoped.daemons()?.for_root(&root);
        if set.daemons.is_empty() {
            continue;
        }
        let will_start = set.with_dependencies(&names.iter().cloned().collect::<Vec<_>>());
        super::presets::ensure_set_runnable_as_user(&will_start)?;
        let previous = runtime::read_state(&root)?;
        let (scoped, ts) = if install_tools {
            runtime::toolset(&scoped, true).await?
        } else {
            // Keeps the install path out of this future entirely, so callers
            // inside a spawned task can await it.
            let ts = runtime::toolset_resolved(&scoped, false).await?;
            (scoped, ts)
        };
        let rt = runtime::Runtime::from_toolset(&scoped, &ts, Some(&previous.bin)).await?;
        let owned = !foreign.contains(&root);
        // Another project's root is registered whole but only checked for what
        // this run starts, so an unrelated daemon of theirs cannot fail a
        // `mise run` here.
        let starting = if owned {
            set.clone()
        } else {
            set.with_dependencies(&names.iter().cloned().collect::<Vec<_>>())
        };
        runtime::validate_tools(&starting, &scoped, &ts).await?;
        starting.validate_tasks(&scoped).await?;
        // `will_start` comes from this root's own configuration, which is the
        // only view that knows about imports the referenced project declares.
        if install_tools {
            super::providers::install_set(&will_start).await?;
        }
        super::ensure_not_blocked(&set, &will_start, Some(&root))?;
        // Let the configuration hash short-circuit re-registration. Forcing it
        // would re-probe `pitchfork usage` and re-run `config add` on every
        // `mise run` of a task that requires daemons, even when nothing about
        // the daemons changed and they are already running.
        // The closure, not the names the task gave: pitchfork starts a
        // daemon's dependencies with it, so their ports are about to be bound
        // and belong in the conflict check too.
        let required: Vec<String> = will_start
            .daemons
            .values()
            .map(|daemon| daemon.name.clone())
            .collect();
        // The profile belongs to the project that owns the root, so a task that
        // reached another project's daemon does not impose its own.
        let (state, _project_lock) = rt.prepare(&root, &set, false, owned, &required).await?;
        let ids: Vec<String> = state
            .ids
            .iter()
            .filter(|id| {
                // `names` holds daemons as the owning project names them, and
                // this root's own set is keyed the same way.
                let name = id.rsplit('/').next().unwrap_or(id);
                names.contains(name) && set.find(name).is_some()
            })
            .cloned()
            .collect();
        if ids.is_empty() {
            continue;
        }
        // The project lock rides along so every root stays held until the last
        // one has started.
        pending.push((rt, root, ids, _project_lock));
    }
    for (rt, root, ids, _project_lock) in pending {
        rt.exec(&root, [vec!["start".into()], ids].concat()).await?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::daemons::Daemon;
    use crate::task::TaskDaemons;

    fn task(name: &str, daemons: Option<TaskDaemons>) -> Task {
        Task {
            name: name.to_string(),
            display_name: name.to_string(),
            daemons,
            ..Default::default()
        }
    }

    /// A set holding one local daemon and one imported from another project,
    /// the way `load` keys them: the import by qualified ID, with the name this
    /// project gave it in the alias table.
    fn set_with_import() -> DaemonSet {
        let mut set = set(&["api"]);
        let mut worker = set.daemons["api"].clone();
        worker.name = "worker".into();
        worker.root = PathBuf::from("/mirror");
        worker.imported = true;
        set.daemons.insert("mirror/worker".into(), worker);
        set.aliases.insert(
            (PathBuf::from("/project"), "pipeline".into()),
            "mirror/worker".into(),
        );
        set.namespaces
            .insert(PathBuf::from("/mirror"), "mirror".into());
        set.namespaces
            .insert(PathBuf::from("/project"), "project".into());
        set
    }

    #[test]
    fn a_task_can_require_a_daemon_imported_from_another_project() {
        let set = set_with_import();
        // The name this project gave the import resolves to its qualified ID.
        let by_alias = [task("dev", Some(TaskDaemons::One("pipeline".into())))];
        assert_eq!(
            required(&by_alias, &set)
                .unwrap()
                .into_iter()
                .collect::<Vec<_>>(),
            ["mirror/worker"]
        );
        // So does the qualified ID itself.
        let by_id = [task("dev", Some(TaskDaemons::One("mirror/worker".into())))];
        assert_eq!(
            required(&by_id, &set)
                .unwrap()
                .into_iter()
                .collect::<Vec<_>>(),
            ["mirror/worker"]
        );
        // `true` means this project's own daemons; another project's daemon is
        // not started because a local task asked for everything.
        let all = [task("dev", Some(TaskDaemons::All(true)))];
        assert_eq!(
            required(&all, &set)
                .unwrap()
                .into_iter()
                .collect::<Vec<_>>(),
            ["api"]
        );
        // A name that is neither is still rejected.
        let unknown = [task("dev", Some(TaskDaemons::One("nope".into())))];
        assert!(
            required(&unknown, &set)
                .unwrap_err()
                .to_string()
                .contains("not defined in [daemons]")
        );
    }

    fn set(names: &[&str]) -> DaemonSet {
        DaemonSet {
            daemons: names
                .iter()
                .map(|name| {
                    (
                        (*name).to_string(),
                        Daemon {
                            name: (*name).to_string(),
                            source: PathBuf::from("/project/mise.toml"),
                            root: PathBuf::from("/project"),
                            table: toml::Table::new(),
                            preset: None,
                            data_dir: None,
                            task: None,
                            tool: None,
                            provider: None,
                            exports: Default::default(),
                            imported: false,
                            port: None,
                            host: None,
                        },
                    )
                })
                .collect(),
            ..Default::default()
        }
    }

    #[test]
    fn required_daemons_are_deduplicated_and_checked() {
        let set = set(&["postgres", "nats"]);
        let tasks = [
            task("dev", Some(TaskDaemons::Names(vec!["postgres".into()]))),
            task("api", Some(TaskDaemons::One("nats".into()))),
            task("web", Some(TaskDaemons::Names(vec!["postgres".into()]))),
            task("test", None),
        ];
        assert_eq!(
            required(&tasks, &set)
                .unwrap()
                .into_iter()
                .collect::<Vec<_>>(),
            ["postgres", "nats"]
        );
        // `true` requires everything the project declares.
        let all = [task("dev", Some(TaskDaemons::All(true)))];
        assert_eq!(required(&all, &set).unwrap().len(), 2);
        // `false` is the same as not declaring any.
        let none = [task("dev", Some(TaskDaemons::All(false)))];
        assert!(required(&none, &set).unwrap().is_empty());
        let unknown = [task("dev", Some(TaskDaemons::One("redis".into())))];
        let err = required(&unknown, &set).unwrap_err().to_string();
        assert!(err.contains("task dev requires daemon \"redis\""), "{err}");
    }

    #[test]
    fn tasks_requiring_daemons_fail_without_experimental() {
        let dev = [task("dev", Some(TaskDaemons::All(true)))];
        let err = gate(false, &dev).unwrap_err().to_string();
        assert_eq!(err, super::super::EXPERIMENTAL);
        assert!(gate(true, &dev).unwrap());
        // A run that requires no daemons is never held to the requirement.
        assert!(!gate(false, &[task("test", None)]).unwrap());
        assert!(!gate(false, &[task("test", Some(TaskDaemons::All(false)))]).unwrap());
        assert!(!gate(false, &[task("test", Some(TaskDaemons::Names(vec![])))]).unwrap());
    }

    #[test]
    fn daemon_requirement_without_project_root_errors() {
        let task = task("server", Some(TaskDaemons::One("postgres".into())));
        let err = project_for_task(None, &task).unwrap_err();
        assert_eq!(
            err.to_string(),
            "task server requires daemons but has no project root"
        );
    }
}