Skip to content

Commit

Permalink
Make the module resolution cache apis for updating compiler options o…
Browse files Browse the repository at this point in the history
…r clearing it
  • Loading branch information
sheetalkamat committed Apr 15, 2021
1 parent 06f25c0 commit 340867b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
32 changes: 31 additions & 1 deletion src/compiler/moduleNameResolver.ts
Expand Up @@ -444,6 +444,12 @@ namespace ts {
export interface ModuleResolutionCache extends NonRelativeModuleNameResolutionCache {
getOrCreateCacheForDirectory(directoryName: string, redirectedReference?: ResolvedProjectReference): Map<ResolvedModuleWithFailedLookupLocations>;
/*@internal*/ directoryToModuleNameMap: CacheWithRedirects<ESMap<string, ResolvedModuleWithFailedLookupLocations>>;
clear(): void;
/**
* Updates with the current compilerOptions the cache will operate with.
* This updates the redirects map as well if needed so module resolutions are cached if they can across the projects
*/
update(options: CompilerOptions): void;
}

/**
Expand Down Expand Up @@ -528,7 +534,31 @@ namespace ts {
currentDirectory: string,
getCanonicalFileName: GetCanonicalFileName): ModuleResolutionCache {

return { getOrCreateCacheForDirectory, getOrCreateCacheForModuleName, directoryToModuleNameMap, moduleNameToDirectoryMap };
return {
getOrCreateCacheForDirectory,
getOrCreateCacheForModuleName,
directoryToModuleNameMap,
moduleNameToDirectoryMap,
clear,
update,
};

function clear() {
directoryToModuleNameMap.clear();
moduleNameToDirectoryMap.clear();
}

function update(options: CompilerOptions) {
if (!options.configFile) return;
const ref: ResolvedProjectReference = {
sourceFile: options.configFile,
commandLine: { options } as ParsedCommandLine
};
directoryToModuleNameMap.setOwnMap(directoryToModuleNameMap.getOrCreateMapOfCacheRedirects(ref));
moduleNameToDirectoryMap.setOwnMap(moduleNameToDirectoryMap.getOrCreateMapOfCacheRedirects(ref));
directoryToModuleNameMap.setOwnOptions(options);
moduleNameToDirectoryMap.setOwnOptions(options);
}

function getOrCreateCacheForDirectory(directoryName: string, redirectedReference?: ResolvedProjectReference) {
const path = toPath(directoryName, currentDirectory, getCanonicalFileName);
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/resolutionCache.ts
Expand Up @@ -284,8 +284,7 @@ namespace ts {
}

function clearPerDirectoryResolutions() {
perDirectoryResolvedModuleNames.clear();
nonRelativeModuleNameCache.clear();
moduleResolutionCache.clear();
perDirectoryResolvedTypeReferenceDirectives.clear();
nonRelativeExternalModuleResolutions.forEach(watchFailedLookupLocationOfNonRelativeModuleResolutions);
nonRelativeExternalModuleResolutions.clear();
Expand Down
17 changes: 4 additions & 13 deletions src/compiler/tsbuildPublic.ts
Expand Up @@ -558,10 +558,7 @@ namespace ts {
compilerHost.getSourceFile = cache.originalGetSourceFile;
state.readFileWithCache = cache.originalReadFileWithCache;
extendedConfigCache.clear();
if (moduleResolutionCache) {
moduleResolutionCache.directoryToModuleNameMap.clear();
moduleResolutionCache.moduleNameToDirectoryMap.clear();
}
moduleResolutionCache?.clear();
state.cache = undefined;
}

Expand Down Expand Up @@ -1321,20 +1318,14 @@ namespace ts {
Debug.assert(moduleResolutionCache.moduleNameToDirectoryMap.redirectsMap.size === 0);
moduleResolutionCache.directoryToModuleNameMap.redirectsMap.set(projPath, moduleResolutionCache.directoryToModuleNameMap.ownMap);
moduleResolutionCache.moduleNameToDirectoryMap.redirectsMap.set(projPath, moduleResolutionCache.moduleNameToDirectoryMap.ownMap);
moduleResolutionCache.directoryToModuleNameMap.setOwnOptions(config.options);
moduleResolutionCache.moduleNameToDirectoryMap.setOwnOptions(config.options);
}
else {
// Set correct own map
Debug.assert(moduleResolutionCache.moduleNameToDirectoryMap.redirectsMap.size > 0);

const ref: ResolvedProjectReference = {
sourceFile: config.options.configFile!,
commandLine: config
};
moduleResolutionCache.directoryToModuleNameMap.setOwnMap(moduleResolutionCache.directoryToModuleNameMap.getOrCreateMapOfCacheRedirects(ref));
moduleResolutionCache.moduleNameToDirectoryMap.setOwnMap(moduleResolutionCache.moduleNameToDirectoryMap.getOrCreateMapOfCacheRedirects(ref));
moduleResolutionCache.update(config.options);
}
moduleResolutionCache.directoryToModuleNameMap.setOwnOptions(config.options);
moduleResolutionCache.moduleNameToDirectoryMap.setOwnOptions(config.options);
}

function checkConfigFileUpToDateStatus(state: SolutionBuilderState, configFile: string, oldestOutputFileTime: Date, oldestOutputFileName: string): Status.OutOfDateWithSelf | undefined {
Expand Down
6 changes: 6 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Expand Up @@ -4729,6 +4729,12 @@ declare namespace ts {
*/
interface ModuleResolutionCache extends NonRelativeModuleNameResolutionCache {
getOrCreateCacheForDirectory(directoryName: string, redirectedReference?: ResolvedProjectReference): Map<ResolvedModuleWithFailedLookupLocations>;
clear(): void;
/**
* Updates with the current compilerOptions the cache will operate with.
* This updates the redirects map as well if needed so module resolutions are cached if they can across the projects
*/
update(options: CompilerOptions): void;
}
/**
* Stored map from non-relative module name to a table: directory -> result of module lookup in this directory
Expand Down
6 changes: 6 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Expand Up @@ -4729,6 +4729,12 @@ declare namespace ts {
*/
interface ModuleResolutionCache extends NonRelativeModuleNameResolutionCache {
getOrCreateCacheForDirectory(directoryName: string, redirectedReference?: ResolvedProjectReference): Map<ResolvedModuleWithFailedLookupLocations>;
clear(): void;
/**
* Updates with the current compilerOptions the cache will operate with.
* This updates the redirects map as well if needed so module resolutions are cached if they can across the projects
*/
update(options: CompilerOptions): void;
}
/**
* Stored map from non-relative module name to a table: directory -> result of module lookup in this directory
Expand Down

0 comments on commit 340867b

Please sign in to comment.