// LSP server auto-install (pi-lens-inspired, same machinery as the CLI // runners): resolve an available command, and when it is missing try the // adapter's install recipes through the user's package managers, then verify // the installed server actually runs. Attempts are shared in flight by // install command so concurrent tool calls never spawn duplicate installs. import { commandPathValue, resetExtraToolDirectories } from "./command.js"; import type { InstallCandidate } from "./runners/helpers.js"; import { installKey, invalidateRunnerAvailability, resolveRunnerCommand, shareInstall, tryInstallCandidates, } from "./runners/helpers.js"; import type { InstallRecipe, LspServerAdapter } from "./types.js"; export interface LspInstallOutcome { /** The server command is resolvable now (pre-existing or just installed). */ ok: boolean; /** Set when this call auto-installed the missing server. */ autoInstalled?: boolean; /** Why installation was not attempted or failed (attached to skips/errors). */ detail?: string; } /** Probe availability through the shared TTL cache (augmented PATH included). */ export function resolveLspCommand( adapter: LspServerAdapter, cwd: string, ): string | undefined { return resolveRunnerCommand( `lsp:${adapter.name}`, adapter.defaultCommand.command, cwd, // Same augmented-PATH rule as the routes/checks commandExists probes: // an adapter with an env PATH override must resolve identically here. commandPathValue(adapter.env), ); } /** True when this adapter may auto-install: enabled on both sides with recipes. */ export function lspAutoInstallEnabled( adapter: LspServerAdapter, autoInstall?: boolean, ): boolean { return ( autoInstall !== false && adapter.autoInstall === true && ((adapter.installRecipes?.length ?? 0) > 0 || adapter.customInstaller !== undefined) ); } export async function ensureLspInstalled( adapter: LspServerAdapter, cwd: string, signal: AbortSignal | undefined, autoInstall: boolean, ): Promise { if (resolveLspCommand(adapter, cwd)) return { ok: true }; const hasRecipes = (adapter.installRecipes?.length ?? 0) > 0; if (!lspAutoInstallEnabled(adapter, autoInstall)) { const reason = !hasRecipes && !adapter.customInstaller ? "no install recipe" : "auto-install disabled"; return { ok: false, detail: reason }; } // Custom installer (download-and-extract or uv-based) replaces the // package-manager recipes entirely: when present it is the only install // channel, and its failure is terminal (no recipe fallback). if (adapter.customInstaller) { const installer = adapter.customInstaller; // Custom installers (jdtls's managed dir, uv tool shims) all target // global directories, so the attempt is shared across workspace roots: // two concurrent first-use calls from different roots must not race // their rm/rename steps on the same install target. const install = await shareInstall(`lsp:${adapter.name}`, () => installer(cwd, signal), ); invalidateRunnerAvailability(`lsp:${adapter.name}`); resetExtraToolDirectories(); if (install.installed && resolveLspCommand(adapter, cwd)) { return { ok: true, autoInstalled: true, detail: install.detail }; } return { ok: false, detail: install.detail }; } // Shared by install command (see installKey) so concurrent tool calls — // including a CLI runner requesting the same package — never spawn // duplicate installs of the same missing tool. const recipes = adapter.installRecipes ?? []; const candidates: InstallCandidate[] = recipes.map((recipe) => toCandidate(adapter, recipe), ); const install = await shareInstall(installKey(candidates), () => tryInstallCandidates(candidates, cwd, signal), ); invalidateRunnerAvailability(`lsp:${adapter.name}`); // The install may have created the tool's bin directory; rescan so the // re-resolution below does not read a stale extra-directories cache. resetExtraToolDirectories(); if (install.installed && resolveLspCommand(adapter, cwd)) { return { ok: true, autoInstalled: true, detail: install.detail }; } return { ok: false, detail: install.detail }; } function toCandidate( adapter: LspServerAdapter, recipe: InstallRecipe, ): InstallCandidate { // "The package manager reported success" is not "the server runs": probe // with the LSP command itself (verifyArgs, default --version). An empty // verifyArgs array opts out (servers without a version flag). const verify = recipe.verifyArgs && recipe.verifyArgs.length === 0 ? undefined : { command: adapter.defaultCommand.command, args: recipe.verifyArgs ?? ["--version"], }; return { label: recipe.label, command: recipe.command, args: recipe.args, verify, }; }