Skip to content

Commit 2fd80b3

Browse files
committed
When resolving from typings cache, handle node code modules
Fixes microsoft#29865
1 parent 6ed8aae commit 2fd80b3

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

src/compiler/resolutionCache.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace ts {
5151
getCachedDirectoryStructureHost(): CachedDirectoryStructureHost | undefined;
5252
projectName?: string;
5353
getGlobalCache?(): string | undefined;
54+
globalCacheResolutionModuleName?(externalModuleName: string): string;
5455
writeLog(s: string): void;
5556
maxNumberOfFilesToIterateForInvalidation?: number;
5657
getCurrentProgram(): Program | undefined;
@@ -235,7 +236,12 @@ namespace ts {
235236
if (globalCache !== undefined && !isExternalModuleNameRelative(moduleName) && !(primaryResult.resolvedModule && extensionIsTS(primaryResult.resolvedModule.extension))) {
236237
// create different collection of failed lookup locations for second pass
237238
// if it will fail and we've already found something during the first pass - we don't want to pollute its results
238-
const { resolvedModule, failedLookupLocations } = loadModuleFromGlobalCache(moduleName, resolutionHost.projectName, compilerOptions, host, globalCache);
239+
const { resolvedModule, failedLookupLocations } = loadModuleFromGlobalCache(
240+
Debug.assertDefined(resolutionHost.globalCacheResolutionModuleName)(moduleName),
241+
resolutionHost.projectName,
242+
compilerOptions,
243+
host,
244+
globalCache);
239245
if (resolvedModule) {
240246
return { resolvedModule, failedLookupLocations: addRange(primaryResult.failedLookupLocations as string[], failedLookupLocations) };
241247
}

src/jsTyping/jsTyping.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ namespace ts.JsTyping {
7070

7171
export const nodeCoreModules = arrayToSet(nodeCoreModuleList);
7272

73+
export function nonRelativeModuleNameForTypingCache(moduleName: string) {
74+
return nodeCoreModules.has(moduleName) ? "node" : moduleName;
75+
}
76+
7377
/**
7478
* A map of loose file names to library names that we are confident require typings
7579
*/
@@ -150,7 +154,7 @@ namespace ts.JsTyping {
150154
// add typings for unresolved imports
151155
if (unresolvedImports) {
152156
const module = deduplicate<string>(
153-
unresolvedImports.map(moduleId => nodeCoreModules.has(moduleId) ? "node" : moduleId),
157+
unresolvedImports.map(nonRelativeModuleNameForTypingCache),
154158
equateStringsCaseSensitive,
155159
compareStringsCaseSensitive);
156160
addInferredTypings(module, "Inferred typings from unresolved imports");

src/server/project.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,9 @@ namespace ts.server {
457457
return this.getTypeAcquisition().enable ? this.projectService.typingsInstaller.globalTypingsCacheLocation : undefined;
458458
}
459459

460+
/*@internal*/
461+
globalCacheResolutionModuleName = JsTyping.nonRelativeModuleNameForTypingCache;
462+
460463
/*@internal*/
461464
fileIsOpen(filePath: Path) {
462465
return this.projectService.openFiles.has(filePath);

src/testRunner/unittests/tsserver/typingsInstaller.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,9 +1831,11 @@ declare module "stream" {
18311831
checkProjectActualFiles(proj, [file.path, libFile.path, nodeTyping.path]);
18321832

18331833
// Here, since typings doesnt contain node typings and resolution fails and
1834-
// node typings go out of project
1834+
// node typings go out of project,
1835+
// but because we handle core node modules when resolving from typings cache
1836+
// node typings are included in the project
18351837
host.checkTimeoutQueueLengthAndRun(2);
1836-
checkProjectActualFiles(proj, [file.path, libFile.path]);
1838+
checkProjectActualFiles(proj, [file.path, libFile.path, nodeTyping.path]);
18371839
});
18381840
});
18391841

0 commit comments

Comments
 (0)