croft-software 0.2.182

VSCode-style TUI written in Rust
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
//! Finding each file's server: its language and project root, the server's config, and
//! the executable on PATH, in a toolchain's folders or through rustup, and the note shown
//! once when a server is missing.

use super::*;

/// The language server language for `path`: its `files_associations` id
/// (#1574), else what its name and extension map to (#1084). The app's
/// sync uses it to decide which tabs go to the servers at all.
pub(crate) fn path_to_language(path: &Path) -> Option<Language> {
    // `files_associations` first (#1574), so the server serves the
    // language the editor highlights: a known id with no server gets none.
    // An id croft does not know falls through to the name and extension,
    // as it does for highlighting.
    if let Some(id) = crate::highlight::associated_language_id(path) {
        let lang = crate::lsp::languages::resolve(&id);
        if lang.is_some()
            || id == "plaintext"
            || crate::highlight::lang_for_language_id(&id).is_some()
        {
            return lang;
        }
    }
    Language::from_path(path)
}

/// Project-manifest filenames whose presence marks a directory as the root of a
/// `lang` project. Used to anchor the language server at the right sub-project
/// (so basedpyright finds that project's `.venv`, rust-analyzer its `Cargo.toml`,
/// etc.). An empty slice means "no per-project rooting" → use the workspace root.
pub(super) fn manifest_names(lang: Language) -> &'static [&'static str] {
    lang.root_markers()
}

/// [`project_root_within`] with the multi-root bound (#147): the walk stays
/// inside the workspace root OWNING the file (longest prefix over all
/// roots), and the fallback is that owning root — so a file under a
/// secondary root roots its servers there, never at the primary. A file
/// outside every root keeps the historical primary-root fallback.
pub(super) fn project_root_for_roots(
    path: &Path,
    lang: Language,
    primary: &Path,
    extra_roots: &[PathBuf],
) -> PathBuf {
    let owner = std::iter::once(primary)
        .chain(extra_roots.iter().map(PathBuf::as_path))
        .filter(|r| path.starts_with(r))
        .max_by_key(|r| r.components().count())
        .unwrap_or(primary);
    project_root_within(path, lang, owner)
}

/// The project root a file's language server should be anchored at: the nearest
/// ancestor holding a language manifest (e.g. `pyproject.toml`, `Cargo.toml`,
/// `go.mod`, `package.json`), bounded by the workspace root `bound`. Falls back
/// to `bound` when none is found, so a plain single-project workspace behaves
/// exactly as before.
pub(super) fn project_root_within(path: &Path, lang: Language, bound: &Path) -> PathBuf {
    let manifests = manifest_names(lang);
    if !manifests.is_empty() {
        let mut dir = path.parent();
        while let Some(d) = dir {
            if !d.starts_with(bound) {
                break;
            }
            if manifests.iter().any(|m| d.join(m).exists()) {
                return d.to_path_buf();
            }
            if d == bound {
                break;
            }
            dir = d.parent();
        }
    }
    bound.to_path_buf()
}

/// Walk `$PATH` looking for an executable file named `cmd`. Avoids
/// invoking the binary with `--help` or `--version` because LSP servers
/// are JSON-RPC daemons with no consistent CLI flags (basedpyright-
/// langserver exits 1 on --help and crashes on --version, rustup shims
/// pretend to exist even when their component isn't installed).
/// Resolve a server config to a spawnable command, or `None` to skip it.
/// A server carrying a `provision` is handled by croft's managed installer
/// (resolved to an absolute path under `~/.croft/servers`; a lazy background
/// install is kicked off when it's absent). Every other server is resolved
/// against PATH unchanged. `log_skip` gates the "not available" log so empty
/// re-probes don't spam the log on every request.
pub(super) fn resolve_config(
    config: &ServerConfig,
    log_skip: bool,
) -> Option<(ServerConfig, Vec<PathBuf>)> {
    if let Some(provision) = &config.provision {
        // Native-binary servers (rust-analyzer, clangd, taplo) are toolchain
        // tools: a real copy on PATH or in a toolchain dir (~/.cargo/bin) must
        // win over a croft-downloaded one so it matches the user's compiler, and
        // for rust-analyzer `resolve_path_only` prepends that dir to the child
        // PATH so the server can find cargo/rustc under a stripped GUI launch.
        // Only when no real binary exists anywhere do we fall back to a managed
        // download / Termux `pkg` install (`resolve_for_root` also does when the
        // binary is a rustup proxy with no component behind it). npm/uv servers
        // keep their own managed-first (vtsls) or PATH-first (ty/ruff) ordering
        // inside `resolve_managed`, so this preference is scoped to the Binary
        // and Go backends (a gopls in ~/go/bin is the user's own, #930).
        if matches!(
            provision,
            crate::lsp::install::Provision::Binary { .. }
                | crate::lsp::install::Provision::Go { .. }
        ) && let Some(resolved) = resolve_path_only(
            config,
            is_on_path(&config.command),
            &toolchain_fallback_dirs(),
        ) {
            return Some(resolved);
        }
        return crate::lsp::install::resolve_managed(config, provision, log_skip);
    }
    if let Some(resolved) = resolve_path_only(
        config,
        is_on_path(&config.command),
        &toolchain_fallback_dirs(),
    ) {
        return Some(resolved);
    }
    if log_skip {
        log_file::log(&format!(
            "lsp[{}] skip: `{}` not on PATH",
            config.name, config.command
        ));
    }
    None
}

