Skip to main content

aube_runtime/
discover.rs

1//! Discovery of already-installed Node versions: aube's own runtime
2//! dir, mise's installs dir (read-only), and the `node` on PATH.
3
4use crate::paths;
5use std::collections::BTreeMap;
6use std::path::{Path, PathBuf};
7
8/// Where an installed Node came from.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum InstallOrigin {
11    Aube,
12    Mise,
13}
14
15impl InstallOrigin {
16    pub fn label(self) -> &'static str {
17        match self {
18            InstallOrigin::Aube => aube_util::embedder().name,
19            InstallOrigin::Mise => "mise",
20        }
21    }
22}
23
24/// A validated on-disk Node install.
25#[derive(Debug, Clone)]
26pub struct InstalledNode {
27    pub version: node_semver::Version,
28    pub install_dir: PathBuf,
29    /// The directory to prepend to PATH: `<dir>/bin` on unix, the
30    /// install dir itself on Windows (node.exe sits at the root).
31    pub bin_dir: PathBuf,
32    pub node_bin: PathBuf,
33    pub origin: InstallOrigin,
34}
35
36/// List every valid installed Node version across aube's runtime dir
37/// and mise's installs dir. When both have the same version, aube's
38/// copy wins (deterministic, and it's the copy aube can manage).
39pub fn list_installed() -> Vec<InstalledNode> {
40    let mut by_version: BTreeMap<node_semver::Version, InstalledNode> = BTreeMap::new();
41    // Insert mise first so aube entries overwrite on version collision.
42    if let Some(dir) = mise_node_installs_dir() {
43        for node in scan_install_dir(&dir, InstallOrigin::Mise) {
44            by_version.insert(node.version.clone(), node);
45        }
46    }
47    if let Some(dir) = paths::runtime_dir() {
48        for node in scan_install_dir(&dir, InstallOrigin::Aube) {
49            by_version.insert(node.version.clone(), node);
50        }
51    }
52    by_version.into_values().collect()
53}
54
55/// mise's node installs directory.
56pub fn mise_node_installs_dir() -> Option<PathBuf> {
57    mise_tool_installs_dir("node")
58}
59
60/// mise's installs directory for one tool:
61/// `$MISE_INSTALLS_DIR || $MISE_DATA_DIR/installs || ~/.local/share/mise/installs`,
62/// plus the tool segment. mise uses `~/.local/share` on every OS.
63pub fn mise_tool_installs_dir(tool: &str) -> Option<PathBuf> {
64    let installs = if let Some(dir) = std::env::var_os("MISE_INSTALLS_DIR") {
65        PathBuf::from(dir)
66    } else if let Some(dir) = std::env::var_os("MISE_DATA_DIR") {
67        PathBuf::from(dir).join("installs")
68    } else {
69        let data_home = aube_util::env::xdg_data_home()
70            .or_else(|| aube_util::env::home_dir().map(|h| h.join(".local/share")))?;
71        data_home.join("mise/installs")
72    };
73    Some(installs.join(tool))
74}
75
76/// Scan one installs root (dir-per-version) and validate each entry:
77/// the dir name must parse as a version, symlinks are skipped (mise's
78/// `latest` / `lts` / `20` aliases are symlinks — including them would
79/// double-count), an in-progress install (mise's `incomplete` marker
80/// file) is skipped, and the node binary must exist.
81fn scan_install_dir(root: &Path, origin: InstallOrigin) -> Vec<InstalledNode> {
82    let Ok(entries) = std::fs::read_dir(root) else {
83        return Vec::new();
84    };
85    let mut out = Vec::new();
86    for entry in entries.flatten() {
87        let path = entry.path();
88        let Ok(file_type) = entry.file_type() else {
89            continue;
90        };
91        if !file_type.is_dir() {
92            // Skips both files and symlinked alias dirs:
93            // `DirEntry::file_type` does not follow symlinks.
94            continue;
95        }
96        let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
97            continue;
98        };
99        let Ok(version) = node_semver::Version::parse(name.trim_start_matches('v')) else {
100            continue;
101        };
102        if let Some(node) = validate_install(&path, version, origin) {
103            out.push(node);
104        }
105    }
106    out
107}
108
109/// Validate a single version dir and compute its bin paths. Public to
110/// the crate so the installer can re-check a freshly-published dir
111/// (or one mise just created) through the exact same rules.
112pub(crate) fn validate_install(
113    dir: &Path,
114    version: node_semver::Version,
115    origin: InstallOrigin,
116) -> Option<InstalledNode> {
117    if dir.join("incomplete").exists() {
118        return None;
119    }
120    let (bin_dir, node_bin) = node_paths_in(dir);
121    if !is_executable_file(&node_bin) {
122        return None;
123    }
124    Some(InstalledNode {
125        version,
126        install_dir: dir.to_path_buf(),
127        bin_dir,
128        node_bin,
129        origin,
130    })
131}
132
133/// A regular file with execute permission (any bit — discovery never
134/// knows which uid will spawn it). Windows has no exec bit; existence
135/// suffices there. Guards against a corrupt or partially-written
136/// install entering the candidate set and only failing at spawn time.
137pub(crate) fn is_executable_file(path: &Path) -> bool {
138    let Ok(meta) = std::fs::metadata(path) else {
139        return false;
140    };
141    #[cfg(unix)]
142    {
143        use std::os::unix::fs::PermissionsExt;
144        meta.is_file() && meta.permissions().mode() & 0o111 != 0
145    }
146    #[cfg(not(unix))]
147    meta.is_file()
148}
149
150/// The platform's `node` executable file name: `node.exe` on Windows,
151/// `node` everywhere else. Single source of truth for the spots that
152/// build or look up the node binary by name.
153pub(crate) const fn node_exe_name() -> &'static str {
154    if cfg!(windows) { "node.exe" } else { "node" }
155}
156
157/// Per-OS layout of a native Node install: unix puts `node` under
158/// `bin/`, Windows puts `node.exe` at the root (mise mirrors both).
159pub(crate) fn node_paths_in(dir: &Path) -> (PathBuf, PathBuf) {
160    let exe_name = node_exe_name();
161    // Windows zips put node.exe at the archive root; mise (and some
162    // mirror layouts) use bin\node.exe instead. Prefer the root copy
163    // when it exists, otherwise fall through to the shared bin/ layout
164    // used on every OS.
165    if cfg!(windows) {
166        let root_exe = dir.join(exe_name);
167        if root_exe.is_file() {
168            return (dir.to_path_buf(), root_exe);
169        }
170    }
171    let bin = dir.join("bin");
172    let exe = bin.join(exe_name);
173    (bin, exe)
174}
175
176/// Find `node` on PATH and probe its version (`node --version`).
177/// Memoized for the process: one spawn no matter how many resolution
178/// calls happen.
179pub fn probe_path_node() -> Option<(node_semver::Version, PathBuf)> {
180    static PROBED: std::sync::OnceLock<Option<(node_semver::Version, PathBuf)>> =
181        std::sync::OnceLock::new();
182    PROBED.get_or_init(probe_path_node_uncached).clone()
183}
184
185fn probe_path_node_uncached() -> Option<(node_semver::Version, PathBuf)> {
186    let exe = find_on_path(node_exe_name())?;
187    let output = std::process::Command::new(&exe)
188        .arg("--version")
189        .output()
190        .ok()?;
191    if !output.status.success() {
192        return None;
193    }
194    let raw = String::from_utf8(output.stdout).ok()?;
195    let version = node_semver::Version::parse(raw.trim().trim_start_matches('v')).ok()?;
196    Some((version, exe))
197}
198
199/// Locate a `node` executable on `PATH` without spawning it (unlike
200/// [`probe_path_node`], which runs `node --version`). Cheap enough to
201/// call on the hot install/run path to populate `npm_node_execpath` /
202/// `NODE` for lifecycle scripts when no runtime switch is active.
203/// Returns the first `node` (`node.exe` on Windows) on `PATH` as an
204/// absolute, executable path, or `None` if none qualifies.
205pub fn node_on_path() -> Option<PathBuf> {
206    let node = find_on_path(node_exe_name())?;
207    // `npm_node_execpath` / `NODE` are contracted to be an absolute,
208    // runnable node. `find_on_path` only guarantees an existing file,
209    // and a relative `PATH` segment yields a relative match, so make
210    // the path absolute and require the exec bit — better to leave the
211    // vars unset than hand tools a path they can't run.
212    let node = if node.is_absolute() {
213        node
214    } else {
215        std::env::current_dir().ok()?.join(node)
216    };
217    is_executable_file(&node).then_some(node)
218}
219
220/// Minimal PATH walk (std-only). Returns the first existing,
221/// file-typed match.
222pub(crate) fn find_on_path(bin_name: &str) -> Option<PathBuf> {
223    let path = std::env::var_os("PATH")?;
224    for dir in std::env::split_paths(&path) {
225        if dir.as_os_str().is_empty() {
226            continue;
227        }
228        let candidate = dir.join(bin_name);
229        if candidate.is_file() {
230            return Some(candidate);
231        }
232        #[cfg(windows)]
233        {
234            // PATHEXT resolution for the bare name (mise is usually
235            // mise.exe; node is node.exe — callers pass the .exe name
236            // already, this is a fallback).
237            let with_exe = dir.join(format!("{bin_name}.exe"));
238            if with_exe.is_file() {
239                return Some(with_exe);
240            }
241        }
242    }
243    None
244}
245
246#[cfg(test)]
247mod tests {
248    use super::*;
249
250    fn fab_install(root: &Path, version: &str) {
251        let dir = root.join(version);
252        let bin = if cfg!(windows) {
253            dir.clone()
254        } else {
255            dir.join("bin")
256        };
257        std::fs::create_dir_all(&bin).unwrap();
258        let exe = bin.join(node_exe_name());
259        std::fs::write(&exe, "#!/bin/sh\necho v0.0.0\n").unwrap();
260        #[cfg(unix)]
261        {
262            use std::os::unix::fs::PermissionsExt;
263            std::fs::set_permissions(&exe, std::fs::Permissions::from_mode(0o755)).unwrap();
264        }
265    }
266
267    #[cfg(unix)]
268    #[test]
269    fn non_executable_node_is_rejected() {
270        let tmp = tempfile::tempdir().unwrap();
271        fab_install(tmp.path(), "22.1.0");
272        use std::os::unix::fs::PermissionsExt;
273        let exe = tmp.path().join("22.1.0/bin/node");
274        std::fs::set_permissions(&exe, std::fs::Permissions::from_mode(0o644)).unwrap();
275        assert!(scan_install_dir(tmp.path(), InstallOrigin::Aube).is_empty());
276    }
277
278    #[test]
279    fn scans_and_validates() {
280        let tmp = tempfile::tempdir().unwrap();
281        fab_install(tmp.path(), "22.1.0");
282        fab_install(tmp.path(), "24.4.1");
283        // Incomplete install: skipped.
284        fab_install(tmp.path(), "26.0.0");
285        std::fs::write(tmp.path().join("26.0.0/incomplete"), "").unwrap();
286        // Missing binary: skipped.
287        std::fs::create_dir_all(tmp.path().join("20.0.0")).unwrap();
288        // Non-version dir: skipped.
289        std::fs::create_dir_all(tmp.path().join(".downloads")).unwrap();
290
291        let found = scan_install_dir(tmp.path(), InstallOrigin::Aube);
292        let mut versions: Vec<String> = found.iter().map(|n| n.version.to_string()).collect();
293        versions.sort();
294        assert_eq!(versions, vec!["22.1.0", "24.4.1"]);
295    }
296
297    #[cfg(unix)]
298    #[test]
299    fn alias_symlinks_are_skipped() {
300        let tmp = tempfile::tempdir().unwrap();
301        fab_install(tmp.path(), "22.1.0");
302        std::os::unix::fs::symlink(tmp.path().join("22.1.0"), tmp.path().join("22")).unwrap();
303        std::os::unix::fs::symlink(tmp.path().join("22.1.0"), tmp.path().join("latest")).unwrap();
304        let found = scan_install_dir(tmp.path(), InstallOrigin::Aube);
305        assert_eq!(found.len(), 1, "{found:?}");
306    }
307}