From 83afa3fb949cbd603788288b254887fe3f7ea9a8 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Tue, 5 Jul 2016 11:48:26 -0700 Subject: [PATCH] more tests --- src/server/editorServices.ts | 8 ++++- .../cases/unittests/tsserverProjectSystem.ts | 36 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index c91760c3977..292313208b4 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -373,10 +373,16 @@ namespace ts.server { const inferredProject = this.createInferredProjectWithRootFileIfNecessary(info); if (!this.useSingleInferredProject) { // if useOneInferredProject is not set then try to fixup ownership of open files + // check 'defaultProject !== inferredProject' is necessary to handle cases + // when creation inferred project for some file has added other open files into this project (i.e. as referenced files) + // we definitely don't want to delete the project that was just created for (const f of this.openFiles) { + if (f.containingProjects.length === 0) { + // this is orphaned file that we have not processed yet - skip it + continue; + } const defaultProject = f.getDefaultProject(); if (isRootFileInInferredProject(info) && defaultProject !== inferredProject && inferredProject.containsScriptInfo(f)) { - // open file used to be root in inferred project, // this inferred project is different from the one we've just created for current file // and new inferred project references this open file. diff --git a/tests/cases/unittests/tsserverProjectSystem.ts b/tests/cases/unittests/tsserverProjectSystem.ts index 5cf7500a196..f10b37bd41f 100644 --- a/tests/cases/unittests/tsserverProjectSystem.ts +++ b/tests/cases/unittests/tsserverProjectSystem.ts @@ -1039,5 +1039,41 @@ namespace ts { checkNumberOfProjects(projectService, { configuredProjects: 1 }); checkProjectActualFiles(projectService.configuredProjects[0], [ file1.path, file2.path, file3.path ]); }); + + it("correctly migrate files between projects", () => { + const file1 = { + path: "/a/b/f1.ts", + content: ` + export * from "../c/f2.ts"; + export * from "../d/f3.ts";` + }; + const file2 = { + path: "/a/c/f2.ts", + content: "export let x = 1;" + }; + const file3 = { + path: "/a/d/f3.ts", + content: "export let y = 1;" + }; + const host = createServerHost([file1, file2, file3]); + const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useSingleInferredProject*/ false); + + projectService.openClientFile(file2.path); + checkNumberOfProjects(projectService, { inferredProjects: 1 }); + checkProjectActualFiles(projectService.inferredProjects[0], [file2.path]); + + projectService.openClientFile(file3.path); + checkNumberOfProjects(projectService, { inferredProjects: 2 }); + checkProjectActualFiles(projectService.inferredProjects[0], [file2.path]); + checkProjectActualFiles(projectService.inferredProjects[1], [file3.path]); + + projectService.openClientFile(file1.path); + checkNumberOfProjects(projectService, { inferredProjects: 1 }); + checkProjectRootFiles(projectService.inferredProjects[0], [file1.path]); + checkProjectActualFiles(projectService.inferredProjects[0], [file1.path, file2.path, file3.path]); + + projectService.closeClientFile(file1.path); + checkNumberOfProjects(projectService, { inferredProjects: 2 }); + }); }); } \ No newline at end of file