mise 2026.4.11

The front-end to your dev env
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
use std::fmt::{Display, Formatter};
use std::fs;
use std::hash::{Hash, Hasher};
use std::path::PathBuf;
use std::{cmp::Ordering, sync::LazyLock};
use std::{collections::BTreeMap, sync::Arc};

use crate::backend::{ABackend, VersionInfo};
use crate::cli::args::BackendArg;
use crate::config::{Config, Settings};
use crate::env;
#[cfg(windows)]
use crate::file;
use crate::hash::hash_to_str;
use crate::lockfile::{CondaPackageInfo, LockfileTool, PlatformInfo};
use crate::toolset::{ToolRequest, ToolSource, ToolVersionOptions, tool_request};
use console::style;
use dashmap::DashMap;
use eyre::{Result, bail};
use jiff::Timestamp;
#[cfg(windows)]
use path_absolutize::Absolutize;

static INSTALL_PATH_CACHE: LazyLock<DashMap<ToolVersion, PathBuf>> = LazyLock::new(DashMap::new);

/// Clear the install_path cache. Called when install state is reset
/// to avoid stale paths (e.g. shared dir paths after a new install).
pub fn reset_install_path_cache() {
    INSTALL_PATH_CACHE.clear();
}

/// represents a single version of a tool for a particular plugin
#[derive(Debug, Clone)]
pub struct ToolVersion {
    pub request: ToolRequest,
    pub version: String,
    pub lock_platforms: BTreeMap<String, PlatformInfo>,
    pub install_path: Option<PathBuf>,
    /// Conda packages resolved during installation: (platform, basename) -> CondaPackageInfo
    pub conda_packages: BTreeMap<(String, String), CondaPackageInfo>,
}

impl ToolVersion {
    pub fn new(request: ToolRequest, version: String) -> Self {
        ToolVersion {
            request,
            version,
            lock_platforms: Default::default(),
            install_path: None,
            conda_packages: Default::default(),
        }
    }

    pub async fn resolve(
        config: &Arc<Config>,
        request: ToolRequest,
        opts: &ResolveOptions,
    ) -> Result<Self> {
        trace!("resolving {} {}", &request, opts);
        if opts.use_locked_version
            && !has_linked_version(request.ba())
            && let Some(lt) = request.lockfile_resolve(config)?
        {
            return Ok(Self::from_lockfile(request.clone(), lt));
        }
        let backend = request.ba().backend()?;
        if let Some(plugin) = backend.plugin()
            && !plugin.is_installed()
        {
            let tv = Self::new(request.clone(), request.version());
            return Ok(tv);
        }
        let tv = match request.clone() {
            ToolRequest::Version { version: v, .. } => {
                Self::resolve_version(config, request, &v, opts).await?
            }
            ToolRequest::Prefix { prefix, .. } => {
                Self::resolve_prefix(config, request, &prefix, opts).await?
            }
            ToolRequest::Sub {
                sub, orig_version, ..
            } => Self::resolve_sub(config, request, &sub, &orig_version, opts).await?,
            _ => {
                let version = request.version();
                Self::new(request, version)
            }
        };
        trace!("resolved: {tv}");
        Ok(tv)
    }

    fn from_lockfile(request: ToolRequest, lt: LockfileTool) -> Self {
        let mut tv = Self::new(request, lt.version);
        tv.lock_platforms = lt.platforms;
        tv
    }

    pub fn ba(&self) -> &BackendArg {
        self.request.ba()
    }

    pub fn backend(&self) -> Result<ABackend> {
        self.ba().backend()
    }

    pub fn short(&self) -> &str {
        &self.ba().short
    }

