Skip to content

Commit 98cb0ce

Browse files
committed
Move the cleanup of script infos to next file open
This helps in reusing script infos even if the project is closed but next open recreates the same project
1 parent 1bf1209 commit 98cb0ce

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,10 +2204,10 @@ namespace ts.projectSystem {
22042204
projectService.closeClientFile(f1.path);
22052205
projectService.checkNumberOfProjects({});
22062206

2207-
for (const f of [f2, f3]) {
2207+
for (const f of [f1, f2, f3]) {
22082208
// There shouldnt be any script info as we closed the file that resulted in creation of it
22092209
const scriptInfo = projectService.getScriptInfoForNormalizedPath(server.toNormalizedPath(f.path));
2210-
assert.equal(scriptInfo, undefined, `expected script info to be closed: '${f.path}'`);
2210+
assert.equal(scriptInfo.containingProjects.length, 0, `expect 0 containing projects for '${f.path}'`);
22112211
}
22122212
});
22132213

src/server/editorServices.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -563,10 +563,17 @@ namespace ts.server {
563563
}
564564
else {
565565
if (info && (!info.isScriptOpen())) {
566-
// file has been changed which might affect the set of referenced files in projects that include
567-
// this file and set of inferred projects
568-
info.reloadFromFile();
569-
this.updateProjectGraphs(info.containingProjects);
566+
if (info.containingProjects.length === 0) {
567+
// Orphan script info, remove it as we can always reload it on next open
568+
info.stopWatcher();
569+
this.filenameToScriptInfo.remove(info.path);
570+
}
571+
else {
572+
// file has been changed which might affect the set of referenced files in projects that include
573+
// this file and set of inferred projects
574+
info.reloadFromFile();
575+
this.updateProjectGraphs(info.containingProjects);
576+
}
570577
}
571578
}
572579
}
@@ -828,8 +835,17 @@ namespace ts.server {
828835
}
829836
}
830837

831-
// Cleanup script infos that are not open and not part of any project
832-
this.deleteOrphanScriptInfoNotInAnyProject();
838+
// Cleanup script infos that arent part of any project is postponed to
839+
// next file open so that if file from same project is opened we wont end up creating same script infos
840+
}
841+
842+
// If the current info is being just closed - add the watcher file to track changes
843+
// But if file was deleted, handle that part
844+
if (this.host.fileExists(info.fileName)) {
845+
this.watchClosedScriptInfo(info);
846+
}
847+
else {
848+
this.handleDeletedFile(info);
833849
}
834850
}
835851

@@ -1310,6 +1326,14 @@ namespace ts.server {
13101326
return this.getScriptInfoForNormalizedPath(toNormalizedPath(uncheckedFileName));
13111327
}
13121328

1329+
watchClosedScriptInfo(info: ScriptInfo) {
1330+
// do not watch files with mixed content - server doesn't know how to interpret it
1331+
if (!info.hasMixedContent) {
1332+
const { fileName } = info;
1333+
info.setWatcher(this.host.watchFile(fileName, _ => this.onSourceFileChanged(fileName)));
1334+
}
1335+
}
1336+
13131337
getOrCreateScriptInfoForNormalizedPath(fileName: NormalizedPath, openedByClient: boolean, fileContent?: string, scriptKind?: ScriptKind, hasMixedContent?: boolean) {
13141338
let info = this.getScriptInfoForNormalizedPath(fileName);
13151339
if (!info) {
@@ -1325,15 +1349,13 @@ namespace ts.server {
13251349
}
13261350
}
13271351
else {
1328-
// do not watch files with mixed content - server doesn't know how to interpret it
1329-
if (!hasMixedContent) {
1330-
info.setWatcher(this.host.watchFile(fileName, _ => this.onSourceFileChanged(fileName)));
1331-
}
1352+
this.watchClosedScriptInfo(info);
13321353
}
13331354
}
13341355
}
13351356
if (info) {
13361357
if (openedByClient && !info.isScriptOpen()) {
1358+
info.stopWatcher();
13371359
info.open(fileContent);
13381360
if (hasMixedContent) {
13391361
info.registerFileUpdate();
@@ -1429,7 +1451,6 @@ namespace ts.server {
14291451
p.updateGraph();
14301452
}
14311453

1432-
this.deleteOrphanScriptInfoNotInAnyProject();
14331454
this.printProjects();
14341455
}
14351456

@@ -1463,6 +1484,7 @@ namespace ts.server {
14631484
// at this point if file is the part of some configured/external project then this project should be created
14641485
const info = this.getOrCreateScriptInfoForNormalizedPath(fileName, /*openedByClient*/ true, fileContent, scriptKind, hasMixedContent);
14651486
this.assignScriptInfoToInferredProjectIfNecessary(info, /*addToListOfOpenFiles*/ true);
1487+
this.deleteOrphanScriptInfoNotInAnyProject();
14661488
this.printProjects();
14671489
return { configFileName, configFileErrors };
14681490
}

0 commit comments

Comments
 (0)