mise 2026.5.0

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
use crate::cli::args::ToolArg;
use crate::cmd::cmd;
use crate::config::Config;
use crate::file::display_path;
use crate::registry::{REGISTRY, RegistryTool};
use crate::tera::get_tera;
use crate::toolset::{InstallOptions, ToolsetBuilder};
use crate::ui::time;
use crate::{dirs, env, file};
use eyre::{Result, bail, eyre};
use std::path::{Path, PathBuf};
use std::{collections::BTreeSet, sync::Arc};
use tokio::task::JoinSet;

/// Test a tool installs and executes
#[derive(Debug, Clone, clap::Args)]
#[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct TestTool {
    /// Tool(s) to test
    #[clap(required_unless_present_any = ["all", "all_config"])]
    pub tools: Option<Vec<ToolArg>>,
    /// Test every tool specified in registry/
    #[clap(long, short, conflicts_with = "tools", conflicts_with = "all_config")]
    pub all: bool,
    /// Number of tool tests to run in parallel
    /// [default: 4]
    #[clap(long, short, env = "MISE_TEST_TOOL_JOBS", verbatim_doc_comment)]
    pub jobs: Option<usize>,
    /// Test all tools specified in config files
    #[clap(long, conflicts_with = "tools", conflicts_with = "all")]
    pub all_config: bool,
    /// Also test tools not defined in registry/, guessing how to test it
    #[clap(long)]
    pub include_non_defined: bool,
    /// Directly pipe stdin/stdout/stderr from plugin to user
    /// Sets --jobs=1
    #[clap(long, overrides_with = "jobs")]
    pub raw: bool,
}

impl TestTool {
    pub async fn run(self) -> Result<()> {
        let mut errored = vec![];
        self.github_summary(vec![
            "Tool".to_string(),
            "Duration".to_string(),
            "Status".to_string(),
        ])?;
        self.github_summary(vec![
            "---".to_string(),
            "---".to_string(),
            "---".to_string(),
        ])?;
        let mut config = Config::get().await?;

        let target_tools = self.get_target_tools(&config).await?;
        let mut targets = vec![];
        for (i, (tool, rt)) in target_tools.into_iter().enumerate() {
            if *env::TEST_TRANCHE_COUNT > 0 && (i % *env::TEST_TRANCHE_COUNT) != *env::TEST_TRANCHE
            {
                continue;
            }
            let (cmd, expected) = if let Some(test) = &rt.test {
                (test.0.to_string(), test.1.to_string())
            } else if self.include_non_defined {
                (format!("{} --version", tool.short), "__TODO__".to_string())
            } else {
                continue;
            };
            targets.push(TestToolTarget {
                index: targets.len(),
                tool,
                cmd,
                expected,
            });
        }

        if self.run_in_process(targets.len()) {
            let mut cleaned_any = false;
            for target in &targets {
                cleaned_any |= self.clean(&target.tool)?;
            }
            if cleaned_any {
                config = Config::reset().await?;
            }
        }

        let results = self.test_targets(config, targets).await?;
        for result in results {
            if !result.output.is_empty() {
                miseprintln!("{}", result.output);
            }
            if let Some(err) = result.error {
                if !result.status_logged {
                    error!("{}: {}", result.tool, err);
                }
                errored.push(result.tool.clone());
                self.github_summary(vec![
                    result.tool,
                    time::format_duration(result.duration).to_string(),
                    ":x:".to_string(),
                ])?;
            } else {
                if !result.status_logged {
                    info!("{}: OK", result.tool);
                }
                self.github_summary(vec![
                    result.tool,
                    time::format_duration(result.duration).to_string(),
                    ":white_check_mark:".to_string(),
                ])?;
            };
        }
        if let Ok(github_summary) = env::var("GITHUB_STEP_SUMMARY") {
            let mut content: String = "\n".into();
            if !errored.is_empty() {
                content.push_str(&format!("**Failed Tools**: {}\n", errored.join(", ")));
            }
            file::append(github_summary, content)?;
        }
        if !errored.is_empty() {
            bail!("tools failed: {}", errored.join(", "));
        }
        Ok(())
    }

