import { spawn, type ChildProcess } from 'node:child_process'; import { stat } from 'node:fs/promises'; import { extname, isAbsolute, relative, resolve, sep } from 'node:path'; import { pathToFileURL } from 'node:url'; import { createMessageConnection, StreamMessageReader, StreamMessageWriter, type MessageConnection, CancellationTokenSource, } from 'vscode-jsonrpc/node.js'; import { sanitizeCredentialEnv } from '../../config/configService.js'; import { onFileMutation } from '../shared/fileMutationBus.js'; import { killTree } from '../shared/killTree.js'; import { readTextFile } from '../shared/fileUtils.js'; import { discoverLspServers, lspAvailabilitySignature, type ResolvedLspServerConfig, } from './config.js'; type Diagnostic = Record; const REQUEST_TIMEOUT_MS = 20_000; const IDLE_TIMEOUT_MS = 5 * 60_000; function withTimeout(promise: Promise, timeoutMs: number, label: string, signal?: AbortSignal): Promise { return new Promise((resolvePromise, reject) => { const onAbort = () => { cleanup(); reject(new DOMException('Aborted', 'AbortError')); }; const cleanup = () => { clearTimeout(timer); signal?.removeEventListener('abort', onAbort); }; const timer = setTimeout(() => { cleanup(); reject(new Error(`${label} timed out after ${timeoutMs}ms`)); }, timeoutMs); signal?.addEventListener('abort', onAbort, { once: true }); if (signal?.aborted) onAbort(); promise.then((value) => { cleanup(); resolvePromise(value); }, (error) => { cleanup(); reject(error); }); }); } export class LspServer { private child?: ChildProcess; private connection?: MessageConnection; private starting?: Promise; private opened = new Map(); private desiredDocuments = new Map(); private idleTimer?: ReturnType; private connectionGeneration = 0; private connectionClosed?: Promise; private resolveConnectionClosed?: () => void; readonly diagnostics = new Map(); constructor(readonly config: ResolvedLspServerConfig, readonly cwd: string) {} private touch(): void { if (this.idleTimer) clearTimeout(this.idleTimer); this.idleTimer = setTimeout(() => { void this.stop(); }, IDLE_TIMEOUT_MS); this.idleTimer.unref?.(); } async start(): Promise { if (this.starting) return this.starting; if (this.connection) return; this.starting = this.startInner().finally(() => { this.starting = undefined; }); return this.starting; } private async startInner(): Promise { const child = spawn(this.config.command, this.config.args ?? [], { cwd: this.cwd, env: { ...sanitizeCredentialEnv(), ...this.config.env }, stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true, }); await new Promise((resolvePromise, reject) => { child.once('spawn', resolvePromise); child.once('error', reject); }); if (!child.stdin || !child.stdout) throw new Error('LSP server stdio unavailable'); // Most language servers write diagnostics to stderr. We intentionally do not // expose that stream to the model, but it still must be drained or a chatty // server can fill the OS pipe and deadlock its JSON-RPC stdout. child.stderr?.resume(); this.child = child; const connection = createMessageConnection(new StreamMessageReader(child.stdout), new StreamMessageWriter(child.stdin)); this.connection = connection; this.connectionClosed = new Promise((resolvePromise) => { this.resolveConnectionClosed = resolvePromise; }); const generation = ++this.connectionGeneration; this.diagnostics.clear(); connection.onRequest('workspace/configuration', (params: { items?: unknown[] }) => (params.items ?? []).map(() => null)); connection.onNotification('textDocument/publishDiagnostics', (params: { uri?: string; diagnostics?: Diagnostic[] }) => { if (params.uri && generation === this.connectionGeneration) this.diagnostics.set(params.uri, params.diagnostics ?? []); }); connection.onClose(() => { if (generation !== this.connectionGeneration) return; this.connection = undefined; this.resolveConnectionClosed?.(); this.opened.clear(); this.diagnostics.clear(); }); child.once('close', () => { if (this.child === child) this.child = undefined; }); connection.listen(); const rootUri = pathToFileURL(this.cwd).href; try { await withTimeout(connection.sendRequest('initialize', { processId: process.pid, rootUri, rootPath: this.cwd, workspaceFolders: [{ uri: rootUri, name: this.cwd.split(/[\\/]/).pop() ?? 'workspace' }], initializationOptions: this.config.initializationOptions ?? {}, capabilities: { workspace: { configuration: false, workspaceFolders: false }, textDocument: { synchronization: { dynamicRegistration: false, didSave: true }, hover: { contentFormat: ['markdown', 'plaintext'] }, definition: { linkSupport: true }, references: {}, documentSymbol: { hierarchicalDocumentSymbolSupport: true }, callHierarchy: {}, publishDiagnostics: { relatedInformation: true }, }, general: { positionEncodings: ['utf-16'] }, }, }), this.config.startupTimeoutMs ?? 10_000, `LSP ${this.config.name} initialize`); await connection.sendNotification('initialized', {}); for (const [uri, filePath] of this.desiredDocuments) { await this.sendDidOpen(filePath).catch(() => { this.desiredDocuments.delete(uri); this.diagnostics.delete(uri); }); } } catch (error) { await this.stop(); throw error; } this.touch(); } async openFile(filePath: string, signal?: AbortSignal): Promise { signal?.throwIfAborted(); const absolute = resolve(filePath); const uri = pathToFileURL(absolute).href; this.desiredDocuments.set(uri, absolute); await withTimeout(this.start(), REQUEST_TIMEOUT_MS, 'LSP startup', signal); signal?.throwIfAborted(); this.desiredDocuments.set(uri, absolute); const info = await stat(absolute); const existing = this.opened.get(uri); if (existing) { if (existing.mtimeMs !== info.mtimeMs || existing.size !== info.size) { await this.fileChanged(absolute); } return; } await this.sendDidOpen(absolute); } private async sendDidOpen(absolute: string): Promise { const uri = pathToFileURL(absolute).href; const info = await stat(absolute); const text = (await readTextFile(absolute)).content; const languageId = this.config.extensionToLanguage[extname(absolute).toLowerCase()] ?? 'plaintext'; await this.connection!.sendNotification('textDocument/didOpen', { textDocument: { uri, languageId, version: 1, text }, }); this.opened.set(uri, { version: 1, mtimeMs: info.mtimeMs, size: info.size }); } async fileChanged(filePath: string): Promise { const uri = pathToFileURL(resolve(filePath)).href; const opened = this.opened.get(uri); if (!this.connection || opened === undefined) return; try { const text = (await readTextFile(filePath)).content; const info = await stat(filePath); const version = opened.version + 1; this.diagnostics.delete(uri); await this.connection.sendNotification('textDocument/didChange', { textDocument: { uri, version }, contentChanges: [{ text }], }); await this.connection.sendNotification('textDocument/didSave', { textDocument: { uri } }); this.opened.set(uri, { version, mtimeMs: info.mtimeMs, size: info.size }); } catch {} } async request(method: string, params: unknown, signal?: AbortSignal): Promise { signal?.throwIfAborted(); await withTimeout(this.start(), REQUEST_TIMEOUT_MS, 'LSP startup', signal); this.touch(); let last: unknown; for (let attempt = 0; attempt < 3; attempt += 1) { const cancellation = new CancellationTokenSource(); try { signal?.throwIfAborted(); const connection = this.connection!; const closed = this.connectionClosed?.then(() => { throw Object.assign(new Error('LSP connection closed during request'), { code: 1 }); }); return await withTimeout( closed ? Promise.race([connection.sendRequest(method, params, cancellation.token), closed]) : connection.sendRequest(method, params, cancellation.token), REQUEST_TIMEOUT_MS, method, signal, ); } catch (error) { if (signal?.aborted || /timed out/i.test((error as Error).message)) cancellation.cancel(); if (signal?.aborted) throw error; last = error; const code = (error as { code?: number }).code; const retryable = code === -32801 || code === 1 || code === 2 || /closed|disposed|write after end/i.test((error as Error).message) || !this.connection; if (!retryable || attempt === 2) break; await this.stop(false); await this.start(); await new Promise((resolvePromise) => setTimeout(resolvePromise, 250 * 2 ** attempt)); } finally { cancellation.dispose(); } } throw last; } async stop(forgetDocuments = false): Promise { if (this.idleTimer) clearTimeout(this.idleTimer); const connection = this.connection; this.connection = undefined; this.resolveConnectionClosed?.(); this.connectionClosed = undefined; this.resolveConnectionClosed = undefined; this.opened.clear(); this.diagnostics.clear(); this.connectionGeneration += 1; if (forgetDocuments) this.desiredDocuments.clear(); if (connection) { try { await withTimeout(connection.sendRequest('shutdown'), 1_000, 'LSP shutdown'); } catch {} try { await connection.sendNotification('exit'); } catch {} try { connection.dispose(); } catch {} } if (this.child) killTree(this.child); this.child = undefined; } forceStopSync(): void { if (this.idleTimer) clearTimeout(this.idleTimer); this.connectionGeneration += 1; this.connection?.dispose(); this.connection = undefined; this.resolveConnectionClosed?.(); this.connectionClosed = undefined; this.resolveConnectionClosed = undefined; this.opened.clear(); this.desiredDocuments.clear(); this.diagnostics.clear(); if (this.child) killTree(this.child); this.child = undefined; } } export class WorkspaceLspManager { private servers = new Map(); constructor(readonly cwd: string) { for (const config of discoverLspServers(cwd)) this.servers.set(config.name, new LspServer(config, cwd)); } serverFor(filePath: string): LspServer | undefined { const extension = extname(filePath).toLowerCase(); return [...this.servers.values()].find((server) => extension in server.config.extensionToLanguage); } fileChanged(filePath: string): void { const rel = relative(this.cwd, resolve(filePath)); if (rel === '..' || rel.startsWith(`..${sep}`) || isAbsolute(rel)) return; const server = this.serverFor(filePath); if (server) void server.fileChanged(filePath); } stop(): Promise { return Promise.all([...this.servers.values()].map((server) => server.stop(true))); } forceStopSync(): void { for (const server of this.servers.values()) server.forceStopSync(); } } const managers = new Map(); const MAX_LSP_MANAGERS = 32; onFileMutation((path) => { for (const entry of managers.values()) entry.manager.fileChanged(path); }); export function getLspManager(cwd: string): WorkspaceLspManager { const key = resolve(cwd); const signature = lspAvailabilitySignature(key); let entry = managers.get(key); if (!entry || entry.signature !== signature) { if (entry) void entry.manager.stop(); entry = { signature, manager: new WorkspaceLspManager(key), lastUsedAt: Date.now() }; managers.set(key, entry); } entry.lastUsedAt = Date.now(); if (managers.size > MAX_LSP_MANAGERS) { const victim = [...managers.entries()] .filter(([candidate]) => candidate !== key) .sort((a, b) => a[1].lastUsedAt - b[1].lastUsedAt)[0]; if (victim) { managers.delete(victim[0]); void victim[1].manager.stop(); } } return entry.manager; } export async function shutdownAllLsp(): Promise { await Promise.all([...managers.values()].map((entry) => entry.manager.stop())); managers.clear(); } process.once('exit', () => { for (const entry of managers.values()) entry.manager.forceStopSync(); });