-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlanguage-server.ts
More file actions
645 lines (567 loc) · 18.2 KB
/
language-server.ts
File metadata and controls
645 lines (567 loc) · 18.2 KB
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/**
* CodeQL Language Server manager for LSP communication
* Manages the lifecycle and communication with the CodeQL language server process
*/
import { spawn, ChildProcess } from 'child_process';
import { EventEmitter } from 'events';
import { setTimeout, clearTimeout } from 'timers';
import { pathToFileURL } from 'url';
import { delimiter, join } from 'path';
import { logger } from '../utils/logger';
import { getPackageVersion } from '../utils/package-paths';
import { getProjectTmpDir } from '../utils/temp-dir';
import { getResolvedCodeQLDir } from './cli-executor';
import { waitForProcessReady } from '../utils/process-ready';
export interface LSPMessage {
jsonrpc: '2.0';
id?: number | string;
method: string;
params?: unknown;
result?: unknown;
error?: {
code: number;
message: string;
data?: unknown;
};
}
export interface Diagnostic {
range: {
start: { line: number; character: number };
end: { line: number; character: number };
};
severity: number; // 1=Error, 2=Warning, 3=Information, 4=Hint
source?: string;
message: string;
code?: string | number;
}
export interface PublishDiagnosticsParams {
uri: string;
diagnostics: Diagnostic[];
}
export interface LanguageServerOptions {
searchPath?: string;
logdir?: string;
loglevel?: 'ALL' | 'DEBUG' | 'ERROR' | 'INFO' | 'OFF' | 'TRACE' | 'WARN';
synchronous?: boolean;
verbosity?: 'errors' | 'progress' | 'progress+' | 'progress++' | 'progress+++' | 'warnings';
}
/**
* Position in a text document (0-based line and character).
*/
export interface LSPPosition {
character: number;
line: number;
}
/**
* A range in a text document.
*/
export interface LSPRange {
end: LSPPosition;
start: LSPPosition;
}
/**
* A location in a resource (file URI + range).
*/
export interface LSPLocation {
range: LSPRange;
uri: string;
}
/**
* Identifies a text document by its URI.
*/
export interface TextDocumentIdentifier {
uri: string;
}
/**
* A text document position (document + position within it).
*/
export interface TextDocumentPositionParams {
position: LSPPosition;
textDocument: TextDocumentIdentifier;
}
/**
* A completion item returned by the language server.
*/
export interface CompletionItem {
detail?: string;
documentation?: string | { kind: string; value: string };
insertText?: string;
kind?: number;
label: string;
sortText?: string;
}
/**
* Symbol kinds as defined by the LSP spec.
*/
/* eslint-disable no-unused-vars */
export enum SymbolKind {
File = 1,
Module = 2,
Namespace = 3,
Package = 4,
Class = 5,
Method = 6,
Property = 7,
Field = 8,
Constructor = 9,
Enum = 10,
Interface = 11,
Function = 12,
Variable = 13,
Constant = 14,
String = 15,
Number = 16,
Boolean = 17,
Array = 18,
Object = 19,
Key = 20,
Null = 21,
EnumMember = 22,
Struct = 23,
Event = 24,
Operator = 25,
TypeParameter = 26,
}
/* eslint-enable no-unused-vars */
/**
* Hierarchical document symbol (returned when the server supports hierarchical symbols).
*/
export interface DocumentSymbol {
children?: DocumentSymbol[];
detail?: string;
kind: SymbolKind;
name: string;
range: LSPRange;
selectionRange: LSPRange;
}
/**
* Flat symbol information (returned when the server does not support hierarchical symbols).
*/
export interface SymbolInformation {
containerName?: string;
kind: SymbolKind;
location: LSPLocation;
name: string;
}
export class CodeQLLanguageServer extends EventEmitter {
private server: ChildProcess | null = null;
private messageId = 1;
private pendingResponses = new Map<number, { resolve: (_value: unknown) => void; reject: (_error: Error) => void }>();
private isInitialized = false;
private currentWorkspaceUri: string | undefined;
private messageBuffer = '';
constructor(private _options: LanguageServerOptions = {}) {
super();
}
async start(): Promise<void> {
if (this.server) {
throw new Error('Language server is already running');
}
logger.info('Starting CodeQL Language Server...');
const args = [
'execute', 'language-server',
'--check-errors=ON_CHANGE'
];
// Add optional arguments
if (this._options.searchPath) {
args.push(`--search-path=${this._options.searchPath}`);
}
if (this._options.logdir) {
args.push(`--logdir=${this._options.logdir}`);
}
if (this._options.loglevel) {
args.push(`--loglevel=${this._options.loglevel}`);
}
if (this._options.synchronous) {
args.push('--synchronous');
}
if (this._options.verbosity) {
args.push(`--verbosity=${this._options.verbosity}`);
}
// Build environment with CODEQL_PATH directory prepended to PATH
// (mirrors the approach in cli-executor.ts getSafeEnvironment).
const spawnEnv = { ...process.env };
const codeqlDir = getResolvedCodeQLDir();
if (codeqlDir && spawnEnv.PATH) {
spawnEnv.PATH = `${codeqlDir}${delimiter}${spawnEnv.PATH}`;
} else if (codeqlDir) {
spawnEnv.PATH = codeqlDir;
}
this.server = spawn('codeql', args, {
stdio: ['pipe', 'pipe', 'pipe'],
env: spawnEnv
});
this.server.stderr?.on('data', (data) => {
logger.debug('CodeQL LS stderr:', data.toString());
});
this.server.stdout?.on('data', (data) => {
this.handleStdout(data);
});
this.server.on('error', (error) => {
logger.error('CodeQL Language Server error:', error);
this.emit('error', error);
});
this.server.on('exit', (code) => {
logger.info('CodeQL Language Server exited with code:', code);
this.server = null;
this.isInitialized = false;
this.emit('exit', code);
});
// Wait for the JVM to initialise (resolves on first stderr/stdout output)
await waitForProcessReady(this.server, 'CodeQL Language Server');
}
private handleStdout(data: Buffer): void {
this.messageBuffer += data.toString();
let headerEnd = this.messageBuffer.indexOf('\r\n\r\n');
while (headerEnd !== -1) {
const header = this.messageBuffer.substring(0, headerEnd);
const contentLengthMatch = header.match(/Content-Length: (\d+)/);
if (contentLengthMatch) {
const contentLength = parseInt(contentLengthMatch[1]);
const messageStart = headerEnd + 4;
const messageEnd = messageStart + contentLength;
if (this.messageBuffer.length >= messageEnd) {
const messageContent = this.messageBuffer.substring(messageStart, messageEnd);
this.messageBuffer = this.messageBuffer.substring(messageEnd);
try {
const message: LSPMessage = JSON.parse(messageContent);
this.handleMessage(message);
} catch (error) {
logger.error('Failed to parse LSP message:', error, messageContent);
}
headerEnd = this.messageBuffer.indexOf('\r\n\r\n');
} else {
break;
}
} else {
logger.error('Invalid LSP header:', header);
this.messageBuffer = '';
break;
}
}
}
private handleMessage(message: LSPMessage): void {
logger.debug('Received LSP message:', message);
// Handle responses to our requests
if (message.id !== undefined && this.pendingResponses.has(Number(message.id))) {
const pending = this.pendingResponses.get(Number(message.id))!;
this.pendingResponses.delete(Number(message.id));
if (message.error) {
pending.reject(new Error(`LSP Error: ${message.error.message}`));
} else {
pending.resolve(message.result);
}
return;
}
// Handle notifications from server
if (message.method === 'textDocument/publishDiagnostics') {
this.emit('diagnostics', message.params as PublishDiagnosticsParams);
}
}
private sendMessage(message: LSPMessage): void {
if (!this.server?.stdin) {
throw new Error('Language server is not running');
}
const messageStr = JSON.stringify(message);
const contentLength = Buffer.byteLength(messageStr, 'utf8');
const header = `Content-Length: ${contentLength}\r\n\r\n`;
const fullMessage = header + messageStr;
logger.debug('Sending LSP message:', fullMessage);
this.server.stdin.write(fullMessage);
}
private sendRequest(method: string, params?: unknown): Promise<unknown> {
const id = this.messageId++;
const message: LSPMessage = {
jsonrpc: '2.0',
id,
method,
params
};
return new Promise((resolve, reject) => {
// Wrap resolve/reject to clear the timer when the promise settles.
const timer = setTimeout(() => {
if (this.pendingResponses.has(id)) {
this.pendingResponses.delete(id);
reject(new Error(`LSP request timeout for method: ${method}`));
}
}, 60_000); // 60 second timeout (Windows CI cold JVM can exceed 30s)
this.pendingResponses.set(id, {
reject: (err: Error) => { clearTimeout(timer); reject(err); },
resolve: (val: unknown) => { clearTimeout(timer); resolve(val); },
});
this.sendMessage(message);
});
}
private sendNotification(method: string, params?: unknown): void {
const message: LSPMessage = {
jsonrpc: '2.0',
method,
params
};
this.sendMessage(message);
}
/**
* Initialize the language server with an optional workspace URI.
*
* If the server is already initialized with a different workspace, a
* `workspace/didChangeWorkspaceFolders` notification is sent to update
* the workspace context instead of requiring a full restart.
*/
async initialize(workspaceUri?: string): Promise<void> {
if (this.isInitialized) {
// If workspace changed, notify the server
if (workspaceUri && workspaceUri !== this.currentWorkspaceUri) {
await this.updateWorkspace(workspaceUri);
}
return;
}
logger.info('Initializing CodeQL Language Server...');
const initParams = {
processId: process.pid,
clientInfo: {
name: 'codeql-development-mcp-server',
version: getPackageVersion()
},
capabilities: {
textDocument: {
completion: { completionItem: { snippetSupport: false } },
definition: {},
publishDiagnostics: {},
references: {},
synchronization: {
didClose: true,
didChange: true,
didOpen: true,
},
},
workspace: {
workspaceFolders: true,
},
}
};
if (workspaceUri) {
(initParams as unknown as { workspaceFolders: unknown[] }).workspaceFolders = [{
uri: workspaceUri,
name: 'codeql-workspace'
}];
}
await this.sendRequest('initialize', initParams);
this.sendNotification('initialized', {});
this.currentWorkspaceUri = workspaceUri;
this.isInitialized = true;
logger.info('CodeQL Language Server initialized successfully');
}
/**
* Update the workspace folders on a running, initialized server.
*/
private async updateWorkspace(newUri: string): Promise<void> {
logger.info(`Updating workspace from ${this.currentWorkspaceUri} to ${newUri}`);
const removed = this.currentWorkspaceUri
? [{ uri: this.currentWorkspaceUri, name: 'codeql-workspace' }]
: [];
this.sendNotification('workspace/didChangeWorkspaceFolders', {
event: {
added: [{ uri: newUri, name: 'codeql-workspace' }],
removed,
},
});
this.currentWorkspaceUri = newUri;
}
/**
* Get the current workspace URI.
*/
getWorkspaceUri(): string | undefined {
return this.currentWorkspaceUri;
}
async evaluateQL(qlCode: string, uri?: string): Promise<Diagnostic[]> {
if (!this.isInitialized) {
throw new Error('Language server is not initialized');
}
// Default to a project-local virtual URI rather than /tmp
const documentUri = uri || pathToFileURL(join(getProjectTmpDir('lsp-eval'), 'eval.ql')).href;
return new Promise((resolve, reject) => {
let diagnosticsReceived = false;
const timeout = setTimeout(() => {
if (!diagnosticsReceived) {
this.removeListener('diagnostics', diagnosticsHandler);
reject(new Error('Timeout waiting for diagnostics'));
}
}, 90_000); // 90s — first call triggers JVM start + compilation; Windows CI is slow
// Listen for diagnostics
const diagnosticsHandler = (params: PublishDiagnosticsParams) => {
if (params.uri === documentUri) {
diagnosticsReceived = true;
clearTimeout(timeout);
this.removeListener('diagnostics', diagnosticsHandler);
// Close the document
this.sendNotification('textDocument/didClose', {
textDocument: { uri: documentUri }
});
resolve(params.diagnostics);
}
};
this.on('diagnostics', diagnosticsHandler);
// Open the document with the QL code
this.sendNotification('textDocument/didOpen', {
textDocument: {
uri: documentUri,
languageId: 'ql',
version: 1,
text: qlCode
}
});
});
}
// ---- LSP feature methods (issue #1) ----
/**
* Get code completions at a position in a document.
*/
async getCompletions(params: TextDocumentPositionParams): Promise<CompletionItem[]> {
if (!this.isInitialized) {
throw new Error('Language server is not initialized');
}
if (!this.isRunning()) {
throw new Error('Language server process is not running');
}
const result = await this.sendRequest('textDocument/completion', params);
// The result may be a CompletionList or CompletionItem[]
if (result && typeof result === 'object' && 'items' in (result as object)) {
return (result as { items: CompletionItem[] }).items;
}
return (result as CompletionItem[]) || [];
}
/**
* Find the definition(s) of a symbol at a position.
*/
async getDefinition(params: TextDocumentPositionParams): Promise<LSPLocation[]> {
if (!this.isInitialized) {
throw new Error('Language server is not initialized');
}
if (!this.isRunning()) {
throw new Error('Language server process is not running');
}
const result = await this.sendRequest('textDocument/definition', params);
return this.normalizeLocations(result);
}
/**
* Find all references to a symbol at a position.
*/
async getReferences(params: TextDocumentPositionParams & { context?: { includeDeclaration: boolean } }): Promise<LSPLocation[]> {
if (!this.isInitialized) {
throw new Error('Language server is not initialized');
}
if (!this.isRunning()) {
throw new Error('Language server process is not running');
}
const result = await this.sendRequest('textDocument/references', {
...params,
context: params.context ?? { includeDeclaration: true },
});
return this.normalizeLocations(result);
}
/**
* Get document symbols (i.e. top-level declarations) for a file.
* Returns a hierarchical DocumentSymbol[] when the server supports it, or a
* flat SymbolInformation[] otherwise. Top-level symbols are the root nodes
* of the returned array.
*/
async getDocumentSymbols(params: { textDocument: TextDocumentIdentifier }): Promise<DocumentSymbol[] | SymbolInformation[]> {
if (!this.isInitialized) {
throw new Error('Language server is not initialized');
}
if (!this.isRunning()) {
throw new Error('Language server process is not running');
}
const result = await this.sendRequest('textDocument/documentSymbol', params);
if (!result || !Array.isArray(result) || result.length === 0) {
return [];
}
// Hierarchical DocumentSymbol items have a `selectionRange` field.
if ('selectionRange' in (result[0] as object)) {
return result as DocumentSymbol[];
}
// Flat SymbolInformation items have a `location` field.
return result as SymbolInformation[];
}
/**
* Open a text document in the language server.
* The document must be opened before requesting completions, definitions, etc.
*/
openDocument(uri: string, text: string, languageId = 'ql', version = 1): void {
if (!this.isInitialized) {
throw new Error('Language server is not initialized');
}
this.sendNotification('textDocument/didOpen', {
textDocument: { uri, languageId, version, text },
});
}
/**
* Close a text document in the language server.
*/
closeDocument(uri: string): void {
if (!this.isInitialized) {
throw new Error('Language server is not initialized');
}
this.sendNotification('textDocument/didClose', {
textDocument: { uri },
});
}
/**
* Normalize a definition/references/implementation result to Location[].
* The LSP spec allows Location | Location[] | LocationLink[].
*/
private normalizeLocations(result: unknown): LSPLocation[] {
if (!result) return [];
if (Array.isArray(result)) {
return result.map((item) => {
// LocationLink has targetUri/targetRange
if ('targetUri' in item) {
return { uri: item.targetUri, range: item.targetRange } as LSPLocation;
}
return item as LSPLocation;
});
}
// Single Location
if (typeof result === 'object' && 'uri' in (result as object)) {
return [result as LSPLocation];
}
return [];
}
async shutdown(): Promise<void> {
if (!this.server) {
return;
}
logger.info('Shutting down CodeQL Language Server...');
try {
await this.sendRequest('shutdown', {});
if (this.server) {
this.sendNotification('exit', {});
}
} catch (error) {
logger.warn('Error during graceful shutdown:', error);
}
// Force kill if needed
await new Promise<void>((resolve) => {
const timer = setTimeout(() => {
if (this.server) {
this.server.kill('SIGTERM');
}
resolve();
}, 1000);
if (this.server) {
this.server.once('exit', () => {
clearTimeout(timer);
this.server = null;
resolve();
});
} else {
clearTimeout(timer);
resolve();
}
});
this.isInitialized = false;
}
isRunning(): boolean {
return this.server !== null && !this.server.killed;
}
}