    pub fn install_path(&self) -> PathBuf {
        if let Some(p) = &self.install_path {
            return p.clone();
        }
        if let Some(p) = INSTALL_PATH_CACHE.get(self) {
            return p.clone();
        }
        let pathname = match &self.request {
            ToolRequest::Path { path: p, .. } => p.to_string_lossy().to_string(),
            _ => self.tv_pathname(),
        };
        let path = self.ba().installs_path.join(&pathname);

        // handle non-symlinks on windows
        // TODO: make this a utility function in xx
        #[cfg(windows)]
        if path.is_file() {
            if let Ok(p) = file::read_to_string(&path).map(PathBuf::from) {
                let path = self.ba().installs_path.join(p);
                if path.exists() {
                    return path
                        .absolutize()
                        .expect("failed to absolutize path")
                        .to_path_buf();
                }
            }
        }

        // Check shared install directories if the primary path doesn't exist
        let path = if matches!(&self.request, ToolRequest::Path { .. }) {
            path
        } else {
            env::find_in_shared_installs(path, &self.ba().tool_dir_name(), &pathname)
        };

        INSTALL_PATH_CACHE.insert(self.clone(), path.clone());
        path
    }
    pub fn cache_path(&self) -> PathBuf {
        self.ba().cache_path.join(self.tv_pathname())
    }
    pub fn download_path(&self) -> PathBuf {
        self.request.ba().downloads_path.join(self.tv_pathname())
    }
    pub async fn latest_version(&self, config: &Arc<Config>) -> Result<String> {
        self.latest_version_with_opts(config, &ResolveOptions::default())
            .await
    }

