mise 2026.9.1

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
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
use std::collections::{BTreeMap, HashMap, HashSet};
use std::ffi::OsString;
use std::fmt::{Debug, Formatter};
use std::fs;
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

use crate::backend::VersionInfo;
use crate::backend::backend_type::BackendType;
use crate::backend::external_plugin_cache::ExternalPluginCache;
use crate::backend::normalize_idiomatic_contents;
use crate::cache::{CacheManager, CacheManagerBuilder};
use crate::cli::args::BackendArg;
use crate::config::env_directive::EnvResults;
use crate::config::{Config, Settings};
use crate::env_diff::{EnvDiff, EnvDiffOperation, EnvMap};
use crate::hash::hash_to_str;
use crate::install_context::InstallContext;
use crate::plugins::Script::{Download, ExecEnv, Install, ParseIdiomaticFile};
use crate::plugins::asdf_plugin::AsdfPlugin;
use crate::plugins::mise_plugin_toml::MisePluginToml;
use crate::plugins::{PluginType, Script, ScriptManager};
use crate::toolset::{ToolRequest, ToolVersion, Toolset};
use crate::ui::progress_report::SingleReport;
use crate::{backend::Backend, plugins::PluginEnum, timeout};
use crate::{dirs, env, file};
use async_trait::async_trait;
use color_eyre::eyre::{Result, WrapErr, bail, eyre};
use console::style;
use heck::ToKebabCase;

/// This represents a plugin installed to ~/.local/share/mise/plugins
pub(crate) struct AsdfBackend {
    pub ba: Arc<BackendArg>,
    pub name: String,
    pub plugin_path: PathBuf,
    pub repo_url: Option<String>,
    pub toml: MisePluginToml,
    plugin: Arc<AsdfPlugin>,
    plugin_enum: PluginEnum,
    cache: ExternalPluginCache,
    latest_stable_caches: Mutex<HashMap<String, Arc<CacheManager<Option<String>>>>>,
    alias_cache: CacheManager<Vec<(String, String)>>,
    idiomatic_filename_cache: CacheManager<Vec<String>>,
}

impl AsdfBackend {
    pub(crate) fn from_arg(ba: BackendArg) -> Self {
        let name = ba.tool_name.clone();
        let plugin_path = dirs::PLUGINS.join(ba.short.to_kebab_case());
        let plugin = AsdfPlugin::new(name.clone(), plugin_path.clone());
        let mut toml_path = plugin_path.join("mise.plugin.toml");
        if plugin_path.join("rtx.plugin.toml").exists() {
            toml_path = plugin_path.join("rtx.plugin.toml");
        }
        let toml = MisePluginToml::from_file(&toml_path).unwrap();
        let plugin = Arc::new(plugin);
        let plugin_enum = PluginEnum::Asdf(plugin.clone());
        Self {
            cache: ExternalPluginCache::default(),
            latest_stable_caches: Mutex::new(HashMap::new()),
            alias_cache: CacheManagerBuilder::new(ba.cache_path.join("aliases.msgpack.z"))
                .with_fresh_file(plugin_path.clone())
                .with_fresh_file(plugin_path.join("bin/list-aliases"))
                .build(),
            idiomatic_filename_cache: CacheManagerBuilder::new(
                ba.cache_path.join("idiomatic_filenames.msgpack.z"),
            )
            .with_fresh_file(plugin_path.clone())
            .with_fresh_file(plugin_path.join("bin/list-legacy-filenames"))
            .build(),
            plugin_path,
            plugin,
            plugin_enum,
            repo_url: None,
            toml,
            name,
            ba: Arc::new(ba),
        }
    }

    fn fetch_cached_idiomatic_file(&self, idiomatic_file: &Path) -> Result<Option<String>> {
        let fp = self.idiomatic_cache_file_path(idiomatic_file);
        if !fp.exists() || fp.metadata()?.modified()? < idiomatic_file.metadata()?.modified()? {
            return Ok(None);
        }

        Ok(Self::cached_idiomatic_version(&fs::read_to_string(fp)?))
    }

