Skip to content

Commit

Permalink
For now make sure module resolution cache usage doesnt go past progra…
Browse files Browse the repository at this point in the history
…m creation
  • Loading branch information
sheetalkamat committed Oct 20, 2023
1 parent 8bdf98e commit ad81eea
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,9 @@ export function resolvePackageNameToPackageJson(
containingDirectory: string,
options: CompilerOptions,
host: ModuleResolutionHost,
cache: ModuleResolutionCache | undefined,
cache: PackageJsonInfoCache | undefined,
): PackageJsonInfo | undefined {
const moduleResolutionState = getTemporaryModuleResolutionState(cache?.getPackageJsonInfoCache(), host, options);
const moduleResolutionState = getTemporaryModuleResolutionState(cache, host, options);

return forEachAncestorDirectory(containingDirectory, ancestorDirectory => {
if (getBaseFileName(ancestorDirectory) !== "node_modules") {
Expand Down Expand Up @@ -2198,7 +2198,7 @@ export function getEntrypointsFromPackageJsonInfo(
packageJsonInfo: PackageJsonInfo,
options: CompilerOptions,
host: GetPackageJsonEntrypointsHost,
cache: ModuleResolutionCache | undefined,
cache: PackageJsonInfoCache | undefined,
resolveJs?: boolean,
): string[] | false {
if (!resolveJs && packageJsonInfo.contents.resolvedEntrypoints !== undefined) {
Expand All @@ -2210,7 +2210,7 @@ export function getEntrypointsFromPackageJsonInfo(
let entrypoints: string[] | undefined;
const extensions = Extensions.TypeScript | Extensions.Declaration | (resolveJs ? Extensions.JavaScript : 0);
const features = getNodeResolutionFeatures(options);
const loadPackageJsonMainState = getTemporaryModuleResolutionState(cache?.getPackageJsonInfoCache(), host, options);
const loadPackageJsonMainState = getTemporaryModuleResolutionState(cache, host, options);
loadPackageJsonMainState.conditions = getConditions(options);
loadPackageJsonMainState.requestContainingDirectory = packageJsonInfo.packageDirectory;
const mainResolution = loadNodeModuleFromDirectoryWorker(
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1869,14 +1869,16 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
resolvedLibProcessing = undefined;
resolvedModulesProcessing = undefined;
resolvedTypeReferenceDirectiveNamesProcessing = undefined;
const packageJsonCache = moduleResolutionCache?.getPackageJsonInfoCache();
moduleResolutionCache = undefined;

const program: Program = {
getRootFileNames: () => rootNames,
getSourceFile,
getSourceFileByPath,
getSourceFiles: () => files,
getMissingFilePaths: () => missingFilePaths!, // TODO: GH#18217
getModuleResolutionCache: () => moduleResolutionCache,
getPackageJsonInfoCache: () => packageJsonCache,
getFilesByNameMap: () => filesByName,
getCompilerOptions: () => options,
getSyntacticDiagnostics,
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4666,7 +4666,7 @@ export interface Program extends ScriptReferenceHost {
*/
getMissingFilePaths(): readonly Path[];
/** @internal */
getModuleResolutionCache(): ModuleResolutionCache | undefined;
getPackageJsonInfoCache(): PackageJsonInfoCache | undefined;
/** @internal */
getFilesByNameMap(): Map<string, SourceFile | false | undefined>;

Expand Down
15 changes: 10 additions & 5 deletions src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,11 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
return this.resolutionCache.getModuleResolutionCache();
}

/** @internal */
getPackageJsonInfoCache() {
return this.resolutionCache.getModuleResolutionCache().getPackageJsonInfoCache();
}

/** @internal */
resolveTypeReferenceDirectiveReferences<T extends string | FileReference>(
typeDirectiveReferences: readonly T[],
Expand Down Expand Up @@ -2625,7 +2630,7 @@ export class AutoImportProviderProject extends Project {
hostProject.currentDirectory,
compilerOptions,
host,
program.getModuleResolutionCache(),
program.getPackageJsonInfoCache(),
);
if (packageJson) {
const entrypoints = getRootNamesFromPackageJson(packageJson, program, symlinkCache);
Expand All @@ -2645,7 +2650,7 @@ export class AutoImportProviderProject extends Project {
directory,
compilerOptions,
host,
program.getModuleResolutionCache(),
program.getPackageJsonInfoCache(),
);
if (typesPackageJson) {
const entrypoints = getRootNamesFromPackageJson(typesPackageJson, program, symlinkCache);
Expand Down Expand Up @@ -2685,7 +2690,7 @@ export class AutoImportProviderProject extends Project {
packageJson,
compilerOptions,
host,
program.getModuleResolutionCache(),
program.getPackageJsonInfoCache(),
resolveJs,
);
if (entrypoints) {
Expand Down Expand Up @@ -2836,8 +2841,8 @@ export class AutoImportProviderProject extends Project {
}

/** @internal */
override getModuleResolutionCache() {
return this.hostProject.getCurrentProgram()?.getModuleResolutionCache();
override getPackageJsonInfoCache() {
return this.hostProject.getPackageJsonInfoCache();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,7 @@ export class Session<TMessage = string> implements EventSender {
if (nodeModulesPathParts && fileName.lastIndexOf(nodeModulesPathPart) === nodeModulesPathParts.topLevelNodeModulesIndex) {
// Second check ensures the fileName only contains one `/node_modules/`. If there's more than one I give up.
const packageDirectory = fileName.substring(0, nodeModulesPathParts.packageRootIndex);
const packageJsonCache = project.getModuleResolutionCache()?.getPackageJsonInfoCache();
const packageJsonCache = project.getPackageJsonInfoCache();
const compilerOptions = project.getCompilationSettings();
const packageJson = getPackageScopeForPath(getNormalizedAbsolutePath(packageDirectory + "/package.json", project.getCurrentDirectory()), getTemporaryModuleResolutionState(packageJsonCache, project, compilerOptions));
if (!packageJson) return undefined;
Expand All @@ -1605,7 +1605,7 @@ export class Session<TMessage = string> implements EventSender {
packageJson,
{ moduleResolution: ModuleResolutionKind.Node10 },
project,
project.getModuleResolutionCache(),
packageJsonCache,
);
// This substring is correct only because we checked for a single `/node_modules/` at the top.
const packageNamePathPart = fileName.substring(
Expand Down
2 changes: 1 addition & 1 deletion src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2463,7 +2463,7 @@ export function createModuleSpecifierResolutionHost(program: Program, host: Lang
useCaseSensitiveFileNames: maybeBind(host, host.useCaseSensitiveFileNames),
getSymlinkCache: maybeBind(host, host.getSymlinkCache) || program.getSymlinkCache,
getModuleSpecifierCache: maybeBind(host, host.getModuleSpecifierCache),
getPackageJsonInfoCache: () => program.getModuleResolutionCache()?.getPackageJsonInfoCache(),
getPackageJsonInfoCache: () => program.getPackageJsonInfoCache(),
getGlobalTypingsCacheLocation: maybeBind(host, host.getGlobalTypingsCacheLocation),
redirectTargetsMap: program.redirectTargetsMap,
getProjectReferenceRedirect: fileName => program.getProjectReferenceRedirect(fileName),
Expand Down

0 comments on commit ad81eea

Please sign in to comment.