Skip to main content

subspace_core/
path.rs

1//! Tool and `PATH` resolution for every Subspace app.
2//!
3//! A desktop app started from Finder, the Dock, a desktop entry or the Start
4//! Menu does not inherit the `PATH` a terminal has. On macOS launchd gives it
5//! `/usr/bin:/bin:/usr/sbin:/sbin`, so Azure CLI from Homebrew
6//! (`/opt/homebrew/bin/az`) is not found. The host calls [`apply`] once at
7//! startup, before any thread exists. It asks the user's login shell (on
8//! Windows, the registry) for the `PATH` a terminal has and merges it with the
9//! inherited one. When the shell fails, it adds the `PATH` that worked last
10//! time ([`PathCache`]) and then the usual install folders of the platform
11//! ([`FALLBACK_DIRECTORIES`]). Child processes inherit the result.
12//!
13//! The same login shell also supplies the proxy and certificate variables
14//! ([`network`]) that this process lacks. They are never written to the cache.
15//!
16//! Every lookup of an external program uses [`find_tool`]: `PATH` first, then
17//! the path where the tool was found last time. Do not add other `PATH`
18//! lookups.
19
20use std::path::{Path, PathBuf};
21use std::time::Duration;
22
23mod cache;
24mod capture;
25mod entries;
26mod fallback;
27mod lookup;
28pub mod network;
29#[cfg(unix)]
30mod output;
31mod registry;
32mod resolve;
33mod save;
34#[cfg(unix)]
35mod shell;
36
37pub use cache::{CacheSource, CachedPath, CachedTool, PathCache, read_cache};
38pub use capture::Capture;
39pub use fallback::{
40    FALLBACK_DIRECTORIES, LINUX_DIRECTORIES, MACOS_DIRECTORIES, WINDOWS_DIRECTORIES,
41};
42pub use lookup::{ToolError, find_tool, find_tool_readonly};
43pub use resolve::{Resolved, Source, Startup, applied, apply};
44
45/// Set to `0` to keep the inherited `PATH`.
46pub const DISABLE_ENV: &str = "SUBSPACE_GUI_LOGIN_PATH";
47
48/// The short name of [`DISABLE_ENV`]; either one set to `0` works.
49pub const DISABLE_ENV_ALIAS: &str = "SUBSPACE_LOGIN_PATH";
50
51/// How long the login shell may take to start and print its environment
52/// when no saved `PATH` exists. This is the worst-case delay of a start: a
53/// fast shell returns at the end marker, so only a slow or stuck shell waits
54/// this long. Startup files that load nvm or endpoint-security agents can
55/// take several seconds just after the computer starts. There is one
56/// attempt, never a second one.
57pub const SHELL_TIMEOUT: Duration = Duration::from_secs(10);
58
59/// The shorter limit when a saved `PATH` can stand in for a slow shell.
60pub const CACHED_SHELL_TIMEOUT: Duration = Duration::from_secs(5);
61
62/// The login-shell time limit: shorter when a saved `PATH` exists.
63pub fn shell_timeout(cache_present: bool) -> Duration {
64    if cache_present {
65        CACHED_SHELL_TIMEOUT
66    } else {
67        SHELL_TIMEOUT
68    }
69}
70
71/// The cache file name, beside the product's `state.json`.
72pub const CACHE_FILE: &str = "path-cache.json";
73
74/// `<state dir>/path-cache.json`.
75pub fn cache_path(state_dir: &Path) -> PathBuf {
76    state_dir.join(CACHE_FILE)
77}
78
79/// The markers around the `env` output of the login shell (Unix only).
80#[cfg(unix)]
81pub(crate) const START: &str = "__SUBSPACE_PATH_START__";
82#[cfg(unix)]
83pub(crate) const END: &str = "__SUBSPACE_PATH_END__";