mise 2026.9.11

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
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
use crate::Result;
use crate::config::{ALL_TOML_CONFIG_FILES, TASK_INPUT_GROUP_PREFIX, is_glob_pattern};
use crate::{config, dirs, file};
use eyre::bail;
use std::io::{self, Read, Write};
use taplo::formatter::Options;
use toml_edit::{Array, DocumentMut, RawString, TableLike, Value};

/// Format mise TOML configuration
///
/// Sorts keys and normalizes whitespace using TOML 1.1 syntax, including multiline
/// inline tables. Lists whose order carries no meaning are sorted as well: task
/// `sources` and `outputs`, `task_config.global_inputs` and `input_groups`, and
/// `redactions`. File pattern lists sort by reach — `@group:` references, then
/// globs, then literal paths — and a list is left as written when an entry
/// excludes with `!` or carries a comment. By default, formats config files in
/// the current directory; `--all` includes every loaded config. Use `--check`
/// in CI or `--stdin` to format a supplied document without rewriting a file.
#[derive(Debug, usage_rs::Args)]
#[usage(
    verbatim_doc_comment,
    example(
        r###"mise fmt
mise fmt --check
cat mise.toml | mise fmt --stdin"###
    )
)]
pub(crate) struct Fmt {
    /// Format every config file mise currently loads, not just those in the current directory
    #[usage(short, long)]
    pub all: bool,

    /// Check whether the configs are formatted without rewriting them; exits 1 if any are not
    #[usage(short, long)]
    pub check: bool,

    /// Read config from stdin and write its formatted version into
    /// stdout
    #[usage(short, long)]
    pub stdin: bool,
}

impl Fmt {
    pub(crate) fn run(self) -> eyre::Result<()> {
        if self.stdin {
            let mut toml = String::new();
            io::stdin().read_to_string(&mut toml)?;

            let toml = sort(toml)?;
            let toml = format(toml)?;
            let mut stdout = io::stdout();
            write!(stdout, "{toml}")?;

            return Ok(());
        }

        let cwd = dirs::CWD.clone().unwrap_or_default();
        let configs = if self.all {
            ALL_TOML_CONFIG_FILES.clone()
        } else {
            config::config_files_in_dir(&cwd)
        };
        if configs.is_empty() {
            bail!("No config file found in current directory");
        }
        let mut errors = Vec::new();
        for p in configs {
            if !p
                .file_name()
                .is_some_and(|f| f.to_string_lossy().ends_with("toml"))
            {
                continue;
            }
            let source = file::read_to_string(&p)?;
            let toml = source.clone();
            let toml = sort(toml)?;
            let toml = format(toml)?;
            if self.check {
                if source != toml {
                    errors.push(p.display().to_string());
                }
                continue;
            }
            file::write(&p, &toml)?;
        }

        if !errors.is_empty() {
            bail!(
                "Following config files are not properly formatted:\n{}",
                errors.join("\n")
            );
        }

        Ok(())
    }
}

/// Put the document's top-level keys and its unordered lists into a canonical
/// order. Whitespace and layout are left to [`format`].
fn sort(toml: String) -> Result<String> {
    let mut doc: DocumentMut = toml.parse()?;
    let order = |k: String| match k.as_str() {
        "min_version" => 0,
        "env_file" => 1,
        "env_path" => 2,
        "_" => 3,
        "env" => 4,
        "vars" => 5,
        "hooks" => 6,
        "watch_files" => 7,
        "tools" => 8,
        "tasks" => 10,
        "task_config" => 11,
        "redactions" => 12,
        "alias" => 13,
        "plugins" => 14,
        "settings" => 15,
        _ => 9,
    };
    doc.sort_values_by(|a, _, b, _| order(a.to_string()).cmp(&order(b.to_string())));
    sort_unordered_sets(&mut doc);
    Ok(doc.to_string())
}

/// Arrays whose entries form a set: reordering them cannot change what mise
/// does, so `mise fmt` can put them in a canonical order.
///
/// Paths are matched key by key from the document root, and `*` matches any
/// single key. Order-sensitive lists are deliberately absent: `env_file`,
/// `env_path`, and the `_.path`/`_.source` directives establish precedence,
/// `tools.*` treats the first version as the primary one,
/// `task_config.includes` fixes task discovery order, and
/// `depends`/`depends_post`/`wait_for` are often grouped by hand.
///
/// `mise fmt` only ever sees whole config files, so tasks are always nested
/// under `tasks`; a standalone task TOML file listed in
/// `task_config.includes` is not formatted and needs no path of its own.
const UNORDERED_SETS: &[(&[&str], SetOrder)] = &[
    (&["redactions"], SetOrder::Lexical),
    (&["task_config", "global_inputs"], SetOrder::FilePatterns),
    (
        &["task_config", "input_groups", "*"],
        SetOrder::FilePatterns,
    ),
    (&["tasks", "*", "sources"], SetOrder::FilePatterns),
    (&["tasks", "*", "outputs"], SetOrder::FilePatterns),
    (&["task_templates", "*", "sources"], SetOrder::FilePatterns),
    (&["task_templates", "*", "outputs"], SetOrder::FilePatterns),
];

