Skip to content

Commit

Permalink
fix: LSP server crashing in case of error in schema
Browse files Browse the repository at this point in the history
- Added try-catch block around handleWatchedFilesChangedNotification
  handler to prevent server crash on schema update.
  • Loading branch information
A-N-uraag committed Nov 20, 2023
1 parent 0c64305 commit 5910332
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-steaks-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-language-service-server': patch
---

Fixed crashing of LSP server on saving a schema with errors
73 changes: 39 additions & 34 deletions packages/graphql-language-service-server/src/MessageProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check warning on line 687 in packages/graphql-language-service-server/src/MessageProcessor.ts

View check run for this annotation

Codecov / codecov/patch

packages/graphql-language-service-server/src/MessageProcessor.ts#L687

Added line #L687 was not covered by tests
}
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: [] };

Check warning on line 706 in packages/graphql-language-service-server/src/MessageProcessor.ts

View check run for this annotation

Codecov / codecov/patch

packages/graphql-language-service-server/src/MessageProcessor.ts#L705-L706

Added lines #L705 - L706 were not covered by tests
}
}
if (change.type === FileChangeTypeKind.Deleted) {
await this._graphQLCache.updateFragmentDefinitionCache(
Expand Down

0 comments on commit 5910332

Please sign in to comment.