Skip to content

Commit 1045f3b

Browse files
authored
detach root files on project close if project language service is disabled (#13077)
1 parent c90af3a commit 1045f3b

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,41 @@ namespace ts.projectSystem {
18401840
assert.isFalse(service.externalProjects[0].languageServiceEnabled, "language service should be disabled - 2");
18411841
});
18421842

1843+
it("files are properly detached when language service is disabled", () => {
1844+
const f1 = {
1845+
path: "/a/app.js",
1846+
content: "var x = 1"
1847+
};
1848+
const f2 = {
1849+
path: "/a/largefile.js",
1850+
content: ""
1851+
};
1852+
const f3 = {
1853+
path: "/a/lib.js",
1854+
content: "var x = 1"
1855+
};
1856+
const config = {
1857+
path: "/a/tsconfig.json",
1858+
content: JSON.stringify({ compilerOptions: { allowJs: true } })
1859+
};
1860+
const host = createServerHost([f1, f2, f3, config]);
1861+
const originalGetFileSize = host.getFileSize;
1862+
host.getFileSize = (filePath: string) =>
1863+
filePath === f2.path ? server.maxProgramSizeForNonTsFiles + 1 : originalGetFileSize.call(host, filePath);
1864+
1865+
const projectService = createProjectService(host);
1866+
projectService.openClientFile(f1.path);
1867+
projectService.checkNumberOfProjects({ configuredProjects: 1 });
1868+
1869+
projectService.closeClientFile(f1.path);
1870+
projectService.checkNumberOfProjects({});
1871+
1872+
for (const f of [f2, f3]) {
1873+
const scriptInfo = projectService.getScriptInfoForNormalizedPath(server.toNormalizedPath(f.path));
1874+
assert.equal(scriptInfo.containingProjects.length, 0, `expect 0 containing projects for '${f.path}'`)
1875+
}
1876+
});
1877+
18431878
it("language service disabled events are triggered", () => {
18441879
const f1 = {
18451880
path: "/a/app.js",

src/server/project.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,9 @@ namespace ts.server {
257257
info.detachFromProject(this);
258258
}
259259
}
260-
else {
261-
// release all root files
260+
if (!this.program || !this.languageServiceEnabled) {
261+
// release all root files either if there is no program or language service is disabled.
262+
// in the latter case set of root files can be larger than the set of files in program.
262263
for (const root of this.rootFiles) {
263264
root.detachFromProject(this);
264265
}

0 commit comments

Comments
 (0)