    /// A cached idiomatic version, or `None` when the entry has to be parsed again.
    ///
    /// The cache holds the output of `normalize_idiomatic_contents`, and an entry written before
    /// that stripped the byte-order mark still carries it. Nothing retires such an entry on its
    /// own: it is compared only against the source file's modification time, and upgrading mise
    /// moves neither. So the mark is treated as a miss — the file is re-read through the current
    /// normaliser and the entry rewritten clean, once.
    ///
    /// A miss rather than a strip, because stripping repairs only the simpler shape. The old
    /// normaliser also failed to recognise a commented first line with a mark in front of the `#`
    /// (`starts_with('#')` is false there) and cached the comment itself as a version; only a
    /// re-parse fixes that.
    fn cached_idiomatic_version(cached: &str) -> Option<String> {
        if cached.contains('\u{feff}') {
            return None;
        }
        Some(cached.trim().to_string())
    }

    fn idiomatic_cache_file_path(&self, idiomatic_file: &Path) -> PathBuf {
        self.ba
            .cache_path
            .join("idiomatic")
            .join(&self.name)
            .join(hash_to_str(&idiomatic_file.to_string_lossy()))
            .with_extension("txt")
    }

    fn write_idiomatic_cache(&self, idiomatic_file: &Path, idiomatic_version: &str) -> Result<()> {
        let fp = self.idiomatic_cache_file_path(idiomatic_file);
        file::create_dir_all(fp.parent().unwrap())?;
        file::write(fp, idiomatic_version)?;
        Ok(())
    }

    fn version_listing_cache_context(env_results: &EnvResults) -> Option<String> {
        if env_results.env.is_empty()
            && env_results.env_remove.is_empty()
            && env_results.env_paths.is_empty()
        {
            return None;
        }
        let env = env_results
            .env
            .iter()
            .map(|(key, (value, _))| (key, value))
            .collect::<BTreeMap<_, _>>();
        Some(hash_to_str(&(
            env,
            &env_results.env_remove,
            &env_results.env_paths,
        )))
    }

    fn script_man_for_version_listing(&self, env_results: &EnvResults) -> Result<ScriptManager> {
        let mut sm = self.plugin.script_man.clone();
        for key in &env_results.env_remove {
            sm = sm.without_env(key);
        }
        for (key, (value, _)) in &env_results.env {
            sm = sm.with_env(key, value);
        }
        if !env_results.env_paths.is_empty() {
            let path_key = OsString::from(&*env::PATH_KEY);
            let current_path = sm.env.get(&path_key).cloned().unwrap_or_default();
            let mut paths = env_results.env_paths.clone();
            if !current_path.is_empty() {
                paths.extend(env::split_paths(&current_path));
            }
            sm = sm.with_env(path_key, env::join_paths(paths)?);
        }
        Ok(sm)
    }

    fn latest_stable_cache(&self, context: Option<&str>) -> Arc<CacheManager<Option<String>>> {
        let map_key = context.unwrap_or_default().to_string();
        self.latest_stable_caches
            .lock()
            .unwrap()
            .entry(map_key)
            .or_insert_with(|| {
                let mut cm =
                    CacheManagerBuilder::new(self.ba.cache_path.join("latest_stable.msgpack.z"))
                        .with_fresh_duration(Settings::get().fetch_remote_versions_cache())
                        .with_fresh_file(self.plugin_path.clone())
                        .with_fresh_file(self.plugin_path.join("bin/latest-stable"));
                if let Some(context) = context {
                    cm = cm.with_cache_key(context.to_string());
                }
                Arc::new(cm.build())
            })
            .clone()
    }

