From 57c2d7fbb250d0178bbc51c9f971e8ac1c77f2a9 Mon Sep 17 00:00:00 2001 From: A-N-uraag Date: Sat, 18 Nov 2023 22:34:28 +0530 Subject: [PATCH] fix: LSP server crashing in case of error in schema - Added try-catch block around handleWatchedFilesChangedNotification handler to prevent server crash on schema update. --- .../src/MessageProcessor.ts | 73 ++++++++++--------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/packages/graphql-language-service-server/src/MessageProcessor.ts b/packages/graphql-language-service-server/src/MessageProcessor.ts index 4772d8f1058..3d916c77954 100644 --- a/packages/graphql-language-service-server/src/MessageProcessor.ts +++ b/packages/graphql-language-service-server/src/MessageProcessor.ts @@ -664,42 +664,47 @@ export class MessageProcessor { await this._updateFragmentDefinition(uri, contents); await this._updateObjectTypeDefinition(uri, contents); - const project = this._graphQLCache.getProjectForFile(uri); - if (project) { - await this._updateSchemaIfChanged(project, uri); - } + try { + const project = this._graphQLCache.getProjectForFile(uri); + if (project) { + await this._updateSchemaIfChanged(project, uri); + } - let diagnostics: Diagnostic[] = []; - - if ( - project?.extensions?.languageService?.enableValidation !== false - ) { - diagnostics = ( - await Promise.all( - contents.map(async ({ query, range }) => { - const results = await this._languageService.getDiagnostics( - query, - uri, - this._isRelayCompatMode(query), - ); - if (results && results.length > 0) { - return processDiagnosticsMessage(results, query, range); - } - return []; - }), - ) - ).reduce((left, right) => left.concat(right), diagnostics); - } + let diagnostics: Diagnostic[] = []; + + if ( + project?.extensions?.languageService?.enableValidation !== false + ) { + diagnostics = ( + await Promise.all( + contents.map(async ({ query, range }) => { + const results = await this._languageService.getDiagnostics( + query, + uri, + this._isRelayCompatMode(query), + ); + if (results && results.length > 0) { + return processDiagnosticsMessage(results, query, range); + } + return []; + }), + ) + ).reduce((left, right) => left.concat(right), diagnostics); + } - this._logger.log( - JSON.stringify({ - type: 'usage', - messageType: 'workspace/didChangeWatchedFiles', - projectName: project?.name, - fileName: uri, - }), - ); - return { uri, diagnostics }; + this._logger.log( + JSON.stringify({ + type: 'usage', + messageType: 'workspace/didChangeWatchedFiles', + projectName: project?.name, + fileName: uri, + }), + ); + return { uri, diagnostics }; + } catch (err) { + this._handleConfigError({ err, uri }); + return { uri, diagnostics: [] }; + } } if (change.type === FileChangeTypeKind.Deleted) { await this._graphQLCache.updateFragmentDefinitionCache(