Skip to content

Commit 66590a9

Browse files
committed
Verify ref counts are set correctly when reusing the source file from script info cache
1 parent e4c380a commit 66590a9

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8459,7 +8459,8 @@ new C();`
84598459
const moduleInfo = service.getScriptInfo(moduleFile.path);
84608460
assert.isDefined(moduleInfo);
84618461
assert.equal(moduleInfo.isOrphan(), moduleIsOrphan);
8462-
assert.equal(service.documentRegistry.hasDocument(moduleInfo.path), !moduleIsOrphan);
8462+
const key = service.documentRegistry.getKeyForCompilationSettings(project.getCompilationSettings());
8463+
assert.deepEqual(service.documentRegistry.getLanguageServiceRefCounts(moduleInfo.path), [[key, moduleIsOrphan ? undefined : 1]]);
84638464
}
84648465
});
84658466
});

src/services/documentRegistry.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ namespace ts {
8888
releaseDocumentWithKey(path: Path, key: DocumentRegistryBucketKey): void;
8989

9090
/*@internal*/
91-
hasDocument(path: Path): boolean;
91+
getLanguageServiceRefCounts(path: Path): [string, number | undefined][];
9292

9393
reportStats(): string;
9494
}
@@ -188,9 +188,10 @@ namespace ts {
188188
if (!entry && externalCache) {
189189
const sourceFile = externalCache.getDocument(key, path);
190190
if (sourceFile) {
191+
Debug.assert(acquiring);
191192
entry = {
192193
sourceFile,
193-
languageServiceRefCount: 1
194+
languageServiceRefCount: 0
194195
};
195196
bucket.set(path, entry);
196197
}
@@ -229,6 +230,7 @@ namespace ts {
229230
entry.languageServiceRefCount++;
230231
}
231232
}
233+
Debug.assert(entry.languageServiceRefCount !== 0);
232234

233235
return entry.sourceFile;
234236
}
@@ -252,8 +254,11 @@ namespace ts {
252254
}
253255
}
254256

255-
function hasDocument(path: Path) {
256-
return !!forEachEntry(buckets, bucket => bucket.has(path));
257+
function getLanguageServiceRefCounts(path: Path) {
258+
return arrayFrom(buckets.entries(), ([key, bucket]): [string, number | undefined] => {
259+
const entry = bucket.get(path);
260+
return [key, entry && entry.languageServiceRefCount];
261+
});
257262
}
258263

259264
return {
@@ -263,7 +268,7 @@ namespace ts {
263268
updateDocumentWithKey,
264269
releaseDocument,
265270
releaseDocumentWithKey,
266-
hasDocument,
271+
getLanguageServiceRefCounts,
267272
reportStats,
268273
getKeyForCompilationSettings
269274
};

0 commit comments

Comments
 (0)