    async fn fetch_bin_paths(&self, config: &Arc<Config>, tv: &ToolVersion) -> Result<Vec<String>> {
        let list_bin_paths = self.plugin_path.join("bin/list-bin-paths");
        let bin_paths = if matches!(tv.request, ToolRequest::System { .. }) {
            Vec::new()
        } else if list_bin_paths.exists() {
            let sm = self.script_man_for_tv(config, tv).await?;
            // TODO: find a way to enable this without deadlocking
            // for (t, tv) in ts.list_current_installed_versions(config) {
            //     if t.name == self.name {
            //         continue;
            //     }
            //     for p in t.list_bin_paths(config, ts, &tv)? {
            //         sm.prepend_path(p);
            //     }
            // }
            Settings::ensure_not_safe("executing asdf plugin scripts")?;
            let output = sm.cmd(&Script::ListBinPaths).read()?;
            output
                .split_whitespace()
                .map(|f| {
                    if f == "." {
                        String::new()
                    } else {
                        f.to_string()
                    }
                })
                .collect()
        } else {
            vec!["bin".into()]
        };
        Ok(bin_paths)
    }
    async fn fetch_exec_env(
        &self,
        config: &Arc<Config>,
        ts: &Toolset,
        tv: &ToolVersion,
    ) -> Result<EnvMap> {
        let mut sm = self.script_man_for_tv(config, tv).await?;
        for p in ts.list_paths(config).await {
            sm.prepend_path(p);
        }
        let script = sm.get_script_path(&ExecEnv);
        let dir = dirs::CWD.clone().unwrap_or_default();
        let ed = EnvDiff::from_bash_script(&script, &dir, &sm.env, &Default::default())?;
        let env = ed
            .to_patches()
            .into_iter()
            .filter_map(|p| match p {
                EnvDiffOperation::Add(key, value) => Some((key, value)),
                EnvDiffOperation::Change(key, value) => Some((key, value)),
                _ => None,
            })
            .collect();
        Ok(env)
    }

    async fn script_man_for_tv(
        &self,
        config: &Arc<Config>,
        tv: &ToolVersion,
    ) -> Result<ScriptManager> {
        let mut sm = self.plugin.script_man.clone();
        for (key, value) in tv.request.options().opts_as_strings() {
            let k = format!("MISE_TOOL_OPTS__{}", key.to_uppercase());
            sm = sm.with_env(k, value);
        }
        for (key, value) in tv.install_env() {
            sm = match value.into_string() {
                Some(value) => sm.with_env(key, value),
                None => sm.without_env(key),
            };
        }
        if let Some(project_root) = &config.project_root {
            let project_root = project_root.to_string_lossy().to_string();
            sm = sm.with_env("MISE_PROJECT_ROOT", project_root);
        }
        let install_type = match &tv.request {
            ToolRequest::Version { .. } | ToolRequest::Prefix { .. } => "version",
            ToolRequest::Ref { .. } => "ref",
            ToolRequest::Path { .. } => "path",
            ToolRequest::Sub { .. } => "sub",
            ToolRequest::System { .. } => {
                panic!("should not be called for system tool")
            }
        };
        let install_version = match &tv.request {
            ToolRequest::Ref { ref_: v, .. } => v, // should not have "ref:" prefix
            _ => &tv.version,
        };
        // add env vars from mise.toml files
        for (key, value) in config.env().await? {
            sm = sm.with_env(key, value.clone());
        }
        let install = tv.install_path().to_string_lossy().to_string();
        let download = tv.download_path().to_string_lossy().to_string();
        sm = sm
            .with_env("ASDF_DOWNLOAD_PATH", &download)
            .with_env("ASDF_INSTALL_PATH", &install)
            .with_env("ASDF_INSTALL_TYPE", install_type)
            .with_env("ASDF_INSTALL_VERSION", install_version)
            .with_env("MISE_DOWNLOAD_PATH", download)
            .with_env("MISE_INSTALL_PATH", install)
            .with_env("MISE_INSTALL_TYPE", install_type)
            .with_env(env::MISE_INSTALL_VERSION_ENV_VAR, install_version);
        Ok(sm)
    }
}

