From 55c5e8cb471fd4edc51b638b9f001f609266c95d Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 2 May 2018 12:28:53 -0700 Subject: [PATCH] Fix the assert for orphan script info source change event --- .../unittests/tsserverProjectSystem.ts | 49 ++++++++++++++++++- src/server/editorServices.ts | 9 ++-- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index bfa999a3790d5..29e629cb3c13c 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -3181,7 +3181,7 @@ namespace ts.projectSystem { const projectLocation = "/user/username/projects/project"; const file1: FileOrFolder = { path: `${projectLocation}/src/file1.ts`, - content: `import { y } from "./file1"; let x = 10;` + content: `import { y } from "./file2"; let x = 10;` }; const file2: FileOrFolder = { path: `${projectLocation}/src/file2.ts`, @@ -3217,6 +3217,53 @@ namespace ts.projectSystem { }); }); + + it("Orphan source files are handled correctly on watch trigger", () => { + const projectLocation = "/user/username/projects/project"; + const file1: FileOrFolder = { + path: `${projectLocation}/src/file1.ts`, + content: `export let x = 10;` + }; + const file2: FileOrFolder = { + path: `${projectLocation}/src/file2.ts`, + content: "export let y = 10;" + }; + const configContent1 = JSON.stringify({ + files: ["src/file1.ts", "src/file2.ts"] + }); + const config: FileOrFolder = { + path: `${projectLocation}/tsconfig.json`, + content: configContent1 + }; + const files = [file1, file2, libFile, config]; + const host = createServerHost(files); + const service = createProjectService(host); + service.openClientFile(file1.path); + checkProjectActualFiles(service.configuredProjects.get(config.path), [file1.path, file2.path, libFile.path, config.path]); + + const configContent2 = JSON.stringify({ + files: ["src/file1.ts"] + }); + config.content = configContent2; + host.reloadFS(files); + host.runQueuedTimeoutCallbacks(); + + checkProjectActualFiles(service.configuredProjects.get(config.path), [file1.path, libFile.path, config.path]); + verifyFile2InfoIsOrphan(); + + file2.content += "export let z = 10;"; + host.reloadFS(files); + host.runQueuedTimeoutCallbacks(); + + checkProjectActualFiles(service.configuredProjects.get(config.path), [file1.path, libFile.path, config.path]); + verifyFile2InfoIsOrphan(); + + function verifyFile2InfoIsOrphan() { + const info = service.getScriptInfoForPath(file2.path as Path); + assert.isDefined(info); + assert.equal(info.containingProjects.length, 0); + } + }); }); describe("tsserverProjectSystem Proper errors", () => { diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index ef82e6de30e03..0e420ae6da348 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -594,10 +594,12 @@ namespace ts.server { } private delayUpdateProjectGraphs(projects: ReadonlyArray) { - for (const project of projects) { - this.delayUpdateProjectGraph(project); + if (projects.length) { + for (const project of projects) { + this.delayUpdateProjectGraph(project); + } + this.delayEnsureProjectForOpenFiles(); } - this.delayEnsureProjectForOpenFiles(); } setCompilerOptionsForInferredProjects(projectCompilerOptions: protocol.ExternalProjectCompilerOptions, projectRootPath?: string): void { @@ -708,7 +710,6 @@ namespace ts.server { this.handleDeletedFile(info); } else if (!info.isScriptOpen()) { - Debug.assert(info.containingProjects.length !== 0); // file has been changed which might affect the set of referenced files in projects that include // this file and set of inferred projects info.delayReloadNonMixedContentFile();