#[derive(Clone, Copy)]
enum SetOrder {
    /// Sort entries lexically.
    Lexical,
    /// Sort by how broadly an entry reaches — `@group:` references, then
    /// globs, then the literal paths those globs broaden — and lexically
    /// within each rank. Reading a list top to bottom then narrows from the
    /// inputs a task inherits down to the individual files it names.
    FilePatterns,
}

/// Put every array named in [`UNORDERED_SETS`] into its canonical order.
fn sort_unordered_sets(doc: &mut DocumentMut) {
    for (path, order) in UNORDERED_SETS {
        visit_arrays(doc.as_table_mut(), path, &mut |array| {
            sort_set(array, *order);
        });
    }
}

/// Call `visit` on every array the document holds at `path`, where a `*`
/// segment matches any key. Tables and inline tables are both walked, so a
/// task written as `[tasks.build]`, as `build = { .. }`, or with dotted keys
/// is reached the same way.
fn visit_arrays(table: &mut dyn TableLike, path: &[&str], visit: &mut impl FnMut(&mut Array)) {
    let Some((segment, rest)) = path.split_first() else {
        return;
    };
    for (key, item) in table.iter_mut() {
        if *segment != "*" && key.get() != *segment {
            continue;
        }
        if rest.is_empty() {
            if let Some(array) = item.as_array_mut() {
                visit(array);
            }
        } else if let Some(child) = item.as_table_like_mut() {
            visit_arrays(child, rest, visit);
        }
    }
}

/// Sort one array, leaving it as written when reordering could change what
/// it means.
fn sort_set(array: &mut Array, order: SetOrder) {
    if !is_sortable(array, order) {
        return;
    }
    array.sort_by(|a, b| sort_key(a, order).cmp(&sort_key(b, order)));
}

/// Where `value` sorts: its rank first, then the entry itself to break ties
/// within a rank.
fn sort_key(value: &Value, order: SetOrder) -> (u8, &str) {
    let entry = value.as_str().unwrap_or_default();
    match order {
        SetOrder::Lexical => (0, entry),
        SetOrder::FilePatterns => (pattern_rank(entry), entry),
    }
}

/// How broadly a file pattern reaches, lowest first.
fn pattern_rank(entry: &str) -> u8 {
    if entry.starts_with(TASK_INPUT_GROUP_PREFIX) {
        0
    } else if is_glob_pattern(entry) {
        1
    } else {
        2
    }
}

/// Whether reordering `array` is guaranteed to preserve its meaning.
///
/// Entries that are not plain strings are left alone because their order may
/// carry meaning this function cannot see. A comment blocks the sort for the
/// same reason: once entries move there is no way to tell which one it was
/// written about. A comment that follows the last entry lives in the array's
/// trailing decor rather than on a value, so it is checked separately.
fn is_sortable(array: &Array, order: SetOrder) -> bool {
    if has_comment(Some(array.trailing())) {
        return false;
    }
    array.iter().all(|value| {
        value.as_str().is_some_and(|entry| match order {
            SetOrder::Lexical => true,
            SetOrder::FilePatterns => !may_exclude(entry),
        }) && !has_comment(value.decor().prefix())
            && !has_comment(value.decor().suffix())
    })
}

/// Whether `entry` excludes the files it matches, which makes the position of
/// every entry around it meaningful: `sources` and `outputs` are evaluated in
/// order and the last matching entry wins.
///
/// Exclusion is decided on the rendered entry, by the leading `!` that
/// [`crate::task::task_source_checker::source_glob_patterns`] looks for, so
/// only what comes first matters. `\!` escapes a literal `!` and does not
/// exclude. An entry that *opens* with a template tag can render into either
/// and is treated as if it excludes; one that opens with a literal still
/// begins with that literal once rendered, so an embedded tag cannot turn it
/// into an exclusion. Whitespace-control tags (`{{-`) strip what precedes
/// them, so the leading whitespace is trimmed before the tag is looked for.
fn may_exclude(entry: &str) -> bool {
    let head = entry.trim_start();
    head.starts_with('!') || head.starts_with("{{") || head.starts_with("{%")
}

/// Whether a span of decor holds a comment rather than only whitespace.
fn has_comment(raw: Option<&RawString>) -> bool {
    raw.and_then(RawString::as_str)
        .is_some_and(|decor| decor.contains('#'))
}