impl Eq for AsdfBackend {}

impl PartialEq for AsdfBackend {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
    }
}

impl Hash for AsdfBackend {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.name.hash(state);
    }
}

#[async_trait]
impl Backend for AsdfBackend {
    fn get_type(&self) -> BackendType {
        BackendType::Asdf
    }

    fn ba(&self) -> &Arc<BackendArg> {
        &self.ba
    }

    fn get_plugin_type(&self) -> Option<PluginType> {
        Some(PluginType::Asdf)
    }

    fn mark_prereleases_from_version_pattern(&self) -> bool {
        true
    }

    /// ASDF plugins handle their own downloads through plugin scripts.
    /// Lockfile URLs are not applicable since installation is delegated to plugin scripts.
    fn supports_lockfile_url(&self) -> bool {
        false
    }

    async fn remote_version_cache_context(&self, config: &Arc<Config>) -> Result<Option<String>> {
        Ok(Self::version_listing_cache_context(
            config.env_results().await?,
        ))
    }

    async fn _list_remote_versions(&self, config: &Arc<Config>) -> Result<Vec<VersionInfo>> {
        let env_results = config.env_results().await?;
        let sm = self.script_man_for_version_listing(env_results)?;
        let versions = self.plugin.fetch_remote_versions(&sm)?;
        Ok(versions
            .into_iter()
            .map(|v| VersionInfo {
                version: v,
                ..Default::default()
            })
            .collect())
    }

    async fn latest_stable_version(&self, config: &Arc<Config>) -> Result<Option<String>> {
        let env_results = config.env_results().await?;
        let context = Self::version_listing_cache_context(env_results);
        let sm = self.script_man_for_version_listing(env_results)?;
        let cache = self.latest_stable_cache(context.as_deref());
        timeout::run_with_timeout_async(
            || async {
                if !self.plugin.has_latest_stable_script() {
                    return Ok(None);
                }
                cache
                    .get_or_try_init(|| self.plugin.fetch_latest_stable(&sm))
                    .wrap_err_with(|| {
                        eyre!(
                            "Failed fetching latest stable version for plugin {}",
                            style(&self.name).blue().for_stderr(),
                        )
                    })
                    .cloned()
            },
            Settings::get().fetch_remote_versions_timeout(),
        )
        .await
    }

    fn get_aliases(&self) -> Result<BTreeMap<String, String>> {
        if let Some(data) = &self.toml.list_aliases.data {
            return Ok(self.plugin.parse_aliases(data).into_iter().collect());
        }
        if !self.plugin.has_list_alias_script() {
            return Ok(BTreeMap::new());
        }
        let aliases = self
            .alias_cache
            .get_or_try_init(|| self.plugin.fetch_aliases())
            .wrap_err_with(|| {
                eyre!(
                    "Failed fetching aliases for plugin {}",
                    style(&self.name).blue().for_stderr(),
                )
            })?
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect();
        Ok(aliases)
    }

    async fn _idiomatic_filenames(&self) -> Result<Vec<String>> {
        if let Some(data) = &self.toml.list_idiomatic_filenames.data {
            return Ok(self.plugin.parse_idiomatic_filenames(data));
        }
        if !self.plugin.has_list_idiomatic_filenames_script() {
            return Ok(vec![]);
        }
        self.idiomatic_filename_cache
            .get_or_try_init(|| self.plugin.fetch_idiomatic_filenames())
            .wrap_err_with(|| {
                eyre!(
                    "Failed fetching idiomatic filenames for plugin {}",
                    style(&self.name).blue().for_stderr(),
                )
            })
            .cloned()
    }