/// What to tell the user when `lang` is left with no language server (#930):
/// every server it registers is missing from PATH and croft cannot install
/// any of them, because it has no provision or `installer_missing` names
/// the tool its install needs. `None` when the language registers no server,
/// or one of them can still be installed (that install reports its own
/// progress and failures).
pub(super) fn missing_servers_note(
    lang: Language,
    configs: &[ServerConfig],
    installer_missing: impl Fn(&crate::lsp::install::Provision) -> Option<&'static str>,
) -> Option<String> {
    if configs.is_empty() {
        return None;
    }
    let mut why = Vec::with_capacity(configs.len());
    for config in configs {
        match config.provision.as_ref().map(&installer_missing) {
            None => why.push(format!("{} isn't on PATH", config.command)),
            Some(Some(tool)) => why.push(format!(
                "{} isn't on PATH, and installing it needs {tool}",
                config.command
            )),
            Some(None) => return None,
        }
    }
    let label = crate::highlight::lang_for_language_id(lang.lsp_id())
        .map_or(lang.lsp_id(), |kind| {
            crate::highlight::language_label(Some(kind))
        });
    Some(format!(
        "{label}: no language server, so no diagnostics or completion ({})",
        why.join("; ")
    ))
}

/// Show `note` in the status bar and OUTPUT › Server Provisioning the first
/// time `lang` is found without a server this session; later files of that
/// language, and other project roots, stay quiet.
pub(super) fn report_missing_servers_once(lang: Language, note: &str) {
    static REPORTED: StdMutex<std::collections::BTreeSet<&'static str>> =
        StdMutex::new(std::collections::BTreeSet::new());
    let first = REPORTED
        .lock()
        .map(|mut seen| seen.insert(lang.lsp_id()))
        .unwrap_or(false);
    if !first {
        return;
    }
    log_file::log(&format!("lsp[{}] {note}", lang.lsp_id()));
    crate::output::push(
        crate::output::CHANNEL_PROVISION,
        crate::output::OutputLevel::Warn,
        note,
    );
    crate::lsp::install::set_status(note);
}

pub(crate) fn is_on_path(cmd: &str) -> bool {
    let Some(path) = std::env::var_os("PATH") else {
        return false;
    };
    std::env::split_paths(&path).any(|entry| is_executable_file(&entry.join(cmd)))
}

/// True when `path` is a regular file with at least one execute bit set. On
/// non-unix any existing file counts (no mode bits to inspect).
pub(crate) fn is_executable_file(path: &Path) -> bool {
    if !path.is_file() {
        return false;
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::metadata(path)
            .map(|m| m.permissions().mode() & 0o111 != 0)
            .unwrap_or(false)
    }
    #[cfg(not(unix))]
    {
        true
    }
}

/// Toolchain `bin` dirs that a macOS GUI launch (Finder / Dock / the Croft.app
/// bundle, which `open`s Ghostty under the stripped launchd PATH) drops because
/// the login-shell profile is never sourced. rust-analyzer and gopls live here
/// but resolve by bare name, so a GUI-launched croft can't find them even
/// though a terminal launch (full PATH) can. Probed only when the command is
/// already absent from the inherited PATH, so a normal launch never reaches it,
/// which keeps local and remote behaviour identical.
pub(super) fn toolchain_fallback_dirs() -> Vec<PathBuf> {
    let mut dirs = Vec::new();
    if let Some(home) = std::env::var_os("HOME") {
        let home = PathBuf::from(home);
        dirs.push(home.join(".cargo").join("bin"));
        dirs.push(home.join("go").join("bin"));
        dirs.push(home.join(".local").join("bin"));
    }
    dirs.push(PathBuf::from("/opt/homebrew/bin"));
    dirs.push(PathBuf::from("/usr/local/bin"));
    dirs
}

