From 34eaad41553956b1f2bc9bec680dd8ad050f96b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 27 Jun 2022 19:43:43 +0200 Subject: [PATCH] fix(lsp): restart TS language service when caching dependencies (#14979) --- cli/lsp/language_server.rs | 13 ++++++++++--- cli/lsp/tsc.rs | 7 +++++++ cli/tsc/99_main_compiler.js | 10 ++++++++++ cli/tsc/compiler.d.ts | 4 ++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index fdda1aeecd..30a2ce2533 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -2814,9 +2814,16 @@ impl Inner { self.client.show_message(MessageType::WARNING, err).await; } - // now that we have dependencies loaded, we need to re-analyze them and - // invalidate some diagnostics - self.diagnostics_server.invalidate(&[referrer]); + // Now that we have dependencies loaded, we need to re-analyze all the files. + // For that we're invalidating all the existing diagnostics and restarting + // the language server for TypeScript (as it might hold to some stale + // documents). + self.diagnostics_server.invalidate_all(); + let _: bool = self + .ts_server + .request(self.snapshot(), tsc::RequestMethod::Restart) + .await + .unwrap(); self.send_diagnostics_update(); self.send_testing_update(); diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index 2a5bdc2d80..76e428b2f2 100644 --- a/cli/lsp/tsc.rs +++ b/cli/lsp/tsc.rs @@ -2871,6 +2871,9 @@ pub enum RequestMethod { ProvideCallHierarchyIncomingCalls((ModuleSpecifier, u32)), /// Resolve outgoing call hierarchy items for a specific position. ProvideCallHierarchyOutgoingCalls((ModuleSpecifier, u32)), + + // Special request, used only internally by the LSP + Restart, } impl RequestMethod { @@ -3084,6 +3087,10 @@ impl RequestMethod { "position": position }) } + RequestMethod::Restart => json!({ + "id": id, + "method": "restart", + }), } } } diff --git a/cli/tsc/99_main_compiler.js b/cli/tsc/99_main_compiler.js index 7650ff95bd..c2b50ba166 100644 --- a/cli/tsc/99_main_compiler.js +++ b/cli/tsc/99_main_compiler.js @@ -589,11 +589,16 @@ delete Object.prototype.__proto__; */ function serverRequest({ id, ...request }) { debug(`serverRequest()`, { id, ...request }); + // reset all memoized source files names scriptFileNamesCache = undefined; // evict all memoized source file versions scriptVersionCache.clear(); switch (request.method) { + case "restart": { + serverRestart(); + return respond(id, true); + } case "configure": { const { options, errors } = ts .convertCompilerOptionsFromJson(request.compilerOptions, ""); @@ -918,6 +923,11 @@ delete Object.prototype.__proto__; debug("serverInit()"); } + function serverRestart() { + languageService = ts.createLanguageService(host); + debug("serverRestart()"); + } + let hasStarted = false; /** Startup the runtime environment, setting various flags. diff --git a/cli/tsc/compiler.d.ts b/cli/tsc/compiler.d.ts index 2ea7eb2987..1ba1161707 100644 --- a/cli/tsc/compiler.d.ts +++ b/cli/tsc/compiler.d.ts @@ -251,4 +251,8 @@ declare global { specifier: string; position: number; } + + interface Restart { + method: "restart"; + } }