    async fn _parse_idiomatic_file(&self, idiomatic_file: &Path) -> Result<Vec<String>> {
        if let Some(cached) = self.fetch_cached_idiomatic_file(idiomatic_file)? {
            return Ok(cached.split_whitespace().map(|s| s.to_string()).collect());
        }
        trace!(
            "parsing idiomatic file: {}",
            idiomatic_file.to_string_lossy()
        );
        let script = ParseIdiomaticFile(idiomatic_file.to_string_lossy().into());
        let idiomatic_version = match self.plugin.script_man.script_exists(&script) {
            true => self.plugin.script_man.read(&script)?,
            false => fs::read_to_string(idiomatic_file)?,
        }
        .to_string();
        let idiomatic_version = normalize_idiomatic_contents(&idiomatic_version);

        self.write_idiomatic_cache(idiomatic_file, &idiomatic_version)?;
        if idiomatic_version.is_empty() {
            return Ok(vec![]);
        }
        Ok(idiomatic_version
            .split_whitespace()
            .map(|s| s.to_string())
            .collect())
    }

    fn plugin(&self) -> Option<&PluginEnum> {
        Some(&self.plugin_enum)
    }

    async fn install_version_(&self, ctx: &InstallContext, tv: ToolVersion) -> Result<ToolVersion> {
        let mut sm = self.script_man_for_tv(&ctx.config, &tv).await?;

        // Resolve this tool's declared dependencies separately so asdf install scripts
        // can execute them on the first install. Do not expose unrelated active tools:
        // install requirements must be declared or already available on the script's
        // ambient PATH.
        let dependency_paths = ctx.dependency_context(&tv.request).await?.paths.clone();
        let mut seen = HashSet::new();
        let paths: Vec<_> = dependency_paths
            .into_iter()
            .filter(|path| seen.insert(path.clone()))
            .collect();
        for p in paths.into_iter().rev() {
            sm.prepend_path(p);
        }

        let run_script = |script| sm.run_by_line(script, ctx.pr.as_ref());

        if sm.script_exists(&Download) {
            ctx.pr.set_message("bin/download".into());
            run_script(&Download)?;
        }
        ctx.pr.set_message("bin/install".into());
        run_script(&Install)?;
        verify_install_script_output(&self.ba.short, &tv.install_path())?;
        file::remove_dir(&self.ba.downloads_path)?;

        Ok(tv)
    }

    async fn uninstall_version_impl(
        &self,
        config: &Arc<Config>,
        pr: &dyn SingleReport,
        tv: &ToolVersion,
    ) -> Result<()> {
        if self.plugin_path.join("bin/uninstall").exists() {
            self.script_man_for_tv(config, tv)
                .await?
                .run_by_line(&Script::Uninstall, pr)?;
        }
        Ok(())
    }

    async fn list_bin_paths(&self, config: &Arc<Config>, tv: &ToolVersion) -> Result<Vec<PathBuf>> {
        let runtime_path = tv.runtime_path();
        Ok(self
            .cache
            .list_bin_paths(config, self, tv, async || {
                self.fetch_bin_paths(config, tv).await
            })
            .await?
            .into_iter()
            .map(|path| runtime_path.join(path))
            .collect())
    }

    async fn exec_env(
        &self,
        config: &Arc<Config>,
        ts: &Toolset,
        tv: &ToolVersion,
    ) -> eyre::Result<EnvMap> {
        let total_start = std::time::Instant::now();
        if matches!(tv.request, ToolRequest::System { .. }) {
            return Ok(BTreeMap::new());
        }
        if !self.plugin.script_man.script_exists(&ExecEnv) || *env::__MISE_SCRIPT {
            // if the script does not exist, or we're already running from within a script,
            // the second is to prevent infinite loops
            return Ok(BTreeMap::new());
        }
        let res = self
            .cache
            .exec_env(config, self, tv, async || {
                self.fetch_exec_env(config, ts, tv).await
            })
            .await;
        trace!(
            "exec_env cache.get_or_try_init_async for {} finished in {}ms",
            self.name,
            total_start.elapsed().as_millis()
        );
        res
    }
}

