Skip to content

Commit cfed79b

Browse files
authored
Correctly use ownMap from module resolution cache (#43986)
* Test showing the moduleResolutionCache reset issue with tsc --b --w * Fix incorrect usage of ownMap by making it function returning ownMap instead of constant value
1 parent f630365 commit cfed79b

File tree

3 files changed

+443
-6
lines changed

3 files changed

+443
-6
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ namespace ts {
504504

505505
/*@internal*/
506506
export interface CacheWithRedirects<T> {
507-
ownMap: ESMap<string, T>;
507+
getOwnMap: () => ESMap<string, T>;
508508
redirectsMap: ESMap<Path, ESMap<string, T>>;
509509
getOrCreateMapOfCacheRedirects(redirectedReference: ResolvedProjectReference | undefined): ESMap<string, T>;
510510
clear(): void;
@@ -517,14 +517,18 @@ namespace ts {
517517
let ownMap: ESMap<string, T> = new Map();
518518
const redirectsMap = new Map<Path, ESMap<string, T>>();
519519
return {
520-
ownMap,
520+
getOwnMap,
521521
redirectsMap,
522522
getOrCreateMapOfCacheRedirects,
523523
clear,
524524
setOwnOptions,
525525
setOwnMap
526526
};
527527

528+
function getOwnMap() {
529+
return ownMap;
530+
}
531+
528532
function setOwnOptions(newOptions: CompilerOptions) {
529533
options = newOptions;
530534
}
@@ -586,10 +590,10 @@ namespace ts {
586590
if (directoryToModuleNameMap.redirectsMap.size === 0) {
587591
// The own map will be for projectCompilerOptions
588592
Debug.assert(!moduleNameToDirectoryMap || moduleNameToDirectoryMap.redirectsMap.size === 0);
589-
Debug.assert(directoryToModuleNameMap.ownMap.size === 0);
590-
Debug.assert(!moduleNameToDirectoryMap || moduleNameToDirectoryMap.ownMap.size === 0);
591-
directoryToModuleNameMap.redirectsMap.set(options.configFile.path, directoryToModuleNameMap.ownMap);
592-
moduleNameToDirectoryMap?.redirectsMap.set(options.configFile.path, moduleNameToDirectoryMap.ownMap);
593+
Debug.assert(directoryToModuleNameMap.getOwnMap().size === 0);
594+
Debug.assert(!moduleNameToDirectoryMap || moduleNameToDirectoryMap.getOwnMap().size === 0);
595+
directoryToModuleNameMap.redirectsMap.set(options.configFile.path, directoryToModuleNameMap.getOwnMap());
596+
moduleNameToDirectoryMap?.redirectsMap.set(options.configFile.path, moduleNameToDirectoryMap.getOwnMap());
593597
}
594598
else {
595599
// Set correct own map

src/testRunner/unittests/tsbuild/watchMode.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,4 +1020,59 @@ const a: string = "hello";`),
10201020
]
10211021
});
10221022
});
1023+
1024+
describe("unittests:: tsbuild:: watchMode:: module resolution different in referenced project", () => {
1025+
verifyTscWatch({
1026+
scenario: "moduleResolutionCache",
1027+
subScenario: "handles the cache correctly when two projects use different module resolution settings",
1028+
sys: () => createWatchedSystem(
1029+
[
1030+
{ path: `${projectRoot}/project1/index.ts`, content: `import { foo } from "file";` },
1031+
{ path: `${projectRoot}/project1/node_modules/file/index.d.ts`, content: "export const foo = 10;" },
1032+
{
1033+
path: `${projectRoot}/project1/tsconfig.json`,
1034+
content: JSON.stringify({
1035+
compilerOptions: { composite: true, types: ["foo", "bar"] },
1036+
files: ["index.ts"]
1037+
})
1038+
},
1039+
{ path: `${projectRoot}/project2/index.ts`, content: `import { foo } from "file";` },
1040+
{ path: `${projectRoot}/project2/file.d.ts`, content: "export const foo = 10;" },
1041+
{
1042+
path: `${projectRoot}/project2/tsconfig.json`,
1043+
content: JSON.stringify({
1044+
compilerOptions: { composite: true, types: ["foo"], moduleResolution: "classic" },
1045+
files: ["index.ts"]
1046+
})
1047+
},
1048+
{ path: `${projectRoot}/node_modules/@types/foo/index.d.ts`, content: "export const foo = 10;" },
1049+
{ path: `${projectRoot}/node_modules/@types/bar/index.d.ts`, content: "export const bar = 10;" },
1050+
{
1051+
path: `${projectRoot}/tsconfig.json`,
1052+
content: JSON.stringify({
1053+
files: [],
1054+
references: [
1055+
{ path: "./project1" },
1056+
{ path: "./project2" }
1057+
]
1058+
})
1059+
},
1060+
libFile
1061+
],
1062+
{ currentDirectory: projectRoot }
1063+
),
1064+
commandLineArgs: ["--b", "-w", "-v"],
1065+
changes: [
1066+
{
1067+
caption: "Append text",
1068+
change: sys => sys.appendFile(`${projectRoot}/project1/index.ts`, "const bar = 10;"),
1069+
timeouts: sys => {
1070+
sys.checkTimeoutQueueLengthAndRun(1); // build project1
1071+
sys.checkTimeoutQueueLengthAndRun(1); // Solution
1072+
sys.checkTimeoutQueueLength(0);
1073+
}
1074+
},
1075+
]
1076+
});
1077+
});
10231078
}

0 commit comments

Comments
 (0)