    pub async fn latest_version_with_opts(
        &self,
        config: &Arc<Config>,
        base_opts: &ResolveOptions,
    ) -> Result<String> {
        // Note: We always use latest_versions=true and use_locked_version=false for latest version lookup,
        // but we preserve before_date from base_opts to respect date-based filtering
        let opts = ResolveOptions {
            latest_versions: true,
            use_locked_version: false,
            before_date: base_opts.before_date,
        };
        let tv = self.request.resolve(config, &opts).await?;
        // map cargo backend specific prefixes to ref
        let version = match tv.request.version().split_once(':') {
            Some((_ref_type @ ("tag" | "branch" | "rev"), r)) => {
                format!("ref:{r}")
            }
            _ => tv.version,
        };
        Ok(version)
    }
    pub fn style(&self) -> String {
        format!(
            "{}{}",
            style(&self.ba().short).blue().for_stderr(),
            style(&format!("@{}", &self.version)).for_stderr()
        )
    }
    pub fn tv_pathname(&self) -> String {
        match &self.request {
            ToolRequest::Version { .. } => self.version.to_string(),
            ToolRequest::Prefix { .. } => self.version.to_string(),
            ToolRequest::Sub { .. } => self.version.to_string(),
            ToolRequest::Ref { ref_: r, .. } => format!("ref-{r}"),
            ToolRequest::Path { path: p, .. } => format!("path-{}", hash_to_str(p)),
            ToolRequest::System { .. } => {
                // Only show deprecation warning if not from .tool-versions file
                if !matches!(
                    self.request.source(),
                    crate::toolset::ToolSource::ToolVersions(_)
                ) {
                    deprecated!(
                        "system_tool_version",
                        "@system is deprecated, use MISE_DISABLE_TOOLS instead"
                    );
                }
                "system".to_string()
            }
        }
        .replace([':', '/'], "-")
    }
    async fn resolve_version(
        config: &Arc<Config>,
        request: ToolRequest,
        v: &str,
        opts: &ResolveOptions,
    ) -> Result<ToolVersion> {
        let backend = request.backend()?;
        let v = config.resolve_alias(&backend, v).await?;

        // Re-check the lockfile after alias resolution (e.g., "lts" → "24")
        // The initial lockfile check in resolve() uses the unresolved alias which
        // won't match lockfile entries like "24.13.0".starts_with("lts")
        if opts.use_locked_version
            && !has_linked_version(request.ba())
            && let Some(lt) = request.lockfile_resolve_with_prefix(config, &v)?
        {
            return Ok(Self::from_lockfile(request.clone(), lt));
        }
        let settings = Settings::get();
        if settings.locked
            && opts.use_locked_version
            && settings.lockfile_enabled()
            && !has_linked_version(request.ba())
            && request.source().path().is_some()
        {
            bail!(
                "{}@{} is not in the lockfile\nhint: Run `mise install` without --locked to update the lockfile",
                request.ba().short,
                request.version()
            );
        }

        match v.split_once(':') {
            Some((ref_type @ ("ref" | "tag" | "branch" | "rev"), r)) => {
                return Ok(Self::resolve_ref(
                    r.to_string(),
                    ref_type.to_string(),
                    request.options(),
                    &request,
                ));
            }
            Some(("path", p)) => {
                return Self::resolve_path(PathBuf::from(p), &request);
            }
            Some(("prefix", p)) => {
                return Self::resolve_prefix(config, request, p, opts).await;
            }
            Some((part, v)) if part.starts_with("sub-") => {
                let sub = part.split_once('-').unwrap().1;
                return Self::resolve_sub(config, request, sub, v, opts).await;
            }
            _ => (),
        }

        let build = |v| Ok(Self::new(request.clone(), v));

        if let Some(plugin) = backend.plugin()
            && !plugin.is_installed()
        {
            return build(v);
        }

        let settings = Settings::get();
        let is_offline = settings.offline();

        if v == "latest" {
            if !opts.latest_versions
                && let Some(v) = backend.latest_installed_version(None)?
            {
                return build(v);
            }
            if !is_offline
                && let Some(v) = backend
                    .latest_version_with_opts(config, None, opts.before_date)
                    .await?
            {
                return build(v);
            }
        }
        if !opts.latest_versions {
            let matches = backend.list_installed_versions_matching(&v);
            if matches.contains(&v) {
                return build(v);
            }
            if let Some(v) = matches.last() {
                return build(v.clone());
            }
        }
        if matches!(
            request.source(),
            ToolSource::IdiomaticVersionFile(path)
                if crate::config::config_file::idiomatic_version::package_json::is_package_json(path)
        ) && crate::semver::is_npm_semver_range_query(&v)
        {
            if !opts.latest_versions {
                let installed_versions = backend.list_installed_versions();
                if let Some(matches) =
                    crate::semver::npm_semver_range_filter(&installed_versions, &v)
                    && let Some(v) = matches.last()
                {
                    return build(v.clone());
                }
            }
            if !is_offline {
                let versions = match opts.before_date {
                    Some(before) => {
                        let versions_with_info =
                            backend.list_remote_versions_with_info(config).await?;
                        VersionInfo::filter_by_date(versions_with_info, before)
                            .into_iter()
                            .map(|v| v.version)
                            .collect()
                    }
                    None => backend.list_remote_versions(config).await?,
                };
                if let Some(matches) = crate::semver::npm_semver_range_filter(&versions, &v)
                    && let Some(v) = matches.last()
                {
                    return build(v.clone());
                }
            }
        }
        // When OFFLINE, skip ALL remote version fetching regardless of version format
        if is_offline {
            return build(v);
        }
        // In prefer-offline mode (hook-env, activate, exec), skip remote version
        // fetching for fully-qualified versions (e.g. "2.3.2") that aren't installed.
        // Prefix versions like "2" still need remote resolution to find e.g. "2.1.0".
        // "latest" also needs remote resolution but is handled in the block above.
        if settings.prefer_offline() && v.matches('.').count() >= 2 {
            return build(v);
        }
        // First try with date filter (common case)
        let matches = backend
            .list_versions_matching_with_opts(config, &v, opts.before_date)
            .await?;
        if matches.contains(&v) {
            return build(v);
        }
        // If date filter is active and exact version not found, check without filter.
        // Explicit pinned versions like "22.5.0" should not be filtered by date.
        if opts.before_date.is_some() {
            let all_versions = backend.list_versions_matching(config, &v).await?;
            if all_versions.contains(&v) {
                // Exact match exists but was filtered by date - use it anyway
                return build(v);
            }
        }
        Self::resolve_prefix(config, request, &v, opts).await
    }