/// Verifies that `bin/install` actually put something in `$ASDF_INSTALL_PATH`.
///
/// `bin/install` is a plugin-supplied shell script, and one that installs nothing can still exit 0
/// — a missing `set -e`, a build that fails inside a pipeline, a download that 404s. mise would
/// otherwise report `✓ installed`, record the version in install state, and keep resolving to an
/// empty directory afterwards (#5288).
///
/// Emptiness is exact rather than a guess: `Backend::create_install_dirs` recreates the install
/// path immediately before the script runs, and the `incomplete` marker lives under the cache dir,
/// so anything present afterwards came from the plugin. It is also all that can be checked — asdf
/// plugins install into `bin/`, `libexec/`, an unpacked tarball root, or wherever the tool puts
/// things, so a stricter test (spm requires an executable in `bin/`) would reject working plugins.
///
/// There is no `system` case to exclude: anything reaching here has already been through
/// `script_man_for_tv`, which panics for `ToolRequest::System`.
fn verify_install_script_output(tool: &str, install_path: &Path) -> Result<()> {
    if file::ls(install_path)?.is_empty() {
        bail!(
            "{tool}'s bin/install exited successfully but installed nothing into {}; check that the plugin installs into $ASDF_INSTALL_PATH and that it fails when the build fails",
            file::display_path(install_path)
        );
    }
    Ok(())
}

impl Debug for AsdfBackend {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AsdfPlugin")
            .field("name", &self.name)
            .field("plugin_path", &self.plugin_path)
            .field("cache_path", &self.ba.cache_path)
            .field("downloads_path", &self.ba.downloads_path)
            .field("installs_path", &self.ba.installs_path)
            .field("repo_url", &self.repo_url)
            .finish()
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    use std::ffi::OsString;

    #[test]
    fn an_idiomatic_cache_entry_written_before_the_bom_was_stripped_is_a_miss() {
        assert_eq!(
            AsdfBackend::cached_idiomatic_version("20.0.0\n"),
            Some("20.0.0".to_string())
        );
        // The mark is what `normalize_idiomatic_contents` used to leave in the cache. Modification
        // time is the entry's only invalidation and upgrading mise moves neither file's, so
        // without this the stale version outlives the fix indefinitely.
        assert_eq!(
            AsdfBackend::cached_idiomatic_version("\u{feff}20.0.0\n"),
            None
        );
        // The shape a strip on read would not repair: with the mark in front, `starts_with('#')`
        // was false, so the old normaliser cached the comment line as if it were a version.
        assert_eq!(
            AsdfBackend::cached_idiomatic_version("\u{feff}# only a comment\n3.12.0"),
            None
        );
        // Nothing else changes: the trim the cache read has always done still happens.
        assert_eq!(
            AsdfBackend::cached_idiomatic_version("  3.12.0  \n"),
            Some("3.12.0".to_string())
        );
    }

    #[test]
    fn test_verify_install_script_output() {
        let temp = tempfile::tempdir().unwrap();
        let install_path = temp.path().join("1.0.0");

        // a missing install path is the same failure as an empty one: nothing was installed
        let err = verify_install_script_output("tiny", &install_path)
            .unwrap_err()
            .to_string();
        assert!(err.contains("installed nothing into"), "{err}");
        std::fs::create_dir_all(&install_path).unwrap();
        assert!(verify_install_script_output("tiny", &install_path).is_err());

        // anything at all is enough — deliberately not an executable and not `bin/`, because asdf
        // plugins choose their own layout and this must not become spm's stricter check
        std::fs::create_dir(install_path.join("libexec")).unwrap();
        verify_install_script_output("tiny", &install_path).unwrap();
    }

