subspace-core 0.8.0

Subspace Framework core: config file, credentials, HTTP agent, state and PATH lookup. Internal crate; apps depend on `subspace-framework`.
Documentation
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
//! The one lookup of external programs.

use std::collections::BTreeSet;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::sync::Mutex;

use super::cache;
use super::resolve::search_folders;
use crate::log::{Level, Logger};

/// Unix execute bits (owner/group/other).
#[cfg(unix)]
const UNIX_EXECUTE_BITS: u32 = 0o111;

/// Windows extensions that start a program.
const WINDOWS_EXECUTABLE_EXTENSIONS: [&str; 4] = ["exe", "cmd", "bat", "com"];

/// Why [`find_tool`] found nothing. The texts match Go `exec.LookPath`.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ToolError {
    /// A name with a path separator that is not an executable file.
    #[error("exec: {0:?}: not an executable file")]
    NotExecutable(String),
    /// No executable on `PATH` and no usable saved path.
    #[error("exec: {0:?}: executable file not found in $PATH")]
    NotFound(String),
}

/// Find an external program. A name with a path separator is used as given.
/// Otherwise the first executable match wins, in this order: each `PATH`
/// folder; the path where the tool was found last time, if it still exists
/// and is executable; the platform's usual install folders
/// ([`super::FALLBACK_DIRECTORIES`]) that exist. The last two steps run
/// only after [`super::apply`]: they use its cache file and its folders, so
/// a test that never applies the startup `PATH` never finds a tool outside
/// `PATH`. The last step also covers a start from a terminal whose `PATH`
/// lacks, for example, Homebrew. A `PATH` or install-folder result is saved,
/// as an absolute path, when it differs from the saved path; an
/// install-folder result is logged once per tool.
pub fn find_tool(name: &str) -> Result<PathBuf, ToolError> {
    separated(name).map_or_else(|| find_named(name), |path| given(path, name))
}

/// Resolve a tool using the same search order as [`find_tool`] without saving
/// paths or recording and logging cache or fallback use. No process is started.
pub fn find_tool_readonly(name: &str) -> Result<PathBuf, ToolError> {
    separated(name).map_or_else(|| find_named_readonly(name), |path| given(path, name))
}

/// `name` as a path when it holds a path separator.
fn separated(name: &str) -> Option<PathBuf> {
    (name.contains('/') || name.contains(std::path::MAIN_SEPARATOR)).then(|| PathBuf::from(name))
}

/// A bare `name` through [`locate`] with this process's environment.
fn find_named(name: &str) -> Result<PathBuf, ToolError> {
    let path = std::env::var_os("PATH").unwrap_or_default();
    let folders = search_folders().unwrap_or_default();
    locate(name, &path, folders, cfg!(windows))
        .map(|found| record(name, found))
        .ok_or_else(|| ToolError::NotFound(name.to_string()))
}

fn find_named_readonly(name: &str) -> Result<PathBuf, ToolError> {
    let path = std::env::var_os("PATH").unwrap_or_default();
    let folders = search_folders().unwrap_or_default();
    locate_with(
        name,
        &path,
        folders,
        cfg!(windows),
        cache::cached_tool_readonly,
    )
    .map(Located::into_path)
    .ok_or_else(|| ToolError::NotFound(name.to_string()))
}

/// Where [`locate`] found a tool.
#[derive(Debug, PartialEq, Eq)]
pub(super) enum Located {
    Path(PathBuf),
    Cached(PathBuf),
    Fallback(PathBuf),
}

impl Located {
    fn into_path(self) -> PathBuf {
        match self {
            Self::Path(path) | Self::Cached(path) | Self::Fallback(path) => path,
        }
    }
}

/// `name` on `path`, then at its saved path, then in the `fallback` folders.
pub(super) fn locate(
    name: &str,
    path: &OsStr,
    fallback: &[PathBuf],
    windows: bool,
) -> Option<Located> {
    locate_with(name, path, fallback, windows, cache::cached_tool)
}

