Skip to main content

aft/lsp/
registry.rs

1use std::collections::{HashMap, HashSet};
2use std::path::{Path, PathBuf};
3use std::sync::{Arc, OnceLock};
4
5use crate::config::{Config, UserServerDef};
6use crate::lsp::roots::{
7    find_rust_workspace_root, find_workspace_root, find_workspace_root_within,
8};
9
10/// Resolve an LSP binary name to a full path.
11///
12/// Resolution order (mirrors `format::resolve_tool` for formatters/checkers):
13/// 1. `<project_root>/node_modules/.bin/<binary>` — project devDependency
14/// 2. Each path in `extra_paths` joined with `<binary>` — plugin-supplied
15///    auto-install cache locations such as
16///    `~/.cache/aft/lsp-packages/<pkg>/node_modules/.bin/`
17/// 3. PATH via [`which::which`]
18///
19/// On Windows, candidate directories are also probed with `.cmd`, `.exe`,
20/// and `.bat` extensions because npm-installed shims often use `.cmd`.
21/// `which::which` handles PATHEXT natively for the PATH fallback.
22pub fn resolve_lsp_binary(
23    binary: &str,
24    project_root: Option<&Path>,
25    extra_paths: &[PathBuf],
26) -> Option<PathBuf> {
27    // 1. Project-local node_modules/.bin
28    if let Some(root) = project_root {
29        let local_bin = root.join("node_modules").join(".bin");
30        if let Some(found) = probe_dir(&local_bin, binary) {
31            return Some(found);
32        }
33    }
34
35    // 2. Plugin-supplied extra paths (auto-install cache, etc.)
36    for dir in extra_paths {
37        if let Some(found) = probe_dir(dir, binary) {
38            return Some(found);
39        }
40    }
41
42    // 3. PATH fallback
43    which::which(binary).ok()
44}
45
46/// Check `dir/<binary>` and (on Windows) `dir/<binary>.cmd|.exe|.bat`.
47fn probe_dir(dir: &Path, binary: &str) -> Option<PathBuf> {
48    if !dir.is_dir() {
49        return None;
50    }
51
52    if cfg!(windows) {
53        // npm creates both an extensionless POSIX shell shim and a `.cmd`
54        // wrapper under node_modules/.bin. The extensionless shim exists on
55        // Windows too but is not a Win32 executable, so prefer Windows-native
56        // wrappers before falling back to the direct path.
57        for ext in ["cmd", "exe", "bat"] {
58            let candidate = dir.join(format!("{binary}.{ext}"));
59            if candidate.is_file() {
60                return Some(candidate);
61            }
62        }
63    }
64
65    let direct = dir.join(binary);
66    if direct.is_file() {
67        return Some(direct);
68    }
69
70    None
71}
72
73/// Unique identifier for a language server kind.
74///
75/// IDs match OpenCode's `lsp/server.ts` registry where possible so users can
76/// refer to the same names in `lsp.disabled` config across both projects.
77#[derive(Debug, Clone, PartialEq, Eq, Hash)]
78pub enum ServerKind {
79    // --- Built-in (existing, pre-v0.17.0) ---
80    TypeScript,
81    Python, // pyright
82    Rust,
83    Go,
84    Bash,
85    Yaml,
86    Ty, // experimental Astral Python LSP
87    // --- v0.17.0: PATH-only servers (Pattern A) ---
88    Clojure,
89    Dart,
90    ElixirLs,
91    FSharp,
92    Gleam,
93    Haskell,
94    Jdtls, // Java
95    Julia,
96    Nixd,
97    OcamlLsp,
98    PhpIntelephense,
99    RubyLsp,
100    SourceKit, // Swift
101    CSharp,
102    Razor,
103    // --- v0.17.0: Pattern C (PATH-first, GitHub-release auto-download in plugin) ---
104    Clangd,
105    LuaLs,
106    Zls,
107    Tinymist,
108    KotlinLs,
109    Texlab,
110    Oxlint,
111    TerraformLs,
112    // --- v0.17.0: Pattern B/D (npm auto-installable in plugin) ---
113    Vue,
114    Astro,
115    Prisma, // resolves the project's `prisma` CLI from node_modules; not auto-installed by AFT
116    Biome,
117    Svelte,
118    Dockerfile,
119    Custom(Arc<str>),
120}
121
122impl ServerKind {
123    pub fn id_str(&self) -> &str {
124        match self {
125            Self::TypeScript => "typescript",
126            Self::Python => "python",
127            Self::Rust => "rust",
128            Self::Go => "go",
129            Self::Bash => "bash",
130            Self::Yaml => "yaml",
131            Self::Ty => "ty",
132            // Pattern A
133            Self::Clojure => "clojure-lsp",
134            Self::Dart => "dart",
135            Self::ElixirLs => "elixir-ls",
136            Self::FSharp => "fsharp",
137            Self::Gleam => "gleam",
138            Self::Haskell => "haskell-language-server",
139            Self::Jdtls => "jdtls",
140            Self::Julia => "julials",
141            Self::Nixd => "nixd",
142            Self::OcamlLsp => "ocaml-lsp",
143            Self::PhpIntelephense => "php-intelephense",
144            Self::RubyLsp => "ruby-lsp",
145            Self::SourceKit => "sourcekit-lsp",
146            Self::CSharp => "csharp",
147            Self::Razor => "razor",
148            // Pattern C
149            Self::Clangd => "clangd",
150            Self::LuaLs => "lua-ls",
151            Self::Zls => "zls",
152            Self::Tinymist => "tinymist",
153            Self::KotlinLs => "kotlin-ls",
154            Self::Texlab => "texlab",
155            Self::Oxlint => "oxlint",
156            Self::TerraformLs => "terraform",
157            // Pattern B/D
158            Self::Vue => "vue",
159            Self::Astro => "astro",
160            Self::Prisma => "prisma",
161            Self::Biome => "biome",
162            Self::Svelte => "svelte",
163            Self::Dockerfile => "dockerfile",
164            Self::Custom(id) => id.as_ref(),
165        }
166    }
167}
168
169/// Definition of a language server.
170#[derive(Debug, Clone, PartialEq, Eq)]
171pub struct ServerDef {
172    pub kind: ServerKind,
173    /// Display name.
174    pub name: String,
175    /// File extensions this server handles.
176    pub extensions: Vec<String>,
177    /// Binary name to look up on PATH.
178    pub binary: String,
179    /// Arguments to pass when spawning.
180    pub args: Vec<String>,
181    /// Root marker files — presence indicates a workspace root.
182    pub root_markers: Vec<String>,
183    /// Higher-priority root markers checked before fallback markers.
184    ///
185    /// Pyright uses this for configuration files because, in language-server
186    /// mode, Pyright looks for pyrightconfig.json and pyproject.toml only in
187    /// the workspace root supplied by the client. A nearer fallback marker like
188    /// requirements.txt must not hide the directory that contains the actual
189    /// Pyright configuration.
190    pub priority_root_markers: Vec<String>,
191    /// Extra environment variables for this server process.
192    pub env: HashMap<String, String>,
193    /// Optional JSON initializationOptions for the initialize request.
194    pub initialization_options: Option<serde_json::Value>,
195}
196
197impl ServerDef {
198    /// Return the workspace root this server should use for a file.
199    pub fn workspace_root_for_file(&self, file_path: &Path) -> Option<PathBuf> {
200        self.workspace_root_for_file_with_project_root(file_path, None)
201    }
202
203    /// Return the workspace root for a file without searching above `project_root`.
204    ///
205    /// Rust Analyzer indexes the full Cargo workspace passed to it. Resolve Rust
206    /// member crates to their owning workspace before the generic nearest-marker
207    /// fallback so sibling members share a single analyzer. Other languages keep
208    /// their established priority-marker behavior unchanged.
209    pub fn workspace_root_for_file_with_project_root(
210        &self,
211        file_path: &Path,
212        project_root: Option<&Path>,
213    ) -> Option<PathBuf> {
214        if self.kind == ServerKind::Rust {
215            if let Some(root) = find_rust_workspace_root(file_path, project_root) {
216                return Some(root);
217            }
218        }
219
220        for marker in &self.priority_root_markers {
221            if let Some(root) = find_workspace_root(file_path, &[marker.as_str()]) {
222                return Some(root);
223            }
224        }
225
226        if self.kind == ServerKind::Rust {
227            find_workspace_root_within(file_path, &self.root_markers, project_root)
228        } else {
229            find_workspace_root(file_path, &self.root_markers)
230        }
231    }
232
233    /// Check if this server handles a given file extension.
234    pub fn matches_extension(&self, ext: &str) -> bool {
235        self.extensions
236            .iter()
237            .any(|candidate| candidate.eq_ignore_ascii_case(ext))
238    }
239
240    /// Check if the server binary is available on PATH.
241    pub fn is_available(&self) -> bool {
242        which::which(&self.binary).is_ok()
243    }
244}
245
246/// Built-in server definitions.
247pub fn builtin_servers() -> Vec<ServerDef> {
248    vec![
249        builtin_server(
250            ServerKind::TypeScript,
251            "TypeScript Language Server",
252            &["ts", "tsx", "js", "jsx", "mjs", "cjs"],
253            "typescript-language-server",
254            &["--stdio"],
255            &["tsconfig.json", "jsconfig.json", "package.json"],
256        ),
257        builtin_server_with_priority_roots(
258            ServerKind::Python,
259            "Pyright",
260            &["py", "pyi"],
261            "pyright-langserver",
262            &["--stdio"],
263            &[
264                "pyrightconfig.json",
265                "pyproject.toml",
266                "setup.py",
267                "setup.cfg",
268                "requirements.txt",
269            ],
270            &["pyrightconfig.json", "pyproject.toml"],
271        ),
272        builtin_server(
273            ServerKind::Rust,
274            "rust-analyzer",
275            &["rs"],
276            "rust-analyzer",
277            &[],
278            &["Cargo.toml", "Cargo.lock"],
279        ),
280        // gopls requires opt-in for `textDocument/diagnostic` (LSP 3.17 pull)
281        // via the `pullDiagnostics` initializationOption. Without this the
282        // server still publishes via push but ignores pull requests.
283        // See https://github.com/golang/tools/blob/master/gopls/doc/settings.md
284        builtin_server_with_init(
285            ServerKind::Go,
286            "gopls",
287            &["go"],
288            "gopls",
289            &["serve"],
290            &["go.mod", "go.sum"],
291            serde_json::json!({ "pullDiagnostics": true }),
292        ),
293        builtin_server(
294            ServerKind::Bash,
295            "bash-language-server",
296            &["sh", "bash", "zsh"],
297            "bash-language-server",
298            &["start"],
299            &["package.json", ".git"],
300        ),
301        builtin_server(
302            ServerKind::Yaml,
303            "yaml-language-server",
304            &["yaml", "yml"],
305            "yaml-language-server",
306            &["--stdio"],
307            &["package.json", ".git"],
308        ),
309        builtin_server(
310            ServerKind::Ty,
311            "ty",
312            &["py", "pyi"],
313            "ty",
314            &["server"],
315            &[
316                "pyproject.toml",
317                "ty.toml",
318                "setup.py",
319                "setup.cfg",
320                "requirements.txt",
321                "Pipfile",
322                "pyrightconfig.json",
323            ],
324        ),
325        // ===== Pattern A: PATH-only servers =====
326        // These servers are not auto-installed by AFT (the toolchain itself
327        // ships the LSP, e.g. `dart`, `gleam`; or installation is highly
328        // platform-specific, e.g. `jdtls`). Users install via system package
329        // manager / language toolchain. AFT registers the def so users with
330        // the binary on PATH get LSP coverage.
331        builtin_server(
332            ServerKind::Clojure,
333            "clojure-lsp",
334            &["clj", "cljs", "cljc", "edn"],
335            "clojure-lsp",
336            &[],
337            &[
338                "deps.edn",
339                "project.clj",
340                "shadow-cljs.edn",
341                "bb.edn",
342                "build.boot",
343            ],
344        ),
345        builtin_server(
346            ServerKind::Dart,
347            "Dart Language Server",
348            &["dart"],
349            "dart",
350            &["language-server", "--lsp"],
351            &["pubspec.yaml", "analysis_options.yaml"],
352        ),
353        builtin_server(
354            ServerKind::ElixirLs,
355            "elixir-ls",
356            &["ex", "exs"],
357            "elixir-ls",
358            &[],
359            &["mix.exs", "mix.lock"],
360        ),
361        builtin_server(
362            ServerKind::FSharp,
363            "FSAutoComplete",
364            &["fs", "fsi", "fsx", "fsscript"],
365            "fsautocomplete",
366            &[],
367            &[".slnx", ".sln", ".fsproj", "global.json"],
368        ),
369        builtin_server(
370            ServerKind::Gleam,
371            "Gleam Language Server",
372            &["gleam"],
373            "gleam",
374            &["lsp"],
375            &["gleam.toml"],
376        ),
377        builtin_server(
378            ServerKind::Haskell,
379            "haskell-language-server",
380            &["hs", "lhs"],
381            "haskell-language-server-wrapper",
382            &["--lsp"],
383            &["stack.yaml", "cabal.project", "hie.yaml"],
384        ),
385        builtin_server(
386            ServerKind::Jdtls,
387            "Eclipse JDT Language Server",
388            &["java"],
389            "jdtls",
390            &[],
391            &["pom.xml", "build.gradle", "build.gradle.kts", ".project"],
392        ),
393        builtin_server(
394            ServerKind::Julia,
395            "Julia Language Server",
396            &["jl"],
397            "julia",
398            &[
399                "--startup-file=no",
400                "--history-file=no",
401                "-e",
402                "using LanguageServer; runserver()",
403            ],
404            &["Project.toml", "Manifest.toml"],
405        ),
406        builtin_server(
407            ServerKind::Nixd,
408            "nixd",
409            &["nix"],
410            "nixd",
411            &[],
412            &["flake.nix", "default.nix", "shell.nix"],
413        ),
414        builtin_server(
415            ServerKind::OcamlLsp,
416            "ocaml-lsp",
417            &["ml", "mli"],
418            "ocamllsp",
419            &[],
420            &["dune-project", "dune-workspace", ".merlin", "opam"],
421        ),
422        builtin_server(
423            ServerKind::PhpIntelephense,
424            "Intelephense",
425            &["php"],
426            "intelephense",
427            &["--stdio"],
428            &["composer.json", "composer.lock", ".php-version"],
429        ),
430        builtin_server(
431            ServerKind::RubyLsp,
432            "ruby-lsp",
433            &["rb", "rake", "gemspec", "ru"],
434            "ruby-lsp",
435            &[],
436            &["Gemfile"],
437        ),
438        builtin_server(
439            ServerKind::SourceKit,
440            "SourceKit-LSP",
441            &["swift"],
442            "sourcekit-lsp",
443            &[],
444            &["Package.swift"],
445        ),
446        builtin_server(
447            ServerKind::CSharp,
448            "Roslyn Language Server",
449            &["cs", "csx"],
450            "roslyn-language-server",
451            &[],
452            &[".slnx", ".sln", ".csproj", "global.json"],
453        ),
454        builtin_server(
455            ServerKind::Razor,
456            "rzls",
457            &["razor", "cshtml"],
458            "rzls",
459            &[],
460            &[".slnx", ".sln", ".csproj", "global.json"],
461        ),
462        // ===== Pattern C: PATH-first; plugin auto-downloads from GitHub releases =====
463        builtin_server(
464            ServerKind::Clangd,
465            "clangd",
466            &[
467                "c", "cpp", "cc", "cxx", "c++", "h", "hpp", "hh", "hxx", "h++",
468            ],
469            "clangd",
470            &[],
471            &["compile_commands.json", "compile_flags.txt", ".clangd"],
472        ),
473        builtin_server(
474            ServerKind::LuaLs,
475            "lua-language-server",
476            &["lua"],
477            "lua-language-server",
478            &[],
479            &[".luarc.json", ".luarc.jsonc", ".stylua.toml", "stylua.toml"],
480        ),
481        builtin_server(
482            ServerKind::Zls,
483            "zls",
484            &["zig", "zon"],
485            "zls",
486            &[],
487            &["build.zig"],
488        ),
489        builtin_server(
490            ServerKind::Tinymist,
491            "tinymist",
492            &["typ", "typc"],
493            "tinymist",
494            &[],
495            &["typst.toml"],
496        ),
497        builtin_server(
498            ServerKind::KotlinLs,
499            "kotlin-language-server",
500            &["kt", "kts"],
501            "kotlin-language-server",
502            &[],
503            &["settings.gradle", "settings.gradle.kts", "build.gradle"],
504        ),
505        builtin_server(
506            ServerKind::Texlab,
507            "texlab",
508            &["tex", "bib"],
509            "texlab",
510            &[],
511            &[".latexmkrc", "latexmkrc", ".texlabroot", "texlabroot"],
512        ),
513        builtin_server(
514            ServerKind::Oxlint,
515            "oxc-language-server",
516            // Same JS/TS family as TypeScript LS; coexists rather than replaces.
517            &[
518                "ts", "tsx", "js", "jsx", "mjs", "cjs", "mts", "cts", "vue", "astro", "svelte",
519            ],
520            "oxc-language-server",
521            &[],
522            // Only trigger on actual oxlint config files. We previously also
523            // matched `package.json`, but that fired oxc on every JS/TS project
524            // whether they used oxlint or not, producing a persistent warning
525            // for the (overwhelmingly common) case where the user never opted
526            // into oxlint. Users who run oxlint will have one of these config
527            // files; everyone else gets silence.
528            &[".oxlintrc.json", ".oxlintrc"],
529        ),
530        builtin_server(
531            ServerKind::TerraformLs,
532            "terraform-ls",
533            &["tf", "tfvars"],
534            "terraform-ls",
535            &["serve"],
536            &[".terraform.lock.hcl", "terraform.tfstate"],
537        ),
538        // ===== Pattern B/D: PATH-first; plugin auto-installs from npm =====
539        // Order matters slightly: vue/svelte/astro use TypeScript-family
540        // extensions when paired with their primary file extension. Each
541        // server only runs against its own primary extension here; agents
542        // run TypeScript LS for the rest.
543        builtin_server(
544            ServerKind::Vue,
545            "Vue Language Server",
546            &["vue"],
547            "vue-language-server",
548            &["--stdio"],
549            &[
550                "package-lock.json",
551                "bun.lockb",
552                "bun.lock",
553                "pnpm-lock.yaml",
554                "yarn.lock",
555            ],
556        ),
557        builtin_server(
558            ServerKind::Astro,
559            "Astro Language Server",
560            &["astro"],
561            "astro-ls",
562            &["--stdio"],
563            &[
564                "package-lock.json",
565                "bun.lockb",
566                "bun.lock",
567                "pnpm-lock.yaml",
568                "yarn.lock",
569            ],
570        ),
571        // Prisma's LSP runs via `prisma language-server` from the project's
572        // own `prisma` CLI (resolved through node_modules/.bin). AFT does NOT
573        // auto-install the prisma package — users get LSP coverage when their
574        // project has prisma as a devDependency.
575        builtin_server(
576            ServerKind::Prisma,
577            "Prisma Language Server",
578            &["prisma"],
579            "prisma",
580            &["language-server"],
581            &["schema.prisma", "package.json"],
582        ),
583        // Biome: lint+format LSP for the JS/TS family. Coexists with the
584        // TypeScript Language Server (different responsibilities). Disable
585        // via `lsp.disabled: ["biome"]` when not desired.
586        builtin_server(
587            ServerKind::Biome,
588            "Biome",
589            &[
590                "ts", "tsx", "js", "jsx", "mjs", "cjs", "mts", "cts", "json", "jsonc",
591            ],
592            "biome",
593            &["lsp-proxy"],
594            &["biome.json", "biome.jsonc"],
595        ),
596        builtin_server(
597            ServerKind::Svelte,
598            "Svelte Language Server",
599            &["svelte"],
600            "svelteserver",
601            &["--stdio"],
602            &[
603                "package-lock.json",
604                "bun.lockb",
605                "bun.lock",
606                "pnpm-lock.yaml",
607                "yarn.lock",
608            ],
609        ),
610        builtin_server(
611            ServerKind::Dockerfile,
612            "Dockerfile Language Server",
613            // OpenCode special-cases the literal "Dockerfile" name; AFT's
614            // extension-only matcher cannot. Users can `aft_outline`/edit
615            // Dockerfiles by extension `.dockerfile`. Plain `Dockerfile`
616            // files won't auto-trigger LSP — acknowledged limitation; can
617            // be revisited if users complain.
618            &["dockerfile"],
619            "docker-langserver",
620            &["--stdio"],
621            &["Dockerfile", "dockerfile", ".dockerignore"],
622        ),
623        // NOTE: ESLint LSP intentionally not registered — OpenCode resolves it
624        // through `Module.resolve("eslint", root)` and runs custom server-side
625        // logic. AFT does not implement that flow yet; users with ESLint can
626        // run `eslint --fix` via bash.
627    ]
628}
629
630/// Find all server definitions that handle a given file path.
631pub fn servers_for_file(path: &Path, config: &Config) -> Vec<ServerDef> {
632    let extension = path
633        .extension()
634        .and_then(|ext| ext.to_str())
635        .unwrap_or_default();
636
637    resolved_servers(config)
638        .into_iter()
639        .filter(|server| !is_disabled(server, config))
640        .filter(|server| config.experimental_lsp_ty || server.kind != ServerKind::Ty)
641        .filter(|server| server.matches_extension(extension))
642        .collect()
643}
644
645/// Resolve the full server set after applying user overrides.
646///
647/// When a user-defined server's `id` matches a built-in server's `id_str()`
648/// (e.g. `lsp.servers.clangd`), the user entry REPLACES the built-in entry
649/// rather than registering alongside it. Fields the user left at their
650/// default value (empty array, empty string, empty map, None) are inherited
651/// from the built-in so users only have to specify what they actually want
652/// to override.
653///
654/// User-defined servers whose `id` does not match any built-in are appended
655/// as `ServerKind::Custom(id)` with no merging — they're standalone.
656fn resolved_servers(config: &Config) -> Vec<ServerDef> {
657    let mut servers = builtin_servers();
658    for user in &config.lsp_servers {
659        if user.disabled {
660            // Disabled user override means "drop the matching built-in if any"
661            // — equivalent to adding the id to `lsp.disabled`. We don't include
662            // it in the result regardless of whether it matched.
663            servers.retain(|s| s.kind.id_str() != user.id);
664            continue;
665        }
666        if let Some(position) = servers.iter().position(|s| s.kind.id_str() == user.id) {
667            // Replace the built-in with a merged ServerDef. Keep the built-in
668            // `kind` so callers that match on enum variants (e.g. cap probing
669            // for `ServerKind::Go`) continue to work. Inherit any field the
670            // user left at its default value.
671            let builtin = &servers[position];
672            let merged = ServerDef {
673                kind: builtin.kind.clone(),
674                name: builtin.name.clone(),
675                extensions: if user.extensions.is_empty() {
676                    builtin.extensions.clone()
677                } else {
678                    user.extensions.clone()
679                },
680                binary: if user.binary.is_empty() {
681                    builtin.binary.clone()
682                } else {
683                    user.binary.clone()
684                },
685                args: if user.args.is_empty() {
686                    builtin.args.clone()
687                } else {
688                    user.args.clone()
689                },
690                root_markers: if user.root_markers.is_empty() {
691                    builtin.root_markers.clone()
692                } else {
693                    user.root_markers.clone()
694                },
695                priority_root_markers: if user.root_markers.is_empty() {
696                    builtin.priority_root_markers.clone()
697                } else {
698                    Vec::new()
699                },
700                env: if user.env.is_empty() {
701                    builtin.env.clone()
702                } else {
703                    user.env.clone()
704                },
705                initialization_options: user
706                    .initialization_options
707                    .clone()
708                    .or_else(|| builtin.initialization_options.clone()),
709            };
710            servers[position] = merged;
711        } else if let Some(def) = custom_server(user) {
712            servers.push(def);
713        }
714    }
715    servers
716}
717
718/// Returns true when `path` is a project configuration file whose changes can
719/// affect an LSP server's workspace/project graph, even if the edited file
720/// itself is not a source file handled by that server.
721pub fn is_config_file_path(path: &Path) -> bool {
722    const IGNORED_COMPONENTS: &[&str] = &[
723        "node_modules",
724        "target",
725        "vendor",
726        ".git",
727        "dist",
728        "build",
729        ".next",
730        ".nuxt",
731        "__pycache__",
732    ];
733
734    if path.components().any(|component| {
735        component
736            .as_os_str()
737            .to_str()
738            .is_some_and(|name| IGNORED_COMPONENTS.contains(&name))
739    }) {
740        return false;
741    }
742
743    let Some(file_name) = path.file_name().and_then(|name| name.to_str()) else {
744        return false;
745    };
746
747    // Lockfiles appear in root_markers for workspace-detection but should NOT
748    // trigger didChangeWatchedFiles notifications — they are regenerated by
749    // package managers constantly and notifying LSP servers on every install
750    // creates unnecessary churn without affecting language analysis.
751    // Intentional: this list is checked BEFORE builtin_config_file_names so a
752    // file that is both a root_marker and a lockfile is excluded.
753    const LOCKFILE_NAMES: &[&str] = &[
754        "package-lock.json",
755        "yarn.lock",
756        "pnpm-lock.yaml",
757        "Cargo.lock",
758        "Gemfile.lock",
759        "poetry.lock",
760        "go.sum",
761        "bun.lock",
762        "bun.lockb",
763    ];
764    if LOCKFILE_NAMES.contains(&file_name) {
765        return false;
766    }
767
768    builtin_config_file_names().contains(file_name)
769        || (file_name.starts_with("tsconfig.") && file_name.ends_with(".json"))
770}
771
772/// Extended variant that also considers root_markers from user-configured
773/// custom LSP servers (#25). Call this from contexts where Config is available.
774/// Falls back to `is_config_file_path` when `extra_markers` is empty.
775pub fn is_config_file_path_with_custom(path: &Path, extra_markers: &[String]) -> bool {
776    if is_config_file_path(path) {
777        return true;
778    }
779    if extra_markers.is_empty() {
780        return false;
781    }
782    let Some(file_name) = path.file_name().and_then(|n| n.to_str()) else {
783        return false;
784    };
785    extra_markers.iter().any(|m| m == file_name)
786}
787
788fn builtin_config_file_names() -> &'static HashSet<String> {
789    static NAMES: OnceLock<HashSet<String>> = OnceLock::new();
790    NAMES.get_or_init(|| {
791        builtin_servers()
792            .into_iter()
793            .flat_map(|server| server.root_markers)
794            .collect()
795    })
796}
797
798fn builtin_server(
799    kind: ServerKind,
800    name: &str,
801    extensions: &[&str],
802    binary: &str,
803    args: &[&str],
804    root_markers: &[&str],
805) -> ServerDef {
806    ServerDef {
807        kind,
808        name: name.to_string(),
809        extensions: strings(extensions),
810        binary: binary.to_string(),
811        args: strings(args),
812        root_markers: strings(root_markers),
813        priority_root_markers: Vec::new(),
814        env: HashMap::new(),
815        initialization_options: None,
816    }
817}
818
819/// Builder variant of [`builtin_server`] that checks some markers before
820/// fallback root markers even when the fallback marker is closer to the file.
821fn builtin_server_with_priority_roots(
822    kind: ServerKind,
823    name: &str,
824    extensions: &[&str],
825    binary: &str,
826    args: &[&str],
827    root_markers: &[&str],
828    priority_root_markers: &[&str],
829) -> ServerDef {
830    let mut def = builtin_server(kind, name, extensions, binary, args, root_markers);
831    def.priority_root_markers = strings(priority_root_markers);
832    def
833}
834
835fn builtin_server_with_init(
836    kind: ServerKind,
837    name: &str,
838    extensions: &[&str],
839    binary: &str,
840    args: &[&str],
841    root_markers: &[&str],
842    initialization_options: serde_json::Value,
843) -> ServerDef {
844    let mut def = builtin_server(kind, name, extensions, binary, args, root_markers);
845    def.initialization_options = Some(initialization_options);
846    def
847}
848
849fn custom_server(server: &UserServerDef) -> Option<ServerDef> {
850    if server.disabled {
851        return None;
852    }
853
854    Some(ServerDef {
855        kind: ServerKind::Custom(Arc::from(server.id.as_str())),
856        name: server.id.clone(),
857        extensions: server.extensions.clone(),
858        binary: server.binary.clone(),
859        args: server.args.clone(),
860        root_markers: server.root_markers.clone(),
861        priority_root_markers: Vec::new(),
862        env: server.env.clone(),
863        initialization_options: server.initialization_options.clone(),
864    })
865}
866
867fn is_disabled(server: &ServerDef, config: &Config) -> bool {
868    config
869        .disabled_lsp
870        .contains(&server.kind.id_str().to_ascii_lowercase())
871}
872
873fn strings(values: &[&str]) -> Vec<String> {
874    values.iter().map(|value| (*value).to_string()).collect()
875}
876
877#[cfg(test)]
878mod tests {
879    use std::path::{Path, PathBuf};
880    use std::sync::Arc;
881
882    use super::{is_config_file_path, resolve_lsp_binary, servers_for_file, ServerKind};
883    use crate::config::{Config, UserServerDef};
884
885    fn matching_kinds(path: &str, config: &Config) -> Vec<ServerKind> {
886        servers_for_file(Path::new(path), config)
887            .into_iter()
888            .map(|server| server.kind)
889            .collect()
890    }
891
892    #[test]
893    fn test_servers_for_typescript_file() {
894        // TS files match TypeScript (primary) plus Biome / Oxlint / Eslint
895        // co-servers. The full set is asserted in `test_typescript_co_servers`.
896        let kinds = matching_kinds("/tmp/file.ts", &Config::default());
897        assert!(
898            kinds.contains(&ServerKind::TypeScript),
899            "expected TypeScript in {kinds:?}",
900        );
901    }
902
903    #[test]
904    fn test_is_config_file_path_recognizes_project_graph_configs() {
905        // These ARE config files that should trigger didChangeWatchedFiles.
906        for path in [
907            "/repo/package.json",
908            "/repo/tsconfig.json",
909            "/repo/tsconfig.build.json",
910            "/repo/jsconfig.json",
911            "/repo/pyproject.toml",
912            "/repo/pyrightconfig.json",
913            "/repo/Cargo.toml",
914            "/repo/go.mod",
915            "/repo/biome.json",
916        ] {
917            assert!(
918                is_config_file_path(Path::new(path)),
919                "expected config: {path}"
920            );
921        }
922
923        // Lockfiles are excluded even though they appear in root_markers —
924        // they change on every package install and triggering LSP re-analysis
925        // on each install creates unnecessary churn. See the LOCKFILE_NAMES
926        // list in is_config_file_path().
927        for path in [
928            "/repo/Cargo.lock",
929            "/repo/go.sum",
930            "/repo/bun.lock",
931            "/repo/bun.lockb",
932            "/repo/package-lock.json",
933            "/repo/yarn.lock",
934            "/repo/pnpm-lock.yaml",
935        ] {
936            assert!(
937                !is_config_file_path(Path::new(path)),
938                "lockfile should be excluded from config-file detection: {path}"
939            );
940        }
941
942        // Non-config files
943        for path in [
944            "/repo/tsconfig-json",
945            "/repo/tsconfig.build.ts",
946            "/repo/cargo.toml",
947            "/repo/src/package.json.ts",
948        ] {
949            assert!(
950                !is_config_file_path(Path::new(path)),
951                "expected non-config: {path}"
952            );
953        }
954    }
955
956    #[test]
957    fn test_typescript_co_servers() {
958        let kinds = matching_kinds("/tmp/file.ts", &Config::default());
959        assert!(kinds.contains(&ServerKind::TypeScript));
960        assert!(kinds.contains(&ServerKind::Biome));
961        assert!(kinds.contains(&ServerKind::Oxlint));
962    }
963
964    #[test]
965    fn test_typescript_co_servers_can_be_disabled() {
966        // `lsp.disabled` lets users opt out of co-servers individually.
967        let mut disabled = std::collections::HashSet::new();
968        disabled.insert("biome".to_string());
969        disabled.insert("oxlint".to_string());
970
971        let config = Config {
972            disabled_lsp: disabled,
973            ..Config::default()
974        };
975
976        assert_eq!(
977            matching_kinds("/tmp/file.ts", &config),
978            vec![ServerKind::TypeScript]
979        );
980    }
981
982    #[test]
983    fn test_servers_for_python_file() {
984        assert_eq!(
985            matching_kinds("/tmp/file.py", &Config::default()),
986            vec![ServerKind::Python]
987        );
988    }
989
990    #[test]
991    fn test_servers_for_rust_file() {
992        assert_eq!(
993            matching_kinds("/tmp/file.rs", &Config::default()),
994            vec![ServerKind::Rust]
995        );
996    }
997
998    #[test]
999    fn test_servers_for_go_file() {
1000        assert_eq!(
1001            matching_kinds("/tmp/file.go", &Config::default()),
1002            vec![ServerKind::Go]
1003        );
1004    }
1005
1006    #[test]
1007    fn test_servers_for_unknown_file() {
1008        assert!(matching_kinds("/tmp/file.txt", &Config::default()).is_empty());
1009    }
1010
1011    #[test]
1012    fn test_oxlint_root_markers_exclude_package_json() {
1013        // Regression guard (v0.17.2): oxc-language-server previously listed
1014        // `package.json` as a root marker, which fired oxc on every JS/TS
1015        // project — including the overwhelming majority that don't use
1016        // oxlint — producing a persistent "binary missing" warning whenever
1017        // the binary wasn't installed. Root markers are now restricted to
1018        // actual oxlint config files, mirroring user intent.
1019        let oxlint = super::builtin_servers()
1020            .into_iter()
1021            .find(|s| s.kind == ServerKind::Oxlint)
1022            .expect("Oxlint server must be registered");
1023
1024        assert!(
1025            !oxlint.root_markers.iter().any(|m| m == "package.json"),
1026            "package.json must not be a root marker for oxlint (got {:?})",
1027            oxlint.root_markers,
1028        );
1029        assert!(
1030            oxlint.root_markers.iter().any(|m| m == ".oxlintrc.json")
1031                || oxlint.root_markers.iter().any(|m| m == ".oxlintrc"),
1032            "expected an oxlint config file in root markers (got {:?})",
1033            oxlint.root_markers,
1034        );
1035    }
1036
1037    #[test]
1038    fn test_tsx_matches_typescript() {
1039        let kinds = matching_kinds("/tmp/file.tsx", &Config::default());
1040        assert!(
1041            kinds.contains(&ServerKind::TypeScript),
1042            "expected TypeScript in {kinds:?}",
1043        );
1044    }
1045
1046    #[test]
1047    fn test_case_insensitive_extension() {
1048        let kinds = matching_kinds("/tmp/file.TS", &Config::default());
1049        assert!(
1050            kinds.contains(&ServerKind::TypeScript),
1051            "expected TypeScript in {kinds:?}",
1052        );
1053    }
1054
1055    #[test]
1056    fn test_bash_and_yaml_builtins() {
1057        assert_eq!(
1058            matching_kinds("/tmp/file.sh", &Config::default()),
1059            vec![ServerKind::Bash]
1060        );
1061        assert_eq!(
1062            matching_kinds("/tmp/file.yaml", &Config::default()),
1063            vec![ServerKind::Yaml]
1064        );
1065    }
1066
1067    #[test]
1068    fn test_ty_requires_experimental_flag() {
1069        assert_eq!(
1070            matching_kinds("/tmp/file.py", &Config::default()),
1071            vec![ServerKind::Python]
1072        );
1073
1074        let config = Config {
1075            experimental_lsp_ty: true,
1076            ..Config::default()
1077        };
1078        assert_eq!(
1079            matching_kinds("/tmp/file.py", &config),
1080            vec![ServerKind::Python, ServerKind::Ty]
1081        );
1082    }
1083
1084    #[test]
1085    fn test_custom_server_matches_extension() {
1086        // Use an extension that no built-in server claims so the custom
1087        // server is the sole match.
1088        let config = Config {
1089            lsp_servers: vec![UserServerDef {
1090                id: "my-custom-lsp".to_string(),
1091                extensions: vec!["xyzcustom".to_string()],
1092                binary: "my-custom-lsp".to_string(),
1093                root_markers: vec!["custom.toml".to_string()],
1094                ..UserServerDef::default()
1095            }],
1096            ..Config::default()
1097        };
1098
1099        assert_eq!(
1100            matching_kinds("/tmp/file.xyzcustom", &config),
1101            vec![ServerKind::Custom(Arc::from("my-custom-lsp"))]
1102        );
1103    }
1104
1105    #[test]
1106    fn test_custom_server_coexists_with_builtin_for_same_extension() {
1107        // Both built-in tinymist and the user's custom override match
1108        // the same extension. Custom appears after built-ins in the chain.
1109        let config = Config {
1110            lsp_servers: vec![UserServerDef {
1111                id: "tinymist-fork".to_string(),
1112                extensions: vec!["typ".to_string()],
1113                binary: "tinymist-fork".to_string(),
1114                root_markers: vec!["typst.toml".to_string()],
1115                ..UserServerDef::default()
1116            }],
1117            ..Config::default()
1118        };
1119
1120        let kinds = matching_kinds("/tmp/file.typ", &config);
1121        assert!(kinds.contains(&ServerKind::Tinymist));
1122        assert!(kinds.contains(&ServerKind::Custom(Arc::from("tinymist-fork"))));
1123    }
1124
1125    #[test]
1126    fn test_pattern_a_servers_register_for_their_extensions() {
1127        let cases: &[(&str, ServerKind)] = &[
1128            ("/tmp/a.clj", ServerKind::Clojure),
1129            ("/tmp/a.dart", ServerKind::Dart),
1130            ("/tmp/a.ex", ServerKind::ElixirLs),
1131            ("/tmp/a.fs", ServerKind::FSharp),
1132            ("/tmp/a.gleam", ServerKind::Gleam),
1133            ("/tmp/a.hs", ServerKind::Haskell),
1134            ("/tmp/A.java", ServerKind::Jdtls),
1135            ("/tmp/a.jl", ServerKind::Julia),
1136            ("/tmp/a.nix", ServerKind::Nixd),
1137            ("/tmp/a.ml", ServerKind::OcamlLsp),
1138            ("/tmp/a.php", ServerKind::PhpIntelephense),
1139            ("/tmp/a.rb", ServerKind::RubyLsp),
1140            ("/tmp/a.swift", ServerKind::SourceKit),
1141            ("/tmp/a.cs", ServerKind::CSharp),
1142            ("/tmp/a.razor", ServerKind::Razor),
1143        ];
1144
1145        for (path, expected) in cases {
1146            let kinds = matching_kinds(path, &Config::default());
1147            assert!(
1148                kinds.contains(expected),
1149                "expected {expected:?} for {path}; got {kinds:?}",
1150            );
1151        }
1152    }
1153
1154    #[test]
1155    fn test_pattern_c_servers_register_for_their_extensions() {
1156        let cases: &[(&str, ServerKind)] = &[
1157            ("/tmp/a.c", ServerKind::Clangd),
1158            ("/tmp/a.cpp", ServerKind::Clangd),
1159            ("/tmp/a.h", ServerKind::Clangd),
1160            ("/tmp/a.lua", ServerKind::LuaLs),
1161            ("/tmp/a.zig", ServerKind::Zls),
1162            ("/tmp/a.typ", ServerKind::Tinymist),
1163            ("/tmp/a.kt", ServerKind::KotlinLs),
1164            ("/tmp/a.tex", ServerKind::Texlab),
1165            ("/tmp/a.tf", ServerKind::TerraformLs),
1166        ];
1167
1168        for (path, expected) in cases {
1169            let kinds = matching_kinds(path, &Config::default());
1170            assert!(
1171                kinds.contains(expected),
1172                "expected {expected:?} for {path}; got {kinds:?}",
1173            );
1174        }
1175    }
1176
1177    #[test]
1178    fn test_pattern_b_d_servers_register_for_their_extensions() {
1179        let cases: &[(&str, ServerKind)] = &[
1180            ("/tmp/a.vue", ServerKind::Vue),
1181            ("/tmp/a.astro", ServerKind::Astro),
1182            ("/tmp/a.prisma", ServerKind::Prisma),
1183            ("/tmp/a.svelte", ServerKind::Svelte),
1184            ("/tmp/a.dockerfile", ServerKind::Dockerfile),
1185        ];
1186
1187        for (path, expected) in cases {
1188            let kinds = matching_kinds(path, &Config::default());
1189            assert!(
1190                kinds.contains(expected),
1191                "expected {expected:?} for {path}; got {kinds:?}",
1192            );
1193        }
1194    }
1195
1196    #[test]
1197    fn test_lsp_disabled_filters_out_servers_by_id() {
1198        let mut disabled = std::collections::HashSet::new();
1199        disabled.insert("clangd".to_string());
1200        disabled.insert("dart".to_string());
1201        disabled.insert("rust".to_string());
1202
1203        let config = Config {
1204            disabled_lsp: disabled,
1205            ..Config::default()
1206        };
1207
1208        // Disabled servers don't appear; non-disabled servers still match.
1209        let c_kinds = matching_kinds("/tmp/a.c", &config);
1210        assert!(!c_kinds.contains(&ServerKind::Clangd));
1211
1212        let dart_kinds = matching_kinds("/tmp/a.dart", &config);
1213        assert!(!dart_kinds.contains(&ServerKind::Dart));
1214
1215        let rust_kinds = matching_kinds("/tmp/a.rs", &config);
1216        assert!(!rust_kinds.contains(&ServerKind::Rust));
1217
1218        // Unrelated server still works.
1219        let ts_kinds = matching_kinds("/tmp/a.ts", &config);
1220        assert!(ts_kinds.contains(&ServerKind::TypeScript));
1221    }
1222
1223    #[test]
1224    fn test_server_kind_ids_are_unique() {
1225        // Two server defs with the same `id_str()` would collide in
1226        // `lsp.disabled` and `lsp.versions` config — protect against that.
1227        use std::collections::HashSet;
1228        let servers = super::builtin_servers();
1229        let ids: Vec<String> = servers
1230            .iter()
1231            .map(|s| s.kind.id_str().to_string())
1232            .collect();
1233        let unique: HashSet<&String> = ids.iter().collect();
1234        assert_eq!(
1235            ids.len(),
1236            unique.len(),
1237            "duplicate server IDs in registry: {ids:?}",
1238        );
1239    }
1240
1241    #[test]
1242    fn user_override_with_matching_id_replaces_builtin_not_appended() {
1243        // Issue #56: setting `lsp.servers.clangd = { args: [...] }` should
1244        // result in ONE clangd entry (the user-overridden one), not two.
1245        let config = Config {
1246            lsp_servers: vec![UserServerDef {
1247                id: "clangd".to_string(),
1248                args: vec!["--query-driver=/path/to/arm-none-eabi-*".to_string()],
1249                ..UserServerDef::default()
1250            }],
1251            ..Config::default()
1252        };
1253
1254        let cpp_servers = super::servers_for_file(Path::new("/tmp/a.cpp"), &config);
1255        let clangd_entries: Vec<_> = cpp_servers
1256            .iter()
1257            .filter(|s| s.kind.id_str() == "clangd")
1258            .collect();
1259        assert_eq!(
1260            clangd_entries.len(),
1261            1,
1262            "expected exactly one clangd server after user override; got {} ({:?})",
1263            clangd_entries.len(),
1264            cpp_servers.iter().map(|s| &s.kind).collect::<Vec<_>>()
1265        );
1266
1267        // Override fields take effect.
1268        let clangd = clangd_entries[0];
1269        assert_eq!(clangd.args, vec!["--query-driver=/path/to/arm-none-eabi-*"],);
1270
1271        // Fields the user left empty (extensions, root_markers) inherit from
1272        // the built-in — that's the whole point of the merge.
1273        assert!(
1274            !clangd.extensions.is_empty(),
1275            "extensions should inherit from built-in clangd, got empty",
1276        );
1277        assert!(
1278            !clangd.root_markers.is_empty(),
1279            "root_markers should inherit from built-in clangd, got empty",
1280        );
1281    }
1282
1283    #[test]
1284    fn user_override_preserves_builtin_kind_not_custom() {
1285        // The merged entry must keep the built-in ServerKind variant (e.g.
1286        // ServerKind::Clangd) so callers that match on the enum continue to
1287        // work — including `lsp.disabled` and any kind-specific capability
1288        // probing in the LSP manager.
1289        let config = Config {
1290            lsp_servers: vec![UserServerDef {
1291                id: "clangd".to_string(),
1292                root_markers: vec![".clangd".to_string()],
1293                ..UserServerDef::default()
1294            }],
1295            ..Config::default()
1296        };
1297
1298        let cpp_servers = super::servers_for_file(Path::new("/tmp/a.cpp"), &config);
1299        let clangd = cpp_servers
1300            .iter()
1301            .find(|s| s.kind.id_str() == "clangd")
1302            .expect("clangd entry");
1303        assert!(
1304            matches!(clangd.kind, ServerKind::Clangd),
1305            "merged server must keep ServerKind::Clangd, got {:?}",
1306            clangd.kind,
1307        );
1308    }
1309
1310    #[test]
1311    fn user_override_with_non_matching_id_is_appended_as_custom() {
1312        // Pre-existing behavior preserved: a user-defined id that doesn't
1313        // match any built-in is registered as a Custom server alongside the
1314        // built-ins. (This is the workaround issue #56 reporters were using
1315        // — it must keep working.)
1316        //
1317        // Extensions in `lsp.servers` are matched WITHOUT a leading dot
1318        // (the same convention as built-in servers — see `builtin_server()`
1319        // calls). Users writing `".cpp"` in their config would silently
1320        // never match; that's a separate UX gap not part of this fix.
1321        let config = Config {
1322            lsp_servers: vec![UserServerDef {
1323                id: "custom-clangd".to_string(),
1324                extensions: vec!["c".to_string(), "cpp".to_string()],
1325                binary: "clangd".to_string(),
1326                ..UserServerDef::default()
1327            }],
1328            ..Config::default()
1329        };
1330
1331        let cpp_servers = super::servers_for_file(Path::new("/tmp/a.cpp"), &config);
1332        let kinds: Vec<&ServerKind> = cpp_servers.iter().map(|s| &s.kind).collect();
1333        assert!(
1334            kinds.iter().any(|k| matches!(k, ServerKind::Clangd)),
1335            "built-in clangd should still be present alongside custom-clangd; got {kinds:?}",
1336        );
1337        assert!(
1338            kinds
1339                .iter()
1340                .any(|k| matches!(k, ServerKind::Custom(id) if id.as_ref() == "custom-clangd")),
1341            "custom-clangd should be appended as Custom; got {kinds:?}",
1342        );
1343    }
1344
1345    #[test]
1346    fn user_override_with_disabled_true_drops_builtin() {
1347        // `lsp.servers.clangd = { disabled: true }` should be equivalent to
1348        // adding `"clangd"` to `lsp.disabled`.
1349        let config = Config {
1350            lsp_servers: vec![UserServerDef {
1351                id: "clangd".to_string(),
1352                disabled: true,
1353                ..UserServerDef::default()
1354            }],
1355            ..Config::default()
1356        };
1357
1358        let cpp_servers = super::servers_for_file(Path::new("/tmp/a.cpp"), &config);
1359        assert!(
1360            !cpp_servers.iter().any(|s| s.kind.id_str() == "clangd"),
1361            "disabled user override should drop the built-in; got {:?}",
1362            cpp_servers.iter().map(|s| &s.kind).collect::<Vec<_>>(),
1363        );
1364    }
1365
1366    /// Helper: write an executable file containing `#!/bin/sh\n` so it
1367    /// passes both `is_file()` checks and is executable on Unix.
1368    fn touch_exe(path: &Path) {
1369        if let Some(parent) = path.parent() {
1370            std::fs::create_dir_all(parent).unwrap();
1371        }
1372        std::fs::write(path, b"#!/bin/sh\nexit 0\n").unwrap();
1373        #[cfg(unix)]
1374        {
1375            use std::os::unix::fs::PermissionsExt;
1376            let mut perms = std::fs::metadata(path).unwrap().permissions();
1377            perms.set_mode(0o755);
1378            std::fs::set_permissions(path, perms).unwrap();
1379        }
1380    }
1381
1382    #[test]
1383    fn resolve_lsp_binary_prefers_project_node_modules() {
1384        let tmp = tempfile::tempdir().unwrap();
1385        let project = tmp.path();
1386        let local_bin = project.join("node_modules").join(".bin");
1387        touch_exe(&local_bin.join("typescript-language-server"));
1388
1389        let resolved = resolve_lsp_binary("typescript-language-server", Some(project), &[]);
1390        assert_eq!(
1391            resolved.as_deref(),
1392            Some(local_bin.join("typescript-language-server").as_path())
1393        );
1394    }
1395
1396    #[test]
1397    fn resolve_lsp_binary_falls_back_to_extra_paths() {
1398        let tmp = tempfile::tempdir().unwrap();
1399        let project = tmp.path().join("project");
1400        std::fs::create_dir_all(&project).unwrap();
1401
1402        let extra_a = tmp.path().join("extra_a");
1403        let extra_b = tmp.path().join("extra_b");
1404        std::fs::create_dir_all(&extra_a).unwrap();
1405        std::fs::create_dir_all(&extra_b).unwrap();
1406        touch_exe(&extra_b.join("yaml-language-server"));
1407
1408        let resolved = resolve_lsp_binary(
1409            "yaml-language-server",
1410            Some(&project),
1411            &[extra_a.clone(), extra_b.clone()],
1412        );
1413        assert_eq!(
1414            resolved.as_deref(),
1415            Some(extra_b.join("yaml-language-server").as_path())
1416        );
1417    }
1418
1419    #[test]
1420    fn resolve_lsp_binary_extra_paths_search_in_order() {
1421        let tmp = tempfile::tempdir().unwrap();
1422        let extra_a = tmp.path().join("extra_a");
1423        let extra_b = tmp.path().join("extra_b");
1424        std::fs::create_dir_all(&extra_a).unwrap();
1425        std::fs::create_dir_all(&extra_b).unwrap();
1426        // Same binary in both — earlier path wins.
1427        touch_exe(&extra_a.join("bash-language-server"));
1428        touch_exe(&extra_b.join("bash-language-server"));
1429
1430        let resolved = resolve_lsp_binary(
1431            "bash-language-server",
1432            None,
1433            &[extra_a.clone(), extra_b.clone()],
1434        );
1435        assert_eq!(
1436            resolved.as_deref(),
1437            Some(extra_a.join("bash-language-server").as_path())
1438        );
1439    }
1440
1441    #[test]
1442    fn resolve_lsp_binary_project_root_wins_over_extra_paths() {
1443        let tmp = tempfile::tempdir().unwrap();
1444        let project = tmp.path().join("project");
1445        let local_bin = project.join("node_modules").join(".bin");
1446        touch_exe(&local_bin.join("pyright-langserver"));
1447
1448        let extra = tmp.path().join("extra");
1449        std::fs::create_dir_all(&extra).unwrap();
1450        touch_exe(&extra.join("pyright-langserver"));
1451
1452        let resolved = resolve_lsp_binary(
1453            "pyright-langserver",
1454            Some(&project),
1455            std::slice::from_ref(&extra),
1456        );
1457        assert_eq!(
1458            resolved.as_deref(),
1459            Some(local_bin.join("pyright-langserver").as_path())
1460        );
1461    }
1462
1463    #[test]
1464    fn resolve_lsp_binary_returns_none_for_missing_binary() {
1465        let tmp = tempfile::tempdir().unwrap();
1466        let project = tmp.path().join("project");
1467        std::fs::create_dir_all(&project).unwrap();
1468
1469        // Use a binary name that's almost certainly not on PATH.
1470        let resolved =
1471            resolve_lsp_binary("aft-test-nonexistent-binary-xyz123", Some(&project), &[]);
1472        assert!(resolved.is_none());
1473    }
1474
1475    #[test]
1476    fn resolve_lsp_binary_handles_missing_node_modules_gracefully() {
1477        // project_root is set but node_modules/.bin doesn't exist.
1478        // Should fall through to extra_paths and PATH without error.
1479        let tmp = tempfile::tempdir().unwrap();
1480        let project = tmp.path().join("project");
1481        std::fs::create_dir_all(&project).unwrap();
1482
1483        let extra = tmp.path().join("extra");
1484        std::fs::create_dir_all(&extra).unwrap();
1485        touch_exe(&extra.join("gopls"));
1486
1487        let resolved = resolve_lsp_binary("gopls", Some(&project), std::slice::from_ref(&extra));
1488        assert_eq!(resolved.as_deref(), Some(extra.join("gopls").as_path()));
1489    }
1490
1491    #[test]
1492    fn resolve_lsp_binary_skips_nonexistent_extra_path() {
1493        let tmp = tempfile::tempdir().unwrap();
1494        let missing = tmp.path().join("missing");
1495        let valid = tmp.path().join("valid");
1496        std::fs::create_dir_all(&valid).unwrap();
1497        touch_exe(&valid.join("clangd"));
1498
1499        let resolved = resolve_lsp_binary("clangd", None, &[missing, valid.clone()]);
1500
1501        assert_eq!(resolved.as_deref(), Some(valid.join("clangd").as_path()));
1502    }
1503
1504    #[test]
1505    fn resolve_lsp_binary_skips_file_extra_path() {
1506        let tmp = tempfile::tempdir().unwrap();
1507        let file = tmp.path().join("not-a-dir");
1508        let valid = tmp.path().join("valid");
1509        std::fs::write(&file, "not a directory").unwrap();
1510        std::fs::create_dir_all(&valid).unwrap();
1511        touch_exe(&valid.join("lua-language-server"));
1512
1513        let resolved = resolve_lsp_binary("lua-language-server", None, &[file, valid.clone()]);
1514
1515        assert_eq!(
1516            resolved.as_deref(),
1517            Some(valid.join("lua-language-server").as_path())
1518        );
1519    }
1520
1521    #[test]
1522    fn resolve_lsp_binary_skips_deleted_extra_path() {
1523        let tmp = tempfile::tempdir().unwrap();
1524        let deleted = tmp.path().join("deleted");
1525        let valid = tmp.path().join("valid");
1526        std::fs::create_dir_all(&deleted).unwrap();
1527        std::fs::remove_dir(&deleted).unwrap();
1528        std::fs::create_dir_all(&valid).unwrap();
1529        touch_exe(&valid.join("svelte-language-server"));
1530
1531        let resolved =
1532            resolve_lsp_binary("svelte-language-server", None, &[deleted, valid.clone()]);
1533
1534        assert_eq!(
1535            resolved.as_deref(),
1536            Some(valid.join("svelte-language-server").as_path())
1537        );
1538    }
1539
1540    // Avoid unused-import warning on platforms where probe_dir's Windows
1541    // branch is dead code.
1542    #[allow(dead_code)]
1543    fn _path_buf_used(_p: PathBuf) {}
1544}