    fn env_results(entries: &[(&str, &str)], removals: &[&str], paths: &[&str]) -> EnvResults {
        let mut results = EnvResults::default();
        for (key, value) in entries {
            results.env.insert(
                (*key).to_string(),
                ((*value).to_string(), PathBuf::from("mise.toml")),
            );
        }
        results.env_remove = removals.iter().map(|key| (*key).to_string()).collect();
        results.env_paths = paths.iter().map(PathBuf::from).collect();
        results
    }

    #[test]
    fn version_listing_cache_context_is_stable_and_tracks_changes() {
        let first = env_results(
            &[("TOKEN", "secret"), ("CHANNEL", "stable")],
            &["REMOVE_ME"],
            &["/first/bin"],
        );
        let reordered = env_results(
            &[("CHANNEL", "stable"), ("TOKEN", "secret")],
            &["REMOVE_ME"],
            &["/first/bin"],
        );
        let changed = env_results(
            &[("TOKEN", "other"), ("CHANNEL", "stable")],
            &["REMOVE_ME"],
            &["/first/bin"],
        );
        let changed_removal = env_results(
            &[("TOKEN", "secret"), ("CHANNEL", "stable")],
            &["OTHER"],
            &["/first/bin"],
        );
        let changed_path = env_results(
            &[("TOKEN", "secret"), ("CHANNEL", "stable")],
            &["REMOVE_ME"],
            &["/other/bin"],
        );

        assert_eq!(
            AsdfBackend::version_listing_cache_context(&first),
            AsdfBackend::version_listing_cache_context(&reordered)
        );
        assert_ne!(
            AsdfBackend::version_listing_cache_context(&first),
            AsdfBackend::version_listing_cache_context(&changed)
        );
        assert_ne!(
            AsdfBackend::version_listing_cache_context(&first),
            AsdfBackend::version_listing_cache_context(&changed_removal)
        );
        assert_ne!(
            AsdfBackend::version_listing_cache_context(&first),
            AsdfBackend::version_listing_cache_context(&changed_path)
        );
        assert_eq!(
            AsdfBackend::version_listing_cache_context(&EnvResults::default()),
            None
        );
    }

    #[test]
    fn version_listing_script_manager_applies_config_env() {
        let backend = AsdfBackend::from_arg("dummy".into());
        let results = env_results(
            &[("MISE_TEST_VERSION_CHANNEL", "private")],
            &["REMOVE_ME"],
            &["/first/bin", "/second/bin"],
        );

        let sm = backend.script_man_for_version_listing(&results).unwrap();

        assert_eq!(
            sm.env.get(&OsString::from("MISE_TEST_VERSION_CHANNEL")),
            Some(&OsString::from("private"))
        );
        assert!(!sm.env.contains_key(&OsString::from("REMOVE_ME")));
        let paths = env::split_paths(sm.env.get(&OsString::from(&*env::PATH_KEY)).unwrap())
            .collect::<Vec<_>>();
        assert_eq!(
            &paths[..2],
            &[PathBuf::from("/first/bin"), PathBuf::from("/second/bin")]
        );
    }

    #[test]
    fn version_listing_script_manager_can_add_paths_after_path_removal() {
        let backend = AsdfBackend::from_arg("dummy".into());
        let results = env_results(&[], &["PATH"], &["/only/bin"]);

        let sm = backend.script_man_for_version_listing(&results).unwrap();
        let paths = env::split_paths(sm.env.get(&OsString::from(&*env::PATH_KEY)).unwrap())
            .collect::<Vec<_>>();

        assert_eq!(paths, vec![PathBuf::from("/only/bin")]);
    }

    #[tokio::test]
    async fn test_debug() {
        let _config = Config::get().await.unwrap();
        let plugin = AsdfBackend::from_arg("dummy".into());
        assert!(format!("{plugin:?}").starts_with("AsdfPlugin { name: \"dummy\""));
    }
}