Skip to content

Commit a0d164f

Browse files
authored
Merge pull request microsoft#31820 from microsoft/nodeModules
When resolving from typings cache, handle node code modules
2 parents 520f7e8 + 2fd80b3 commit a0d164f

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
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;
@@ -270,7 +271,12 @@ namespace ts {
270271
if (globalCache !== undefined && !isExternalModuleNameRelative(moduleName) && !(primaryResult.resolvedModule && extensionIsTS(primaryResult.resolvedModule.extension))) {
271272
// create different collection of failed lookup locations for second pass
272273
// if it will fail and we've already found something during the first pass - we don't want to pollute its results
273-
const { resolvedModule, failedLookupLocations } = loadModuleFromGlobalCache(moduleName, resolutionHost.projectName, compilerOptions, host, globalCache);
274+
const { resolvedModule, failedLookupLocations } = loadModuleFromGlobalCache(
275+
Debug.assertDefined(resolutionHost.globalCacheResolutionModuleName)(moduleName),
276+
resolutionHost.projectName,
277+
compilerOptions,
278+
host,
279+
globalCache);
274280
if (resolvedModule) {
275281
return { resolvedModule, failedLookupLocations: addRange(primaryResult.failedLookupLocations as string[], failedLookupLocations) };
276282
}

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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,70 @@ namespace ts.projectSystem {
17811781
import * as c from "foo/a/c";
17821782
`, ["foo"], [fooAA, fooAB, fooAC]);
17831783
});
1784+
1785+
it("should handle node core modules", () => {
1786+
const file: TestFSWithWatch.File = {
1787+
path: "/a/b/app.js",
1788+
content: `// @ts-check
1789+
1790+
const net = require("net");
1791+
const stream = require("stream");`
1792+
};
1793+
const nodeTyping: TestFSWithWatch.File = {
1794+
path: `${globalTypingsCacheLocation}/node_modules/node/index.d.ts`,
1795+
content: `
1796+
declare module "net" {
1797+
export type n = number;
1798+
}
1799+
declare module "stream" {
1800+
export type s = string;
1801+
}`,
1802+
};
1803+
1804+
const host = createServerHost([file, libFile]);
1805+
const installer = new (class extends Installer {
1806+
constructor() {
1807+
super(host, { globalTypingsCacheLocation, typesRegistry: createTypesRegistry("node") });
1808+
}
1809+
installWorker(_requestId: number, _args: string[], _cwd: string, cb: TI.RequestCompletedAction) {
1810+
executeCommand(this, host, ["node"], [nodeTyping], cb);
1811+
}
1812+
})();
1813+
const projectService = createProjectService(host, { typingsInstaller: installer });
1814+
projectService.openClientFile(file.path);
1815+
projectService.checkNumberOfProjects({ inferredProjects: 1 });
1816+
1817+
const proj = projectService.inferredProjects[0];
1818+
checkProjectActualFiles(proj, [file.path, libFile.path]);
1819+
installer.installAll(/*expectedCount*/ 1);
1820+
host.checkTimeoutQueueLengthAndRun(2);
1821+
checkProjectActualFiles(proj, [file.path, libFile.path, nodeTyping.path]);
1822+
projectService.applyChangesInOpenFiles(
1823+
/*openFiles*/ undefined,
1824+
arrayIterator([{
1825+
fileName: file.path,
1826+
changes: arrayIterator([{
1827+
span: {
1828+
start: file.content.indexOf(`"stream"`) + 2,
1829+
length: 0
1830+
},
1831+
newText: " "
1832+
}])
1833+
}]),
1834+
/*closedFiles*/ undefined
1835+
);
1836+
// Below timeout Updates the typings to empty array because of "s tream" as unsresolved import
1837+
// and schedules the update graph because of this.
1838+
host.checkTimeoutQueueLengthAndRun(2);
1839+
checkProjectActualFiles(proj, [file.path, libFile.path, nodeTyping.path]);
1840+
1841+
// Here, since typings doesnt contain node typings and resolution fails and
1842+
// node typings go out of project,
1843+
// but because we handle core node modules when resolving from typings cache
1844+
// node typings are included in the project
1845+
host.checkTimeoutQueueLengthAndRun(2);
1846+
checkProjectActualFiles(proj, [file.path, libFile.path, nodeTyping.path]);
1847+
});
17841848
});
17851849

17861850
describe("unittests:: tsserver:: typingsInstaller:: tsserver:: with inferred Project", () => {

0 commit comments

Comments
 (0)