forked from rescript-lang/rescript-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
739 lines (686 loc) · 24.1 KB
/
Copy pathserver.ts
File metadata and controls
739 lines (686 loc) · 24.1 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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
import process from "process";
import * as p from "vscode-languageserver-protocol";
import * as m from "vscode-jsonrpc/lib/messages";
import * as v from "vscode-languageserver";
import * as rpc from "vscode-jsonrpc";
import * as path from "path";
import fs from "fs";
// TODO: check DidChangeWatchedFilesNotification.
import {
DidOpenTextDocumentNotification,
DidChangeTextDocumentNotification,
DidCloseTextDocumentNotification,
} from "vscode-languageserver-protocol";
import * as utils from "./utils";
import * as c from "./constants";
import * as chokidar from "chokidar";
import { assert } from "console";
import { fileURLToPath, pathToFileURL } from "url";
import { ChildProcess } from "child_process";
import { WorkspaceEdit } from "vscode-languageserver";
import { TextEdit } from "vscode-languageserver-types";
// https://microsoft.github.io/language-server-protocol/specification#initialize
// According to the spec, there could be requests before the 'initialize' request. Link in comment tells how to handle them.
let initialized = false;
let serverSentRequestIdCounter = 0;
// https://microsoft.github.io/language-server-protocol/specification#exit
let shutdownRequestAlreadyReceived = false;
let stupidFileContentCache: Map<string, string> = new Map();
let projectsFiles: Map<
string, // project root path
{
openFiles: Set<string>;
filesWithDiagnostics: Set<string>;
bsbWatcherByEditor: null | ChildProcess;
}
> = new Map();
// ^ caching AND states AND distributed system. Why does LSP has to be stupid like this
// will be properly defined later depending on the mode (stdio/node-rpc)
let send: (msg: m.Message) => void = (_) => { };
interface CreateInterfaceRequestParams {
uri: string;
}
let createInterfaceRequest = new v.RequestType<
CreateInterfaceRequestParams,
string,
void
>("rescript-vscode.create_interface");
let sendUpdatedDiagnostics = () => {
projectsFiles.forEach(({ filesWithDiagnostics }, projectRootPath) => {
let content = fs.readFileSync(
path.join(projectRootPath, c.compilerLogPartialPath),
{ encoding: "utf-8" }
);
let { done, result: filesAndErrors } = utils.parseCompilerLogOutput(
content
);
// diff
Object.keys(filesAndErrors).forEach((file) => {
let params: p.PublishDiagnosticsParams = {
uri: file,
diagnostics: filesAndErrors[file],
};
let notification: m.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "textDocument/publishDiagnostics",
params: params,
};
send(notification);
filesWithDiagnostics.add(file);
});
if (done) {
// clear old files
filesWithDiagnostics.forEach((file) => {
if (filesAndErrors[file] == null) {
// Doesn't exist in the new diagnostics. Clear this diagnostic
let params: p.PublishDiagnosticsParams = {
uri: file,
diagnostics: [],
};
let notification: m.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "textDocument/publishDiagnostics",
params: params,
};
send(notification);
filesWithDiagnostics.delete(file);
}
});
}
});
};
let deleteProjectDiagnostics = (projectRootPath: string) => {
let root = projectsFiles.get(projectRootPath);
if (root != null) {
root.filesWithDiagnostics.forEach((file) => {
let params: p.PublishDiagnosticsParams = {
uri: file,
diagnostics: [],
};
let notification: m.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "textDocument/publishDiagnostics",
params: params,
};
send(notification);
});
projectsFiles.delete(projectRootPath);
}
};
let compilerLogsWatcher = chokidar
.watch([], {
awaitWriteFinish: {
stabilityThreshold: 1,
},
})
.on("all", (_e, changedPath) => {
sendUpdatedDiagnostics();
});
let stopWatchingCompilerLog = () => {
// TODO: cleanup of compilerLogs?
compilerLogsWatcher.close();
};
type clientSentBuildAction = {
title: string;
projectRootPath: string;
};
let openedFile = (fileUri: string, fileContent: string) => {
let filePath = fileURLToPath(fileUri);
stupidFileContentCache.set(filePath, fileContent);
let projectRootPath = utils.findProjectRootOfFile(filePath);
if (projectRootPath != null) {
if (!projectsFiles.has(projectRootPath)) {
projectsFiles.set(projectRootPath, {
openFiles: new Set(),
filesWithDiagnostics: new Set(),
bsbWatcherByEditor: null,
});
compilerLogsWatcher.add(
path.join(projectRootPath, c.compilerLogPartialPath)
);
}
let root = projectsFiles.get(projectRootPath)!;
root.openFiles.add(filePath);
let firstOpenFileOfProject = root.openFiles.size === 1;
// check if .bsb.lock is still there. If not, start a bsb -w ourselves
// because otherwise the diagnostics info we'll display might be stale
let bsbLockPath = path.join(projectRootPath, c.bsbLock);
if (firstOpenFileOfProject && !fs.existsSync(bsbLockPath)) {
// TODO: sometime stale .bsb.lock dangling. bsb -w knows .bsb.lock is
// stale. Use that logic
// TODO: close watcher when lang-server shuts down
if (utils.findNodeBuildOfProjectRoot(projectRootPath) != null) {
let payload: clientSentBuildAction = {
title: c.startBuildAction,
projectRootPath: projectRootPath,
};
let params = {
type: p.MessageType.Info,
message: `Start a build for this project to get the freshest data?`,
actions: [payload],
};
let request: m.RequestMessage = {
jsonrpc: c.jsonrpcVersion,
id: serverSentRequestIdCounter++,
method: "window/showMessageRequest",
params: params,
};
send(request);
// the client might send us back the "start build" action, which we'll
// handle in the isResponseMessage check in the message handling way
// below
} else {
// we should send something to say that we can't find bsb.exe. But right now we'll silently not do anything
}
}
// no need to call sendUpdatedDiagnostics() here; the watcher add will
// call the listener which calls it
}
};
let closedFile = (fileUri: string) => {
let filePath = fileURLToPath(fileUri);
stupidFileContentCache.delete(filePath);
let projectRootPath = utils.findProjectRootOfFile(filePath);
if (projectRootPath != null) {
let root = projectsFiles.get(projectRootPath);
if (root != null) {
root.openFiles.delete(filePath);
// clear diagnostics too if no open files open in said project
if (root.openFiles.size === 0) {
compilerLogsWatcher.unwatch(
path.join(projectRootPath, c.compilerLogPartialPath)
);
deleteProjectDiagnostics(projectRootPath);
if (root.bsbWatcherByEditor !== null) {
root.bsbWatcherByEditor.kill();
root.bsbWatcherByEditor = null;
}
}
}
}
};
let updateOpenedFile = (fileUri: string, fileContent: string) => {
let filePath = fileURLToPath(fileUri);
assert(stupidFileContentCache.has(filePath));
stupidFileContentCache.set(filePath, fileContent);
};
let getOpenedFileContent = (fileUri: string) => {
let filePath = fileURLToPath(fileUri);
let content = stupidFileContentCache.get(filePath)!;
assert(content != null);
return content;
};
// Start listening now!
// We support two modes: the regular node RPC mode for VSCode, and the --stdio
// mode for other editors The latter is _technically unsupported_. It's an
// implementation detail that might change at any time
if (process.argv.includes("--stdio")) {
let writer = new rpc.StreamMessageWriter(process.stdout);
let reader = new rpc.StreamMessageReader(process.stdin);
// proper `this` scope for writer
send = (msg: m.Message) => writer.write(msg);
reader.listen(onMessage);
} else {
// proper `this` scope for process
send = (msg: m.Message) => process.send!(msg);
process.on("message", onMessage);
}
function hover(msg: p.RequestMessage) {
let params = msg.params as p.HoverParams;
let filePath = fileURLToPath(params.textDocument.uri);
let response = utils.runAnalysisCommand(
filePath,
["hover", filePath, params.position.line, params.position.character],
msg
);
return response;
}
function definition(msg: p.RequestMessage) {
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition
let params = msg.params as p.DefinitionParams;
let filePath = fileURLToPath(params.textDocument.uri);
let response = utils.runAnalysisCommand(
filePath,
["definition", filePath, params.position.line, params.position.character],
msg
);
return response;
}
function references(msg: p.RequestMessage) {
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references
let params = msg.params as p.ReferenceParams;
let filePath = fileURLToPath(params.textDocument.uri);
let result: typeof p.ReferencesRequest.type = utils.getReferencesForPosition(
filePath,
params.position
);
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result,
// error: code and message set in case an exception happens during the definition request.
};
return response;
}
function prepareRename(msg: p.RequestMessage): m.ResponseMessage {
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_prepareRename
let params = msg.params as p.PrepareRenameParams;
let filePath = fileURLToPath(params.textDocument.uri);
let locations: null | p.Location[] = utils.getReferencesForPosition(
filePath,
params.position
);
let result: p.Range | null = null;
if (locations !== null) {
locations.forEach(loc => {
if (
path.normalize(fileURLToPath(loc.uri)) ===
path.normalize(fileURLToPath(params.textDocument.uri))
) {
let { start, end } = loc.range;
let pos = params.position;
if (
start.character <= pos.character &&
start.line <= pos.line &&
end.character >= pos.character &&
end.line >= pos.line
) {
result = loc.range;
};
}
});
}
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result
};
return response;
}
function rename(msg: p.RequestMessage) {
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rename
let params = msg.params as p.RenameParams;
let filePath = fileURLToPath(params.textDocument.uri);
let documentChanges:
| (p.RenameFile | p.TextDocumentEdit)[]
| null = utils.runAnalysisAfterSanityCheck(filePath, [
"rename",
filePath,
params.position.line,
params.position.character,
params.newName
]);
let result: WorkspaceEdit | null = null;
if (documentChanges !== null) {
result = { documentChanges };
}
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result
};
return response;
}
function documentSymbol(msg: p.RequestMessage) {
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentSymbol
let params = msg.params as p.DocumentSymbolParams;
let filePath = fileURLToPath(params.textDocument.uri);
let response = utils.runAnalysisCommand(
filePath,
["documentSymbol", filePath],
msg
);
return response;
}
function completion(msg: p.RequestMessage) {
let params = msg.params as p.ReferenceParams;
let filePath = fileURLToPath(params.textDocument.uri);
let code = getOpenedFileContent(params.textDocument.uri);
let tmpname = utils.createFileInTempDir();
fs.writeFileSync(tmpname, code, { encoding: "utf-8" });
let response = utils.runAnalysisCommand(
filePath,
[
"completion",
filePath,
params.position.line,
params.position.character,
tmpname,
],
msg
);
fs.unlink(tmpname, () => null);
return response;
}
function format(msg: p.RequestMessage): Array<m.Message> {
// technically, a formatting failure should reply with the error. Sadly
// the LSP alert box for these error replies sucks (e.g. doesn't actually
// display the message). In order to signal the client to display a proper
// alert box (sometime with actionable buttons), we need to first send
// back a fake success message (because each request mandates a
// response), then right away send a server notification to display a
// nicer alert. Ugh.
let fakeSuccessResponse: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result: [],
};
let params = msg.params as p.DocumentFormattingParams;
let filePath = fileURLToPath(params.textDocument.uri);
let extension = path.extname(params.textDocument.uri);
if (extension !== c.resExt && extension !== c.resiExt) {
let params: p.ShowMessageParams = {
type: p.MessageType.Error,
message: `Not a ${c.resExt} or ${c.resiExt} file. Cannot format it.`,
};
let response: m.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "window/showMessage",
params: params,
};
return [fakeSuccessResponse, response];
} else {
// See comment on findBscNativeDirOfFile for why we need
// to recursively search for bsc.exe upward
let bscNativePath = utils.findBscNativeOfFile(filePath);
if (bscNativePath === null) {
let params: p.ShowMessageParams = {
type: p.MessageType.Error,
message: `Cannot find a nearby bsc.exe in rescript or bs-platform. It's needed for formatting.`,
};
let response: m.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "window/showMessage",
params: params,
};
return [fakeSuccessResponse, response];
} else {
// code will always be defined here, even though technically it can be undefined
let code = getOpenedFileContent(params.textDocument.uri);
let formattedResult = utils.formatUsingValidBscNativePath(
code,
bscNativePath,
extension === c.resiExt
);
if (formattedResult.kind === "success") {
let max = code.length;
let result: p.TextEdit[] = [
{
range: {
start: { line: 0, character: 0 },
end: { line: max, character: max },
},
newText: formattedResult.result,
},
];
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result: result,
};
return [response];
} else {
// let the diagnostics logic display the updated syntax errors,
// from the build.
// Again, not sending the actual errors. See fakeSuccessResponse
// above for explanation
return [fakeSuccessResponse];
}
}
}
}
function createInterface(msg: p.RequestMessage): m.Message {
let params = msg.params as CreateInterfaceRequestParams;
let extension = path.extname(params.uri);
let filePath = fileURLToPath(params.uri);
let bscNativePath = utils.findBscNativeOfFile(filePath);
let projDir = utils.findProjectRootOfFile(filePath);
let code = getOpenedFileContent(params.uri);
let isReactComponent = code.includes("@react.component");
if (bscNativePath === null || projDir === null) {
let params: p.ShowMessageParams = {
type: p.MessageType.Error,
message: `Cannot find a nearby bsc.exe to generate the interface file.`,
};
let response: m.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "window/showMessage",
params: params,
};
return response;
}
if (extension !== c.resExt) {
let params: p.ShowMessageParams = {
type: p.MessageType.Error,
message: `Not a ${c.resExt} file. Cannot create an interface for it.`,
};
let response: m.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "window/showMessage",
params: params,
};
return response;
}
if (isReactComponent) {
let params: p.ShowMessageParams = {
type: p.MessageType.Error,
message: `Creating an interface with @react.component is not currently supported.`,
};
let response: m.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "window/showMessage",
params: params,
};
return response;
}
let cmiPartialPath = utils.replaceFileExtension(
filePath.split(projDir)[1],
c.cmiExt
);
let cmiPath = path.join(projDir, c.compilerDirPartialPath, cmiPartialPath);
let cmiAvailable = fs.existsSync(cmiPath);
if (!cmiAvailable) {
let params: p.ShowMessageParams = {
type: p.MessageType.Error,
message: `No compiled interface file found. Please compile your project first.`,
};
let response: m.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "window/showMessage",
params,
};
return response;
}
let intfResult = utils.createInterfaceFileUsingValidBscExePath(
filePath,
cmiPath,
bscNativePath
);
if (intfResult.kind === "success") {
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result: intfResult.result,
};
return response;
} else {
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
error: {
code: m.ErrorCodes.InternalError,
message: "Unable to create interface file.",
},
};
return response;
}
}
function onMessage(msg: m.Message) {
if (m.isNotificationMessage(msg)) {
// notification message, aka the client ends it and doesn't want a reply
if (!initialized && msg.method !== "exit") {
// From spec: "Notifications should be dropped, except for the exit notification. This will allow the exit of a server without an initialize request"
// For us: do nothing. We don't have anything we need to clean up right now
// TODO: we might have things we need to clean up now... like some watcher stuff
} else if (msg.method === "exit") {
// The server should exit with success code 0 if the shutdown request has been received before; otherwise with error code 1
if (shutdownRequestAlreadyReceived) {
process.exit(0);
} else {
process.exit(1);
}
} else if (msg.method === DidOpenTextDocumentNotification.method) {
let params = msg.params as p.DidOpenTextDocumentParams;
let extName = path.extname(params.textDocument.uri);
if (extName === c.resExt || extName === c.resiExt) {
openedFile(params.textDocument.uri, params.textDocument.text);
}
} else if (msg.method === DidChangeTextDocumentNotification.method) {
let params = msg.params as p.DidChangeTextDocumentParams;
let extName = path.extname(params.textDocument.uri);
if (extName === c.resExt || extName === c.resiExt) {
let changes = params.contentChanges;
if (changes.length === 0) {
// no change?
} else {
// we currently only support full changes
updateOpenedFile(
params.textDocument.uri,
changes[changes.length - 1].text
);
}
}
} else if (msg.method === DidCloseTextDocumentNotification.method) {
let params = msg.params as p.DidCloseTextDocumentParams;
closedFile(params.textDocument.uri);
}
} else if (m.isRequestMessage(msg)) {
// request message, aka client sent request and waits for our mandatory reply
if (!initialized && msg.method !== "initialize") {
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
error: {
code: m.ErrorCodes.ServerNotInitialized,
message: "Server not initialized.",
},
};
send(response);
} else if (msg.method === "initialize") {
// send the list of features we support
let result: p.InitializeResult = {
// This tells the client: "hey, we support the following operations".
// Example: we want to expose "jump-to-definition".
// By adding `definitionProvider: true`, the client will now send "jump-to-definition" requests.
capabilities: {
// TODO: incremental sync?
textDocumentSync: v.TextDocumentSyncKind.Full,
documentFormattingProvider: true,
hoverProvider: true,
definitionProvider: true,
referencesProvider: true,
renameProvider: { prepareProvider: true },
documentSymbolProvider: false,
completionProvider: { triggerCharacters: [".", ">", "@", "~"] },
},
};
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result: result,
};
initialized = true;
send(response);
} else if (msg.method === "initialized") {
// sent from client after initialize. Nothing to do for now
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result: null,
};
send(response);
} else if (msg.method === "shutdown") {
// https://microsoft.github.io/language-server-protocol/specification#shutdown
if (shutdownRequestAlreadyReceived) {
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
error: {
code: m.ErrorCodes.InvalidRequest,
message: `Language server already received the shutdown request`,
},
};
send(response);
} else {
shutdownRequestAlreadyReceived = true;
// TODO: recheck logic around init/shutdown...
stopWatchingCompilerLog();
// TODO: delete bsb watchers
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result: null,
};
send(response);
}
} else if (msg.method === p.HoverRequest.method) {
send(hover(msg));
} else if (msg.method === p.DefinitionRequest.method) {
send(definition(msg));
} else if (msg.method === p.ReferencesRequest.method) {
send(references(msg));
} else if (msg.method === p.PrepareRenameRequest.method) {
send(prepareRename(msg));
} else if (msg.method === p.RenameRequest.method) {
send(rename(msg));
} else if (msg.method === p.DocumentSymbolRequest.method) {
send(documentSymbol(msg));
} else if (msg.method === p.CompletionRequest.method) {
send(completion(msg));
} else if (msg.method === p.DocumentFormattingRequest.method) {
let responses = format(msg);
responses.forEach((response) => send(response));
} else if (msg.method === createInterfaceRequest.method) {
send(createInterface(msg));
} else {
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
error: {
code: m.ErrorCodes.InvalidRequest,
message: "Unrecognized editor request.",
},
};
send(response);
}
} else if (m.isResponseMessage(msg)) {
// response message. Currently the client should have only sent a response
// for asking us to start the build (see window/showMessageRequest in this
// file)
if (
msg.result != null &&
// @ts-ignore
msg.result.title != null &&
// @ts-ignore
msg.result.title === c.startBuildAction
) {
let msg_ = msg.result as clientSentBuildAction;
let projectRootPath = msg_.projectRootPath;
// TODO: sometime stale .bsb.lock dangling
// TODO: close watcher when lang-server shuts down. However, by Node's
// default, these subprocesses are automatically killed when this
// language-server process exits
let found = utils.findNodeBuildOfProjectRoot(projectRootPath);
if (found != null) {
let bsbProcess = utils.runBuildWatcherUsingValidBuildPath(
found.buildPath,
found.isReScript,
projectRootPath
);
let root = projectsFiles.get(projectRootPath)!;
root.bsbWatcherByEditor = bsbProcess;
// bsbProcess.on("message", (a) => console.log(a));
}
}
}
}