    async fn test_targets(
        &self,
        config: Arc<Config>,
        targets: Vec<TestToolTarget>,
    ) -> Result<Vec<TestToolResult>> {
        if self.run_in_process(targets.len()) {
            let mut results = vec![];
            for target in targets {
                let tool = target.tool.short.clone();
                let start = std::time::Instant::now();
                let result = match self
                    .test(&config, &target.tool, &target.cmd, &target.expected)
                    .await
                {
                    Ok(output) => TestToolResult {
                        index: target.index,
                        tool,
                        duration: start.elapsed(),
                        output,
                        error: None,
                        status_logged: false,
                    },
                    Err(err) => TestToolResult {
                        index: target.index,
                        tool,
                        duration: start.elapsed(),
                        output: String::new(),
                        error: Some(format!("{err:?}")),
                        status_logged: false,
                    },
                };
                results.push(result);
            }
            return Ok(results);
        }

        let jobs = self.jobs();
        let mut jset = JoinSet::new();
        let mut results = targets.iter().map(|_| None).collect::<Vec<_>>();

        for target in targets {
            while jset.len() >= jobs {
                Self::collect_child_result(&mut jset, &mut results).await?;
            }
            let this = self.clone();
            jset.spawn(async move {
                tokio::task::spawn_blocking(move || this.test_child(target))
                    .await
                    .map_err(|err| eyre!("task panicked: {err}"))?
            });
        }

        while !jset.is_empty() {
            Self::collect_child_result(&mut jset, &mut results).await?;
        }

        Ok(results.into_iter().flatten().collect())
    }

    async fn collect_child_result(
        jset: &mut JoinSet<Result<TestToolResult>>,
        results: &mut [Option<TestToolResult>],
    ) -> Result<()> {
        if let Some(result) = jset.join_next().await {
            let result = result??;
            let index = result.index;
            results[index] = Some(result);
        }
        Ok(())
    }

    fn test_child(&self, target: TestToolTarget) -> Result<TestToolResult> {
        let start = std::time::Instant::now();
        let exe = std::env::current_exe()?;
        let mut args = vec![
            "test-tool".to_string(),
            "--jobs=1".to_string(),
            target.tool.to_string(),
        ];
        if self.include_non_defined {
            args.insert(1, "--include-non-defined".to_string());
        }

        let res = cmd(exe, args)
            .env_remove("GITHUB_STEP_SUMMARY")
            .env_remove("TEST_TRANCHE")
            .env_remove("TEST_TRANCHE_COUNT")
            .stderr_to_stdout()
            .stdout_capture()
            .unchecked()
            .run()?;
        let output = String::from_utf8(res.stdout)?.trim_end().to_string();
        let error = match res.status.code() {
            Some(0) => None,
            Some(code) => Some(format!("command failed: exit code {code}")),
            None => Some("command failed: terminated by signal".to_string()),
        };
        Ok(TestToolResult {
            index: target.index,
            tool: target.tool.short,
            duration: start.elapsed(),
            output,
            error,
            status_logged: true,
        })
    }

    async fn get_target_tools(
        &self,
        config: &Arc<Config>,
    ) -> Result<Vec<(ToolArg, &RegistryTool)>> {
        if let Some(tools) = &self.tools {
            let mut targets = Vec::new();
            let mut not_found = Vec::new();
            for tool_arg in tools {
                if let Some(rt) = REGISTRY.get(tool_arg.short.as_str()) {
                    targets.push((tool_arg.clone(), rt));
                } else {
                    not_found.push(tool_arg.short.clone());
                }
            }
            if !not_found.is_empty() {
                bail!("tools not found: {}", not_found.join(", "));
            }
            Ok(targets)
        } else if self.all {
            REGISTRY
                .iter()
                .filter(|(short, rt)| rt.short == **short) // Filter out aliases
                .map(|(short, rt)| short.parse().map(|s| (s, rt)))
                .collect()
        } else if self.all_config {
            let ts = ToolsetBuilder::new().build(config).await?;
            let config_tools = ts
                .versions
                .keys()
                .map(|t| t.short.clone())
                .collect::<BTreeSet<_>>();
            let mut targets = Vec::new();
            for tool in config_tools {
                if let Some(rt) = REGISTRY.get(tool.as_str()) {
                    targets.push((tool.parse()?, rt));
                }
            }
            Ok(targets)
        } else {
            unreachable!()
        }
    }

    fn github_summary(&self, parts: Vec<String>) -> Result<()> {
        if let Ok(github_summary) = env::var("GITHUB_STEP_SUMMARY") {
            file::append(github_summary, format!("| {} |\n", parts.join(" | ")))?;
        }
        Ok(())
    }

    fn jobs(&self) -> usize {
        if self.raw {
            1
        } else {
            self.jobs.unwrap_or(4).max(1)
        }
    }

    fn run_in_process(&self, target_count: usize) -> bool {
        self.jobs() == 1 || target_count <= 1
    }