/// Absolute path to `cmd` in the first `dirs` entry that holds an executable
/// file of that name, or `None`.
pub(super) fn find_executable_in(cmd: &str, dirs: &[PathBuf]) -> Option<PathBuf> {
    dirs.iter()
        .map(|dir| dir.join(cmd))
        .find(|candidate| is_executable_file(candidate))
}

/// Resolve a PATH-only server (no managed provision) to a spawnable
/// `(config, extra_path)` pair, or `None` to skip it. When `on_path` the
/// bare command spawns as-is; otherwise the toolchain `fallback_dirs` are
/// probed and a hit is pinned to its absolute path (with its dir prepended to
/// the child's PATH) so a stripped GUI PATH can't hide it.
pub(super) fn resolve_path_only(
    config: &ServerConfig,
    on_path: bool,
    fallback_dirs: &[PathBuf],
) -> Option<(ServerConfig, Vec<PathBuf>)> {
    if on_path {
        return Some((config.clone(), Vec::new()));
    }
    let abs = find_executable_in(&config.command, fallback_dirs)?;
    let extra = abs.parent().map(Path::to_path_buf).into_iter().collect();
    let mut resolved = config.clone();
    resolved.command = abs.to_string_lossy().into_owned();
    Some((resolved, extra))
}

/// [`resolve_config`] for the servers of the project at `root` (#927). A
/// native server found on PATH or in `~/.cargo/bin` that is a rustup proxy is
/// only a server when the toolchain `root` selects has the component behind
/// it. rustup puts a `rust-analyzer` proxy there for every toolchain, but its
/// default profile installs no `rust-analyzer`, and that proxy only prints
/// "Unknown binary" and exits, so croft's managed copy serves instead.
pub(super) async fn resolve_for_root(
    config: &ServerConfig,
    root: &Path,
    log_skip: bool,
) -> Option<(ServerConfig, Vec<PathBuf>)> {
    let found = resolve_config(config, log_skip)?;
    let Some(provision @ crate::lsp::install::Provision::Binary { bin, .. }) = &config.provision
    else {
        return Some(found);
    };
    let Some(rustup) = command_path(&found.0.command).and_then(|p| rustup_behind(&p)) else {
        return Some(found);
    };
    let key = (config.name, root.to_path_buf());
    let known = RUSTUP_VERDICTS
        .lock()
        .ok()
        .and_then(|verdicts| verdicts.get(&key).cloned())
        .filter(|(asked, _)| asked.elapsed() < RUSTUP_RECHECK);
    let lacks = match known {
        Some((_, lacks)) => lacks,
        None => {
            let lacks = rustup_lacks(&rustup, bin, root).await;
            if let Ok(mut verdicts) = RUSTUP_VERDICTS.lock() {
                verdicts.insert(key, (std::time::Instant::now(), lacks.clone()));
            }
            lacks
        }
    };
    let Some(RustupLack { why, fix }) = lacks else {
        return Some(found);
    };
    if log_skip {
        let msg = format!(
            "{why} (at {}); falling back to croft's managed copy",
            root.display()
        );
        log_file::log(&format!("lsp[{}] {msg}", config.name));
        crate::output::push(
            crate::output::CHANNEL_PROVISION,
            crate::output::OutputLevel::Info,
            &format!("{}: {msg}", config.name),
        );
    }
    crate::lsp::install::resolve_managed_instead(config, provision, &why, &fix)
}

/// What [`rustup_lacks`] answered per (server, project root), and when, so
/// the re-probe each request makes while croft's copy downloads does not ask
/// rustup again. Each verdict is asked again after [`RUSTUP_RECHECK`]: `None`
/// also stands for a rustup that could not say in time, and a server that
/// runs is never re-probed, so only a question still open is asked again.
pub(super) static RUSTUP_VERDICTS: StdMutex<RustupVerdicts> =
    StdMutex::new(std::collections::BTreeMap::new());

