Skip to content

Commit 75dd9fd

Browse files
authored
Make sure LSP is ready before adding folder (#1597)
Make sure LSP is ready before adding folder Looks like LSP was not started for these tests. Make sure we're not racing with LSP startup
1 parent 8a4c5a3 commit 75dd9fd

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/sourcekit-lsp/LanguageClientManager.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,21 @@ export class LanguageClientManager implements vscode.Disposable {
439439
}
440440

441441
private async startClient(client: LanguageClient, errorHandler: SourceKitLSPErrorHandler) {
442-
client.onDidChangeState(e => {
443-
// if state is now running add in any sub-folder workspaces that
444-
// we have cached. If this is the first time we are starting then
445-
// we won't have any sub folder workspaces, but if the server crashed
446-
// or we forced a restart then we need to do this
447-
if (e.oldState === State.Starting && e.newState === State.Running) {
448-
void this.addSubFolderWorkspaces(client);
449-
}
442+
const runningPromise = new Promise<void>((res, rej) => {
443+
const disposable = client.onDidChangeState(e => {
444+
// if state is now running add in any sub-folder workspaces that
445+
// we have cached. If this is the first time we are starting then
446+
// we won't have any sub folder workspaces, but if the server crashed
447+
// or we forced a restart then we need to do this
448+
if (e.oldState === State.Starting && e.newState === State.Running) {
449+
res();
450+
disposable.dispose();
451+
void this.addSubFolderWorkspaces(client);
452+
} else if (e.oldState === State.Starting && e.newState === State.Stopped) {
453+
rej("SourceKit-LSP failed to start");
454+
disposable.dispose();
455+
}
456+
});
450457
});
451458
if (client.clientOptions.workspaceFolder) {
452459
this.folderContext.workspaceContext.outputChannel.log(
@@ -465,6 +472,7 @@ export class LanguageClientManager implements vscode.Disposable {
465472
// start client
466473
this.clientReadyPromise = client
467474
.start()
475+
.then(() => runningPromise)
468476
.then(() => {
469477
// Now that we've started up correctly, start the error handler to auto-restart
470478
// if sourcekit-lsp crashes during normal operation.

0 commit comments

Comments
 (0)