Skip to content

Correctly use ownMap from module resolution cache #43986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ namespace ts {

/*@internal*/
export interface CacheWithRedirects<T> {
ownMap: ESMap<string, T>;
getOwnMap: () => ESMap<string, T>;
redirectsMap: ESMap<Path, ESMap<string, T>>;
getOrCreateMapOfCacheRedirects(redirectedReference: ResolvedProjectReference | undefined): ESMap<string, T>;
clear(): void;
Expand All @@ -510,14 +510,18 @@ namespace ts {
let ownMap: ESMap<string, T> = new Map();
const redirectsMap = new Map<Path, ESMap<string, T>>();
return {
ownMap,
getOwnMap,
redirectsMap,
getOrCreateMapOfCacheRedirects,
clear,
setOwnOptions,
setOwnMap
};

function getOwnMap() {
return ownMap;
}

function setOwnOptions(newOptions: CompilerOptions) {
options = newOptions;
}
Expand Down Expand Up @@ -579,10 +583,10 @@ namespace ts {
if (directoryToModuleNameMap.redirectsMap.size === 0) {
// The own map will be for projectCompilerOptions
Debug.assert(!moduleNameToDirectoryMap || moduleNameToDirectoryMap.redirectsMap.size === 0);
Debug.assert(directoryToModuleNameMap.ownMap.size === 0);
Debug.assert(!moduleNameToDirectoryMap || moduleNameToDirectoryMap.ownMap.size === 0);
directoryToModuleNameMap.redirectsMap.set(options.configFile.path, directoryToModuleNameMap.ownMap);
moduleNameToDirectoryMap?.redirectsMap.set(options.configFile.path, moduleNameToDirectoryMap.ownMap);
Debug.assert(directoryToModuleNameMap.getOwnMap().size === 0);
Debug.assert(!moduleNameToDirectoryMap || moduleNameToDirectoryMap.getOwnMap().size === 0);
directoryToModuleNameMap.redirectsMap.set(options.configFile.path, directoryToModuleNameMap.getOwnMap());
moduleNameToDirectoryMap?.redirectsMap.set(options.configFile.path, moduleNameToDirectoryMap.getOwnMap());
}
else {
// Set correct own map
Expand Down
55 changes: 55 additions & 0 deletions src/testRunner/unittests/tsbuild/watchMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,4 +1020,59 @@ const a: string = "hello";`),
]
});
});

describe("unittests:: tsbuild:: watchMode:: module resolution different in referenced project", () => {
verifyTscWatch({
scenario: "moduleResolutionCache",
subScenario: "handles the cache correctly when two projects use different module resolution settings",
sys: () => createWatchedSystem(
[
{ path: `${projectRoot}/project1/index.ts`, content: `import { foo } from "file";` },
{ path: `${projectRoot}/project1/node_modules/file/index.d.ts`, content: "export const foo = 10;" },
{
path: `${projectRoot}/project1/tsconfig.json`,
content: JSON.stringify({
compilerOptions: { composite: true, types: ["foo", "bar"] },
files: ["index.ts"]
})
},
{ path: `${projectRoot}/project2/index.ts`, content: `import { foo } from "file";` },
{ path: `${projectRoot}/project2/file.d.ts`, content: "export const foo = 10;" },
{
path: `${projectRoot}/project2/tsconfig.json`,
content: JSON.stringify({
compilerOptions: { composite: true, types: ["foo"], moduleResolution: "classic" },
files: ["index.ts"]
})
},
{ path: `${projectRoot}/node_modules/@types/foo/index.d.ts`, content: "export const foo = 10;" },
{ path: `${projectRoot}/node_modules/@types/bar/index.d.ts`, content: "export const bar = 10;" },
{
path: `${projectRoot}/tsconfig.json`,
content: JSON.stringify({
files: [],
references: [
{ path: "./project1" },
{ path: "./project2" }
]
})
},
libFile
],
{ currentDirectory: projectRoot }
),
commandLineArgs: ["--b", "-w", "-v"],
changes: [
{
caption: "Append text",
change: sys => sys.appendFile(`${projectRoot}/project1/index.ts`, "const bar = 10;"),
timeouts: sys => {
sys.checkTimeoutQueueLengthAndRun(1); // build project1
sys.checkTimeoutQueueLengthAndRun(1); // Solution
sys.checkTimeoutQueueLength(0);
}
},
]
});
});
}
Loading