    /// resolve a version like `sub-1:12.0.0` which becomes `11.0.0`, `sub-0.1:12.1.0` becomes `12.0.0`
    async fn resolve_sub(
        config: &Arc<Config>,
        request: ToolRequest,
        sub: &str,
        v: &str,
        opts: &ResolveOptions,
    ) -> Result<Self> {
        let backend = request.backend()?;
        let v = match v {
            "latest" => backend
                .latest_version_with_opts(config, None, opts.before_date)
                .await?
                .ok_or_else(|| {
                    let msg = if opts.before_date.is_some() {
                        format!(
                            "no versions found for {} matching date filter",
                            backend.id()
                        )
                    } else {
                        format!("no versions found for {}", backend.id())
                    };
                    eyre::eyre!(msg)
                })?,
            _ => config.resolve_alias(&backend, v).await?,
        };
        let v = tool_request::version_sub(&v, sub);
        Box::pin(Self::resolve_version(config, request, &v, opts)).await
    }

    async fn resolve_prefix(
        config: &Arc<Config>,
        request: ToolRequest,
        prefix: &str,
        opts: &ResolveOptions,
    ) -> Result<Self> {
        let backend = request.backend()?;
        if !opts.latest_versions
            && let Some(v) = backend.list_installed_versions_matching(prefix).last()
        {
            return Ok(Self::new(request, v.to_string()));
        }
        let matches = backend
            .list_versions_matching_with_opts(config, prefix, opts.before_date)
            .await?;
        let v = match matches.last() {
            Some(v) => v,
            None => prefix,
            // None => Err(VersionNotFound(plugin.name.clone(), prefix.to_string()))?,
        };
        Ok(Self::new(request, v.to_string()))
    }

    fn resolve_ref(
        ref_: String,
        ref_type: String,
        opts: ToolVersionOptions,
        tr: &ToolRequest,
    ) -> Self {
        let request = ToolRequest::Ref {
            backend: tr.ba().clone(),
            ref_,
            ref_type,
            options: opts.clone(),
            source: tr.source().clone(),
        };
        let version = request.version();
        Self::new(request, version)
    }

    fn resolve_path(path: PathBuf, tr: &ToolRequest) -> Result<ToolVersion> {
        let path = fs::canonicalize(path)?;
        let request = ToolRequest::Path {
            backend: tr.ba().clone(),
            path,
            source: tr.source().clone(),
            options: tr.options().clone(),
        };
        let version = request.version();
        Ok(Self::new(request, version))
    }
}

impl Display for ToolVersion {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}@{}", &self.ba().full(), &self.version)
    }
}

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

impl Eq for ToolVersion {}

impl PartialOrd for ToolVersion {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for ToolVersion {
    fn cmp(&self, other: &Self) -> Ordering {
        match self.request.ba().as_ref().cmp(other.ba()) {
            Ordering::Equal => self.version.cmp(&other.version),
            o => o,
        }
    }
}

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

#[derive(Debug, Clone)]
pub struct ResolveOptions {
    pub latest_versions: bool,
    pub use_locked_version: bool,
    /// Only consider versions released before this timestamp
    pub before_date: Option<Timestamp>,
}

impl Default for ResolveOptions {
    fn default() -> Self {
        Self {
            latest_versions: false,
            use_locked_version: true,
            before_date: None,
        }
    }
}

/// Check if a tool has any user-linked versions (created by `mise link`).
/// A linked version is an installed version whose path is a symlink to an absolute path,
/// as opposed to runtime symlinks which point to relative paths (starting with "./").
fn has_linked_version(ba: &BackendArg) -> bool {
    let installs_dir = &ba.installs_path;
    let Ok(entries) = std::fs::read_dir(installs_dir) else {
        return false;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if let Ok(Some(target)) = crate::file::resolve_symlink(&path) {
            // Runtime symlinks start with "./" (e.g., latest -> ./1.35.0)
            // User-linked symlinks point to absolute paths (e.g., brew -> /opt/homebrew/opt/hk)
            if target.is_absolute() {
                return true;
            }
        }
    }
    false
}

impl Display for ResolveOptions {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        let mut opts = vec![];
        if self.latest_versions {
            opts.push("latest_versions".to_string());
        }
        if self.use_locked_version {
            opts.push("use_locked_version".to_string());
        }
        if let Some(ts) = &self.before_date {
            opts.push(format!("before_date={ts}"));
        }
        write!(f, "({})", opts.join(", "))
    }
}