fn locate_with(
    name: &str,
    path: &OsStr,
    fallback: &[PathBuf],
    windows: bool,
    cached: impl FnOnce(&str) -> Option<PathBuf>,
) -> Option<Located> {
    search(name, path)
        .map(Located::Path)
        .or_else(|| cached(name).map(Located::Cached))
        .or_else(|| in_fallback(name, fallback, windows).map(Located::Fallback))
}

/// The first executable `name` in the existing `fallback` folders.
pub(super) fn in_fallback(name: &str, fallback: &[PathBuf], windows: bool) -> Option<PathBuf> {
    fallback
        .iter()
        .filter(|directory| directory.is_dir())
        .flat_map(|directory| candidates(directory, name, windows))
        .find(|candidate| is_executable(candidate))
}

/// `path` as an absolute path from `directory`, the working directory of
/// the lookup. Symbolic links and `..` stay as they are.
pub(super) fn absolute_from(path: PathBuf, directory: &Path) -> PathBuf {
    if path.is_absolute() {
        path
    } else {
        directory.join(path)
    }
}

/// Save `found` for `name` as an absolute path.
fn remember(name: &str, found: &Path) {
    let directory = std::env::current_dir().unwrap_or_default();
    cache::remember_tool(name, &absolute_from(found.to_path_buf(), &directory));
}

static LOGGED_FALLBACK: Mutex<BTreeSet<String>> = Mutex::new(BTreeSet::new());

/// Save a `PATH` or install-folder result, log an install-folder result once,
/// and return the path.
fn record(name: &str, found: Located) -> PathBuf {
    match found {
        Located::Path(path) => {
            remember(name, &path);
            path
        }
        Located::Cached(path) => path,
        Located::Fallback(path) => {
            remember(name, &path);
            log_first_fallback(name, &path);
            path
        }
    }
}

/// Log the first install-folder result for `name` in this process.
fn log_first_fallback(name: &str, path: &Path) {
    let first = LOGGED_FALLBACK
        .lock()
        .is_ok_and(|mut logged| logged.insert(name.to_string()));
    if first {
        let text = path.display().to_string();
        Logger::new("path").record(
            Level::Info,
            "install folder tool path used",
            &[("tool", name), ("path", &text)],
        );
    }
}

fn given(path: PathBuf, name: &str) -> Result<PathBuf, ToolError> {
    Some(path)
        .filter(|path| is_executable(path))
        .ok_or_else(|| ToolError::NotExecutable(name.to_string()))
}

/// The first executable `name` in the folders of `path`.
pub(super) fn search(name: &str, path: &OsStr) -> Option<PathBuf> {
    std::env::split_paths(path)
        .flat_map(|directory| candidates(&directory, name, cfg!(windows)))
        .find(|candidate| is_executable(candidate))
}

/// The files `name` can be in `directory`. On Windows, as Go
/// `exec.LookPath` does, a name without an executable extension is tried
/// only with one, so the extensionless `az` shell script beside `az.cmd`
/// is never chosen.
pub(super) fn candidates(directory: &Path, name: &str, windows: bool) -> Vec<PathBuf> {
    let base = directory.join(name);
    if !windows || has_executable_extension(&base) {
        return vec![base];
    }
    WINDOWS_EXECUTABLE_EXTENSIONS
        .iter()
        .map(|extension| directory.join(format!("{name}.{extension}")))
        .collect()
}

/// An existing file with an execute bit (Unix), or any existing file
/// (Windows, where the extension decides).
pub(super) fn is_executable(path: &Path) -> bool {
    let Ok(metadata) = std::fs::metadata(path) else {
        return false;
    };
    if !metadata.is_file() {
        return false;
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        metadata.permissions().mode() & UNIX_EXECUTE_BITS != 0
    }
    #[cfg(not(unix))]
    {
        true
    }
}