fn format(toml: String) -> Result<String> {
    let tmp = taplo::formatter::format(
        &toml,
        Options {
            align_comments: true,
            align_entries: false,
            align_single_comments: true,
            allowed_blank_lines: 2,
            array_auto_collapse: true,
            array_auto_expand: true,
            array_trailing_comma: true,
            column_width: 80,
            compact_arrays: true,
            compact_entries: false,
            compact_inline_tables: false,
            crlf: false,
            indent_entries: false,
            indent_string: "  ".to_string(),
            indent_tables: false,
            inline_table_expand: true,
            reorder_arrays: false,
            reorder_keys: false,
            reorder_inline_tables: false,
            trailing_newline: true,
        },
    );

    Ok(tmp)
}

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

    /// Run the same pipeline `mise fmt` writes back to a config file.
    fn fmt(toml: &str) -> String {
        format(sort(toml.to_string()).unwrap()).unwrap()
    }

    #[test]
    fn sorts_groups_then_globs_then_literal_paths() {
        let toml = fmt(r#"
[tasks.build]
sources = ["Cargo.toml", "src/**/*.rs", "@group:rust", "@group:lock"]
"#);

        assert!(
            toml.contains(
                r#"sources = ["@group:lock", "@group:rust", "src/**/*.rs", "Cargo.toml"]"#
            ),
            "{toml}"
        );
    }

    #[test]
    fn ranks_every_glob_metacharacter_ahead_of_a_literal_path() {
        let toml = fmt(r#"
[tasks.build]
sources = ["a.rs", "b?.rs", "c[0-9].rs", "d{x,y}.rs", "e*.rs"]
"#);

        assert!(
            toml.contains(r#""b?.rs", "c[0-9].rs", "d{x,y}.rs", "e*.rs", "a.rs""#),
            "{toml}"
        );
    }

    #[test]
    fn sorts_the_task_input_sets_outside_tasks() {
        let toml = fmt(r#"
redactions = ["TOKEN", "API_KEY"]

[task_config]
global_inputs = ["mise.toml", "@group:lockfiles", ".tool-versions"]

[task_config.input_groups]
lockfiles = ["pnpm-lock.yaml", "Cargo.lock"]
"#);

        assert!(
            toml.contains(r#"redactions = ["API_KEY", "TOKEN"]"#),
            "{toml}"
        );
        assert!(
            toml.contains(r#"global_inputs = ["@group:lockfiles", ".tool-versions", "mise.toml"]"#),
            "{toml}"
        );
        assert!(
            toml.contains(r#"lockfiles = ["Cargo.lock", "pnpm-lock.yaml"]"#),
            "{toml}"
        );
    }

    #[test]
    fn sorts_a_task_written_as_an_inline_table() {
        let toml = fmt(r#"
[tasks]
build = { run = "cargo build", sources = ["src/b.rs", "src/a.rs"] }
"#);

        assert!(
            toml.contains(r#"sources = ["src/a.rs", "src/b.rs"]"#),
            "{toml}"
        );
    }

    #[test]
    fn sorts_a_task_template() {
        let toml = fmt(r#"
[task_templates.rust]
sources = ["Cargo.toml", "src/**/*.rs"]
outputs = ["target/debug/b", "target/debug/a"]
"#);

        assert!(
            toml.contains(r#"sources = ["src/**/*.rs", "Cargo.toml"]"#),
            "{toml}"
        );
        assert!(
            toml.contains(r#"outputs = ["target/debug/a", "target/debug/b"]"#),
            "{toml}"
        );
    }

    #[test]
    fn sorts_sources_whose_template_cannot_open_the_entry() {
        // A literal prefix survives rendering, so an embedded tag can never
        // turn the entry into an exclusion and the list stays a set.
        let toml = fmt(r#"
[tasks.build]
sources = ["src/b.rs", "src/{{ vars.dir }}/**", "Cargo.toml"]
"#);

        assert!(
            toml.contains(r#"sources = ["src/{{ vars.dir }}/**", "Cargo.toml", "src/b.rs"]"#),
            "{toml}"
        );
    }

    #[test]
    fn keeps_the_order_of_sources_opening_with_a_whitespace_control_tag() {
        // `{{-` strips what precedes it, so this renders to whatever the tag
        // yields and could still begin with `!`.
        let toml = fmt(r#"
[tasks.build]
sources = [" {{- vars.pattern }}", "Cargo.toml"]
"#);

        assert!(
            toml.contains(r#"sources = [" {{- vars.pattern }}", "Cargo.toml"]"#),
            "{toml}"
        );
    }

    #[test]
    fn sorts_sources_that_escape_a_literal_exclamation_mark() {
        let toml = fmt(r#"
[tasks.build]
sources = ["src/b.rs", "\\!important.txt"]
"#);

        assert!(
            toml.contains(r#"sources = ["\\!important.txt", "src/b.rs"]"#),
            "{toml}"
        );
    }

    #[test]
    fn formatting_a_sorted_set_again_changes_nothing() {
        let once = fmt(r#"
redactions = ["TOKEN", "API_KEY"]

[task_config]
global_inputs = ["mise.toml", "@group:lockfiles"]

[task_config.input_groups]
lockfiles = ["pnpm-lock.yaml", "Cargo.lock"]

[tasks.build]
outputs = ["dist/bundle.js", "dist/bundle.css"]
sources = ["src/b.ts", "@group:lockfiles", "src/**/*.ts", "src/a.ts"]
"#);

        assert_eq!(fmt(&once), once);
    }

    #[test]
    fn keeps_the_order_of_sources_that_exclude() {
        let toml = fmt(r#"
[tasks.build]
sources = ["src/**/*.ts", "!src/**/*.test.ts", "src/keep.test.ts"]
"#);

        assert!(
            toml.contains(r#"sources = ["src/**/*.ts", "!src/**/*.test.ts", "src/keep.test.ts"]"#),
            "{toml}"
        );
    }

    #[test]
    fn keeps_the_order_of_outputs_that_exclude() {
        let toml = fmt(r#"
[tasks.build]
outputs = ["dist", "!dist/**/*.map"]
"#);

        assert!(
            toml.contains(r#"outputs = ["dist", "!dist/**/*.map"]"#),
            "{toml}"
        );
    }

    #[test]
    fn keeps_the_order_of_sources_that_start_with_a_template() {
        let toml = fmt(r#"
[tasks.build]
sources = ["{{ arg(name = 'src') }}", "Cargo.toml"]
"#);

        assert!(
            toml.contains(r#"sources = ["{{ arg(name = 'src') }}", "Cargo.toml"]"#),
            "{toml}"
        );
    }

    #[test]
    fn keeps_the_order_of_a_set_that_carries_a_comment() {
        let toml = fmt(r#"
[tasks.build]
sources = [
  "src/b.rs", # this comment must not end up describing another entry
  "src/a.rs",
]
"#);

        let b = toml.find("src/b.rs").unwrap();
        let a = toml.find("src/a.rs").unwrap();
        assert!(b < a, "entries moved out from under a comment:\n{toml}");
    }

    #[test]
    fn keeps_the_order_of_a_set_whose_last_entry_carries_a_comment() {
        let toml = fmt(r#"
[tasks.build]
sources = [
  "src/b.rs",
  "src/a.rs", # this comment must not end up describing another entry
]
"#);

        let b = toml.find("src/b.rs").unwrap();
        let a = toml.find("src/a.rs").unwrap();
        assert!(b < a, "entries moved out from under a comment:\n{toml}");
    }

    #[test]
    fn keeps_the_order_of_a_set_followed_by_a_dangling_comment() {
        let toml = fmt(r#"
[tasks.build]
sources = [
  "src/b.rs",
  "src/a.rs",
  # a note about the list, or about the entry above it
]
"#);

        let b = toml.find("src/b.rs").unwrap();
        let a = toml.find("src/a.rs").unwrap();
        assert!(b < a, "entries moved out from under a comment:\n{toml}");
    }

    #[test]
    fn keeps_the_order_of_lists_whose_order_has_meaning() {
        let toml = fmt(r#"
env_file = [".env.local", ".env"]

[env]
_.path = ["./node_modules/.bin", "./bin"]

[tools]
node = ["22", "20"]

[tasks.build]
depends = ["render", "build:deps"]
"#);

        assert!(
            toml.contains(r#"env_file = [".env.local", ".env"]"#),
            "{toml}"
        );
        assert!(
            toml.contains(r#"_.path = ["./node_modules/.bin", "./bin"]"#),
            "{toml}"
        );
        assert!(toml.contains(r#"node = ["22", "20"]"#), "{toml}");
        assert!(
            toml.contains(r#"depends = ["render", "build:deps"]"#),
            "{toml}"
        );
    }

    #[test]
    fn keeps_an_outputs_table_intact() {
        let toml = fmt(r#"
[tasks.build]
outputs = { auto = true }
sources = ["src/b.rs", "src/a.rs"]
"#);

        assert!(toml.contains("auto = true"), "{toml}");
        assert!(
            toml.contains(r#"sources = ["src/a.rs", "src/b.rs"]"#),
            "{toml}"
        );
    }

    #[test]
    fn sorts_the_sources_of_a_task_named_after_a_sorted_key() {
        let toml = fmt(r#"
[tasks.sources]
depends = ["b", "a"]
sources = ["src/b.rs", "src/a.rs"]
"#);

        assert!(toml.contains(r#"depends = ["b", "a"]"#), "{toml}");
        assert!(
            toml.contains(r#"sources = ["src/a.rs", "src/b.rs"]"#),
            "{toml}"
        );
    }
}