Skip to content

Commit 6b9f7c0

Browse files
committed
For now make sure module resolution cache usage doesnt go past program creation
1 parent ad1f196 commit 6b9f7c0

6 files changed

Lines changed: 21 additions & 14 deletions

File tree

src/compiler/moduleNameResolver.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -780,9 +780,9 @@ export function resolvePackageNameToPackageJson(
780780
containingDirectory: string,
781781
options: CompilerOptions,
782782
host: ModuleResolutionHost,
783-
cache: ModuleResolutionCache | undefined,
783+
cache: PackageJsonInfoCache | undefined,
784784
): PackageJsonInfo | undefined {
785-
const moduleResolutionState = getTemporaryModuleResolutionState(cache?.getPackageJsonInfoCache(), host, options);
785+
const moduleResolutionState = getTemporaryModuleResolutionState(cache, host, options);
786786

787787
return forEachAncestorDirectory(containingDirectory, ancestorDirectory => {
788788
if (getBaseFileName(ancestorDirectory) !== "node_modules") {
@@ -2193,7 +2193,7 @@ export function getEntrypointsFromPackageJsonInfo(
21932193
packageJsonInfo: PackageJsonInfo,
21942194
options: CompilerOptions,
21952195
host: GetPackageJsonEntrypointsHost,
2196-
cache: ModuleResolutionCache | undefined,
2196+
cache: PackageJsonInfoCache | undefined,
21972197
resolveJs?: boolean,
21982198
): string[] | false {
21992199
if (!resolveJs && packageJsonInfo.contents.resolvedEntrypoints !== undefined) {
@@ -2205,7 +2205,7 @@ export function getEntrypointsFromPackageJsonInfo(
22052205
let entrypoints: string[] | undefined;
22062206
const extensions = Extensions.TypeScript | Extensions.Declaration | (resolveJs ? Extensions.JavaScript : 0);
22072207
const features = getNodeResolutionFeatures(options);
2208-
const loadPackageJsonMainState = getTemporaryModuleResolutionState(cache?.getPackageJsonInfoCache(), host, options);
2208+
const loadPackageJsonMainState = getTemporaryModuleResolutionState(cache, host, options);
22092209
loadPackageJsonMainState.conditions = getConditions(options);
22102210
loadPackageJsonMainState.requestContainingDirectory = packageJsonInfo.packageDirectory;
22112211
const mainResolution = loadNodeModuleFromDirectoryWorker(

src/compiler/program.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1869,14 +1869,16 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
18691869
resolvedLibProcessing = undefined;
18701870
resolvedModulesProcessing = undefined;
18711871
resolvedTypeReferenceDirectiveNamesProcessing = undefined;
1872+
const packageJsonCache = moduleResolutionCache?.getPackageJsonInfoCache();
1873+
moduleResolutionCache = undefined;
18721874

18731875
const program: Program = {
18741876
getRootFileNames: () => rootNames,
18751877
getSourceFile,
18761878
getSourceFileByPath,
18771879
getSourceFiles: () => files,
18781880
getMissingFilePaths: () => missingFilePaths!, // TODO: GH#18217
1879-
getModuleResolutionCache: () => moduleResolutionCache,
1881+
getPackageJsonInfoCache: () => packageJsonCache,
18801882
getFilesByNameMap: () => filesByName,
18811883
getCompilerOptions: () => options,
18821884
getSyntacticDiagnostics,

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4686,7 +4686,7 @@ export interface Program extends ScriptReferenceHost {
46864686
*/
46874687
getMissingFilePaths(): readonly Path[];
46884688
/** @internal */
4689-
getModuleResolutionCache(): ModuleResolutionCache | undefined;
4689+
getPackageJsonInfoCache(): PackageJsonInfoCache | undefined;
46904690
/** @internal */
46914691
getFilesByNameMap(): Map<string, SourceFile | false | undefined>;
46924692

src/server/project.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,11 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
832832
return this.resolutionCache.getModuleResolutionCache();
833833
}
834834

835+
/** @internal */
836+
getPackageJsonInfoCache() {
837+
return this.resolutionCache.getModuleResolutionCache().getPackageJsonInfoCache();
838+
}
839+
835840
/** @internal */
836841
resolveTypeReferenceDirectiveReferences<T extends string | FileReference>(
837842
typeDirectiveReferences: readonly T[],
@@ -2631,7 +2636,7 @@ export class AutoImportProviderProject extends Project {
26312636
hostProject.currentDirectory,
26322637
compilerOptions,
26332638
host,
2634-
program.getModuleResolutionCache(),
2639+
program.getPackageJsonInfoCache(),
26352640
);
26362641
if (packageJson) {
26372642
const entrypoints = getRootNamesFromPackageJson(packageJson, program, symlinkCache);
@@ -2651,7 +2656,7 @@ export class AutoImportProviderProject extends Project {
26512656
directory,
26522657
compilerOptions,
26532658
host,
2654-
program.getModuleResolutionCache(),
2659+
program.getPackageJsonInfoCache(),
26552660
);
26562661
if (typesPackageJson) {
26572662
const entrypoints = getRootNamesFromPackageJson(typesPackageJson, program, symlinkCache);
@@ -2691,7 +2696,7 @@ export class AutoImportProviderProject extends Project {
26912696
packageJson,
26922697
compilerOptions,
26932698
host,
2694-
program.getModuleResolutionCache(),
2699+
program.getPackageJsonInfoCache(),
26952700
resolveJs,
26962701
);
26972702
if (entrypoints) {
@@ -2842,8 +2847,8 @@ export class AutoImportProviderProject extends Project {
28422847
}
28432848

28442849
/** @internal */
2845-
override getModuleResolutionCache() {
2846-
return this.hostProject.getCurrentProgram()?.getModuleResolutionCache();
2850+
override getPackageJsonInfoCache() {
2851+
return this.hostProject.getPackageJsonInfoCache();
28472852
}
28482853
}
28492854

src/server/session.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ export class Session<TMessage = string> implements EventSender {
15951595
if (nodeModulesPathParts && fileName.lastIndexOf(nodeModulesPathPart) === nodeModulesPathParts.topLevelNodeModulesIndex) {
15961596
// Second check ensures the fileName only contains one `/node_modules/`. If there's more than one I give up.
15971597
const packageDirectory = fileName.substring(0, nodeModulesPathParts.packageRootIndex);
1598-
const packageJsonCache = project.getModuleResolutionCache()?.getPackageJsonInfoCache();
1598+
const packageJsonCache = project.getPackageJsonInfoCache();
15991599
const compilerOptions = project.getCompilationSettings();
16001600
const packageJson = getPackageScopeForPath(getNormalizedAbsolutePath(packageDirectory + "/package.json", project.getCurrentDirectory()), getTemporaryModuleResolutionState(packageJsonCache, project, compilerOptions));
16011601
if (!packageJson) return undefined;
@@ -1606,7 +1606,7 @@ export class Session<TMessage = string> implements EventSender {
16061606
packageJson,
16071607
{ moduleResolution: ModuleResolutionKind.Node10 },
16081608
project,
1609-
project.getModuleResolutionCache(),
1609+
packageJsonCache,
16101610
);
16111611
// This substring is correct only because we checked for a single `/node_modules/` at the top.
16121612
const packageNamePathPart = fileName.substring(

src/services/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2463,7 +2463,7 @@ export function createModuleSpecifierResolutionHost(program: Program, host: Lang
24632463
useCaseSensitiveFileNames: maybeBind(host, host.useCaseSensitiveFileNames),
24642464
getSymlinkCache: maybeBind(host, host.getSymlinkCache) || program.getSymlinkCache,
24652465
getModuleSpecifierCache: maybeBind(host, host.getModuleSpecifierCache),
2466-
getPackageJsonInfoCache: () => program.getModuleResolutionCache()?.getPackageJsonInfoCache(),
2466+
getPackageJsonInfoCache: () => program.getPackageJsonInfoCache(),
24672467
getGlobalTypingsCacheLocation: maybeBind(host, host.getGlobalTypingsCacheLocation),
24682468
redirectTargetsMap: program.redirectTargetsMap,
24692469
getProjectReferenceRedirect: fileName => program.getProjectReferenceRedirect(fileName),

0 commit comments

Comments
 (0)