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 authored and acao committed Nov 20, 2023
1 parent 6674b7b commit 75ccd72
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);
}
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(
Expand Down

0 comments on commit 75ccd72

Please sign in to comment.