Skip to content

Commit ad81eea

Browse files
committed
For now make sure module resolution cache usage doesnt go past program creation
1 parent 8bdf98e commit ad81eea

File tree

6 files changed

+21
-14
lines changed

6 files changed

+21
-14
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,9 +785,9 @@ export function resolvePackageNameToPackageJson(
785785
containingDirectory: string,
786786
options: CompilerOptions,
787787
host: ModuleResolutionHost,
788-
cache: ModuleResolutionCache | undefined,
788+
cache: PackageJsonInfoCache | undefined,
789789
): PackageJsonInfo | undefined {
790-
const moduleResolutionState = getTemporaryModuleResolutionState(cache?.getPackageJsonInfoCache(), host, options);
790+
const moduleResolutionState = getTemporaryModuleResolutionState(cache, host, options);
791791

792792
return forEachAncestorDirectory(containingDirectory, ancestorDirectory => {
793793
if (getBaseFileName(ancestorDirectory) !== "node_modules") {
@@ -2198,7 +2198,7 @@ export function getEntrypointsFromPackageJsonInfo(
21982198
packageJsonInfo: PackageJsonInfo,
21992199
options: CompilerOptions,
22002200
host: GetPackageJsonEntrypointsHost,
2201-
cache: ModuleResolutionCache | undefined,
2201+
cache: PackageJsonInfoCache | undefined,
22022202
resolveJs?: boolean,
22032203
): string[] | false {
22042204
if (!resolveJs && packageJsonInfo.contents.resolvedEntrypoints !== undefined) {
@@ -2210,7 +2210,7 @@ export function getEntrypointsFromPackageJsonInfo(
22102210
let entrypoints: string[] | undefined;
22112211
const extensions = Extensions.TypeScript | Extensions.Declaration | (resolveJs ? Extensions.JavaScript : 0);
22122212
const features = getNodeResolutionFeatures(options);
2213-
const loadPackageJsonMainState = getTemporaryModuleResolutionState(cache?.getPackageJsonInfoCache(), host, options);
2213+
const loadPackageJsonMainState = getTemporaryModuleResolutionState(cache, host, options);
22142214
loadPackageJsonMainState.conditions = getConditions(options);
22152215
loadPackageJsonMainState.requestContainingDirectory = packageJsonInfo.packageDirectory;
22162216
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
@@ -4666,7 +4666,7 @@ export interface Program extends ScriptReferenceHost {
46664666
*/
46674667
getMissingFilePaths(): readonly Path[];
46684668
/** @internal */
4669-
getModuleResolutionCache(): ModuleResolutionCache | undefined;
4669+
getPackageJsonInfoCache(): PackageJsonInfoCache | undefined;
46704670
/** @internal */
46714671
getFilesByNameMap(): Map<string, SourceFile | false | undefined>;
46724672

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[],
@@ -2625,7 +2630,7 @@ export class AutoImportProviderProject extends Project {
26252630
hostProject.currentDirectory,
26262631
compilerOptions,
26272632
host,
2628-
program.getModuleResolutionCache(),
2633+
program.getPackageJsonInfoCache(),
26292634
);
26302635
if (packageJson) {
26312636
const entrypoints = getRootNamesFromPackageJson(packageJson, program, symlinkCache);
@@ -2645,7 +2650,7 @@ export class AutoImportProviderProject extends Project {
26452650
directory,
26462651
compilerOptions,
26472652
host,
2648-
program.getModuleResolutionCache(),
2653+
program.getPackageJsonInfoCache(),
26492654
);
26502655
if (typesPackageJson) {
26512656
const entrypoints = getRootNamesFromPackageJson(typesPackageJson, program, symlinkCache);
@@ -2685,7 +2690,7 @@ export class AutoImportProviderProject extends Project {
26852690
packageJson,
26862691
compilerOptions,
26872692
host,
2688-
program.getModuleResolutionCache(),
2693+
program.getPackageJsonInfoCache(),
26892694
resolveJs,
26902695
);
26912696
if (entrypoints) {
@@ -2836,8 +2841,8 @@ export class AutoImportProviderProject extends Project {
28362841
}
28372842

28382843
/** @internal */
2839-
override getModuleResolutionCache() {
2840-
return this.hostProject.getCurrentProgram()?.getModuleResolutionCache();
2844+
override getPackageJsonInfoCache() {
2845+
return this.hostProject.getPackageJsonInfoCache();
28412846
}
28422847
}
28432848

src/server/session.ts

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