    fn clean(&self, tool: &ToolArg) -> Result<bool> {
        // First, clean all backend data by removing directories
        let pr = crate::ui::multi_progress_report::MultiProgressReport::get()
            .add(&format!("cleaning {}", tool.short));

        let mut cleaned_any = false;

        // Remove entire installs directory for this tool
        if tool.ba.installs_path.exists() {
            info!(
                "Removing installs directory: {}",
                tool.ba.installs_path.display()
            );
            file::remove_all(&tool.ba.installs_path)?;
            cleaned_any = true;
        }

        // Clear cache directory (contains metadata)
        if tool.ba.cache_path.exists() {
            info!("Removing cache directory: {}", tool.ba.cache_path.display());
            file::remove_all(&tool.ba.cache_path)?;
            cleaned_any = true;
        }

        // Clear downloads directory
        if tool.ba.downloads_path.exists() {
            info!(
                "Removing downloads directory: {}",
                tool.ba.downloads_path.display()
            );
            file::remove_all(&tool.ba.downloads_path)?;
            cleaned_any = true;
        }

        pr.finish();
        Ok(cleaned_any)
    }

    async fn test(
        &self,
        config: &Arc<Config>,
        tool: &ToolArg,
        cmd: &str,
        expected: &str,
    ) -> Result<String> {
        let mut config = config.clone();
        let mut args = vec![tool.clone()];
        args.extend(
            tool.ba
                .backend()?
                .get_all_dependencies(false)?
                .into_iter()
                .map(|ba| ba.to_string().parse())
                .collect::<Result<Vec<ToolArg>>>()?,
        );
        let mut ts = ToolsetBuilder::new()
            .with_args(&args)
            .with_default_to_latest(true)
            .build(&config)
            .await?;
        let opts = InstallOptions {
            missing_args_only: false,
            jobs: self.jobs,
            raw: self.raw,
            ..Default::default()
        };
        let (_, missing) = ts.install_missing_versions(&mut config, &opts).await?;
        ts.notify_missing_versions(missing);
        let tv = if let Some(tv) = ts
            .versions
            .get(tool.ba.as_ref())
            .and_then(|tvl| tvl.versions.first())
        {
            tv.clone()
        } else {
            warn!("no versions found for {tool}");
            return Ok(String::new());
        };
        let backend = tv.backend()?;
        let env = ts.env_with_path(&config).await?;
        let mut which_parts = cmd.split_whitespace().collect::<Vec<_>>();
        let cmd = which_parts.remove(0);
        let mut which_cmd = backend
            .which(&config, &tv, cmd)
            .await?
            .unwrap_or(PathBuf::from(cmd));
        if cfg!(windows) && which_cmd == Path::new("which") {
            which_cmd = PathBuf::from("where");
        }
        let cmd = format!("{} {}", which_cmd.display(), which_parts.join(" "));
        info!("$ {cmd}");
        let mut cmd = if cfg!(windows) {
            cmd!("cmd", "/C", cmd)
        } else {
            cmd!("sh", "-c", cmd)
        };
        cmd = cmd.stderr_to_stdout().stdout_capture();
        for (k, v) in env.iter() {
            cmd = cmd.env(k, v);
        }
        let res = cmd.unchecked().run()?;
        match res.status.code() {
            Some(0) => {}
            Some(code) => {
                if code == 127 {
                    // Show captured stdout (which may include stderr via 2>&1)
                    // to help diagnose dynamic linker or missing library errors
                    if let Ok(stdout) = String::from_utf8(res.stdout.clone()) {
                        let stdout = stdout.trim();
                        if !stdout.is_empty() {
                            info!("command output:\n{stdout}");
                        }
                    }
                    let bin_dirs = backend.list_bin_paths(&config, &tv).await?;
                    for bin_dir in &bin_dirs {
                        let bins = file::ls(bin_dir)?
                            .into_iter()
                            .filter(|p| file::is_executable(p))
                            .map(|p| p.file_name().unwrap().to_string_lossy().to_string())
                            .collect::<Vec<_>>();
                        info!(
                            "available bins in {}\n{}",
                            display_path(bin_dir),
                            bins.join("\n")
                        );
                    }
                }
                return Err(eyre!("command failed: exit code {}", code));
            }
            None => return Err(eyre!("command failed: terminated by signal")),
        }
        let mut ctx = config.tera_ctx.clone();
        ctx.insert("version", &tv.version);
        let mut tera = get_tera(dirs::CWD.as_ref().map(|d| d.as_path()));
        let expected = tera.render_str(expected, &ctx)?;
        let stdout = String::from_utf8(res.stdout)?;
        let clean_stdout = console::strip_ansi_codes(&stdout);
        if !clean_stdout.contains(&expected) {
            return Err(eyre!(
                "expected output not found: {expected}, got: {clean_stdout}"
            ));
        }
        Ok(stdout.trim_end().to_string())
    }
}

struct TestToolTarget {
    index: usize,
    tool: ToolArg,
    cmd: String,
    expected: String,
}

struct TestToolResult {
    index: usize,
    tool: String,
    duration: std::time::Duration,
    output: String,
    error: Option<String>,
    status_logged: bool,
}

static AFTER_LONG_HELP: &str = color_print::cstr!(
    r#"<bold><underline>Examples:</underline></bold>

    $ <bold>mise test-tool ripgrep</bold>
"#
);