/// [`RUSTUP_VERDICTS`]' map: (server, project root) to when rustup was
/// asked and what it said.
pub(super) type RustupVerdicts =
    std::collections::BTreeMap<(&'static str, PathBuf), (std::time::Instant, Option<RustupLack>)>;

/// How long a verdict holds before rustup is asked again: the status line
/// names `rustup component add`, and once the user has run it, rustup's own
/// server takes over without a restart (#927); a rustup too slow to answer
/// once is asked again rather than trusted for good.
pub(super) const RUSTUP_RECHECK: std::time::Duration = std::time::Duration::from_secs(30);

/// How long rustup gets to answer before croft stops asking and keeps the
/// proxy, as it did before it asked.
pub(super) const RUSTUP_DEADLINE: std::time::Duration = std::time::Duration::from_secs(3);

/// The file a resolved server `command` runs: itself when absolute, else
/// the first executable of that name on PATH.
pub(super) fn command_path(command: &str) -> Option<PathBuf> {
    let path = Path::new(command);
    if path.is_absolute() {
        return Some(path.to_path_buf());
    }
    let dirs: Vec<PathBuf> = std::env::split_paths(&std::env::var_os("PATH")?).collect();
    find_executable_in(command, &dirs)
}

/// The `rustup` that `candidate` is a proxy for, or `None` when it is a
/// program of its own. rustup installs its proxies as hard links to itself,
/// or symlinks where it cannot, so a proxy is the same file as the `rustup`
/// beside it, or beside wherever its links lead.
pub(super) fn rustup_behind(candidate: &Path) -> Option<PathBuf> {
    let id = file_id(candidate)?;
    let rustup = format!("rustup{}", std::env::consts::EXE_SUFFIX);
    std::iter::once(candidate.to_path_buf())
        .chain(std::fs::canonicalize(candidate).ok())
        .map(|p| p.with_file_name(&rustup))
        .find(|p| file_id(p) == Some(id))
}

/// What identifies the file `path` leads to, links followed.
#[cfg(unix)]
pub(super) fn file_id(path: &Path) -> Option<(u64, u64)> {
    use std::os::unix::fs::MetadataExt;
    std::fs::metadata(path).ok().map(|m| (m.dev(), m.ino()))
}

#[cfg(not(unix))]
pub(super) fn file_id(path: &Path) -> Option<PathBuf> {
    std::fs::canonicalize(path).ok()
}

/// Why the proxy `rustup` puts in front of `bin` cannot start it for the
/// project at `root`, and the `rustup component add` that mends it, named
/// with `--toolchain` so it holds from any directory; `None` when the proxy
/// can start it or rustup cannot say. Asked from
/// `root`, since that picks the toolchain (`rust-toolchain.toml`, a directory
/// override). `RUSTUP_AUTO_INSTALL=0` stops the question from installing a
/// toolchain. A toolchain that is not installed at all is left to the proxy,
/// which installs it as before, and so is a rustup too old to honour that
/// variable that is still busy at [`RUSTUP_DEADLINE`].
pub(super) async fn rustup_lacks(rustup: &Path, bin: &str, root: &Path) -> Option<RustupLack> {
    if rustup_says(rustup, root, &["which", bin])
        .await?
        .status
        .success()
    {
        return None;
    }
    let active = rustup_says(rustup, root, &["show", "active-toolchain"]).await?;
    if !active.status.success() {
        return None;
    }
    let toolchain = String::from_utf8_lossy(&active.stdout)
        .split_whitespace()
        .next()?
        .to_owned();
    // Named the way users name it: `stable`, not `stable-<host triple>`.
    let triple = format!("-{}-", std::env::consts::ARCH);
    let channel = toolchain
        .split_once(&triple)
        .map_or(&*toolchain, |(c, _)| c);
    Some(RustupLack {
        why: format!("rustup's '{channel}' toolchain has no {bin}"),
        fix: format!("rustup component add {bin} --toolchain {channel}"),
    })
}

/// `rustup` run with `args` from `root`, or `None` when it could not be run
/// or did not finish within [`RUSTUP_DEADLINE`] (it is killed then).
pub(super) async fn rustup_says(
    rustup: &Path,
    root: &Path,
    args: &[&str],
) -> Option<std::process::Output> {
    let run = tokio::process::Command::new(rustup)
        .args(args)
        .current_dir(root)
        .env("RUSTUP_AUTO_INSTALL", "0")
        .stdin(std::process::Stdio::null())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::null())
        .kill_on_drop(true)
        .output();
    tokio::time::timeout(RUSTUP_DEADLINE, run).await.ok()?.ok()
}