/// Whether `path` has an extension that starts a Windows program.
pub(super) fn has_executable_extension(path: &Path) -> bool {
    path.extension()
        .and_then(OsStr::to_str)
        .is_some_and(|extension| {
            WINDOWS_EXECUTABLE_EXTENSIONS
                .iter()
                .any(|known| extension.eq_ignore_ascii_case(known))
        })
}

/// Whether a saved tool path can still run: executable, and on Windows with
/// an executable extension.
pub(super) fn usable_cached(path: &Path) -> bool {
    is_executable(path) && (!cfg!(windows) || has_executable_extension(path))
}

#[cfg(test)]
mod tests {
    use std::path::Path;
    #[cfg(unix)]
    use std::path::PathBuf;

    use super::super::resolve::search_folders;
    #[cfg(unix)]
    use super::{Located, absolute_from, search, usable_cached};
    use super::{
        ToolError, candidates, find_tool, find_tool_readonly, has_executable_extension, locate,
    };

    #[cfg(unix)]
    #[test]
    fn only_an_existing_executable_saved_path_is_usable() {
        use std::os::unix::fs::PermissionsExt as _;

        let directory = tempfile::tempdir().unwrap();
        let runnable = directory.path().join("az");
        crate::executable_tests::write_executable(&runnable, "#!/bin/sh\n");
        let plain = directory.path().join("plain");
        std::fs::write(&plain, "text").unwrap();
        std::fs::set_permissions(&plain, std::fs::Permissions::from_mode(0o600)).unwrap();
        assert!(usable_cached(&runnable));
        assert!(!usable_cached(&plain));
        assert!(!usable_cached(&directory.path().join("missing")));
        assert!(!usable_cached(directory.path()));
    }

    #[cfg(unix)]
    #[test]
    fn search_returns_the_first_executable_on_path() {
        let first = tempfile::tempdir().unwrap();
        let second = tempfile::tempdir().unwrap();
        std::fs::write(first.path().join("tool"), "not executable").unwrap();
        crate::executable_tests::write_executable(&second.path().join("tool"), "#!/bin/sh\n");
        let path = std::env::join_paths([first.path(), second.path()]).unwrap();
        assert_eq!(search("tool", &path), Some(second.path().join("tool")));
        assert_eq!(search("absent", &path), None);
    }

    #[cfg(unix)]
    #[test]
    fn a_tool_missing_from_a_terminal_path_is_found_in_an_install_folder() {
        use std::os::unix::fs::PermissionsExt as _;

        let terminal_path = tempfile::tempdir().unwrap();
        let install = tempfile::tempdir().unwrap();
        let folder = install.path().to_path_buf();
        let path = terminal_path.path().as_os_str();
        assert_eq!(
            locate("pi", path, std::slice::from_ref(&folder), false),
            None
        );
        let pi = install.path().join("pi");
        std::fs::write(&pi, "not executable").unwrap();
        std::fs::set_permissions(&pi, std::fs::Permissions::from_mode(0o600)).unwrap();
        assert_eq!(
            locate("pi", path, std::slice::from_ref(&folder), false),
            None
        );
        std::fs::remove_file(&pi).unwrap();
        crate::executable_tests::write_executable(&pi, "#!/bin/sh\n");
        let folders = [PathBuf::from("/fixture/missing/bin"), folder.clone()];
        assert_eq!(
            locate("pi", path, &folders, false),
            Some(Located::Fallback(pi.clone()))
        );
        crate::executable_tests::write_executable(&terminal_path.path().join("pi"), "#!/bin/sh\n");
        assert_eq!(
            locate("pi", path, &[folder], false),
            Some(Located::Path(terminal_path.path().join("pi")))
        );
    }

    #[test]
    fn without_the_startup_resolution_no_install_folder_is_searched() {
        // Tests never call `apply`, so `find_tool` gets no folders and finds
        // nothing outside PATH, whatever this computer has installed.
        assert_eq!(search_folders(), None);
        assert_eq!(
            locate(
                "subspace-fixture-absent-tool",
                std::ffi::OsStr::new(""),
                search_folders().unwrap_or_default(),
                false
            ),
            None
        );
        assert_eq!(
            find_tool("subspace-fixture-absent-tool"),
            Err(ToolError::NotFound("subspace-fixture-absent-tool".into()))
        );
        assert_eq!(
            find_tool_readonly("subspace-fixture-absent-tool"),
            Err(ToolError::NotFound("subspace-fixture-absent-tool".into()))
        );
    }

    #[cfg(unix)]
    #[test]
    fn readonly_resolution_obeys_path_cache_fallback_order() {
        let path_dir = tempfile::tempdir().unwrap();
        let fallback = tempfile::tempdir().unwrap();
        let cached = tempfile::tempdir().unwrap();
        let name = "readonly-order-fixture";
        let cached_tool = cached.path().join(name);
        let fallback_tool = fallback.path().join(name);
        crate::executable_tests::write_executable(&cached_tool, "#!/bin/sh\n");
        crate::executable_tests::write_executable(&fallback_tool, "#!/bin/sh\n");
        let folders = [fallback.path().to_path_buf()];
        let lookup = || {
            super::locate_with(name, path_dir.path().as_os_str(), &folders, false, |_| {
                Some(cached_tool.clone())
            })
            .map(Located::into_path)
        };
        assert_eq!(lookup(), Some(cached_tool.clone()));
        let path_tool = path_dir.path().join(name);
        crate::executable_tests::write_executable(&path_tool, "#!/bin/sh\n");
        assert_eq!(lookup(), Some(path_tool));
        std::fs::remove_file(path_dir.path().join(name)).unwrap();
        assert_eq!(
            super::locate_with(name, path_dir.path().as_os_str(), &folders, false, |_| None)
                .map(Located::into_path),
            Some(fallback_tool)
        );
    }

    #[cfg(unix)]
    #[test]
    fn tool_paths_are_saved_absolute_without_resolving_links() {
        let directory = Path::new("/fixture/work");
        assert_eq!(
            absolute_from(PathBuf::from("bin/az"), directory),
            PathBuf::from("/fixture/work/bin/az")
        );
        assert_eq!(
            absolute_from(PathBuf::from("../tools/az"), directory),
            PathBuf::from("/fixture/work/../tools/az")
        );
        assert_eq!(
            absolute_from(PathBuf::from("/opt/homebrew/bin/az"), directory),
            PathBuf::from("/opt/homebrew/bin/az")
        );
    }

    #[test]
    fn windows_names_get_an_executable_extension_unless_they_have_one() {
        let directory = Path::new("/fixture/bin");
        assert_eq!(
            candidates(directory, "az", true),
            ["az.exe", "az.cmd", "az.bat", "az.com"].map(|name| directory.join(name))
        );
        assert_eq!(
            candidates(directory, "Headlamp.exe", true),
            [directory.join("Headlamp.exe")]
        );
        assert_eq!(candidates(directory, "az", false), [directory.join("az")]);
    }

    #[test]
    fn windows_extensions_are_case_insensitive() {
        assert!(has_executable_extension(Path::new(r"C:\Tools\az.CMD")));
        assert!(has_executable_extension(Path::new("az.exe")));
        assert!(!has_executable_extension(Path::new("az")));
        assert!(!has_executable_extension(Path::new("az.ps1")));
    }

    #[test]
    fn errors_keep_the_go_texts() {
        assert_eq!(
            ToolError::NotFound("az".into()).to_string(),
            "exec: \"az\": executable file not found in $PATH"
        );
        assert_eq!(
            ToolError::NotExecutable("/x/az".into()).to_string(),
            "exec: \"/x/az\": not an executable file"
        );
    }
}