Skip to content

Commit 9f93c67

Browse files
authored
Dont calculate version paths proactively as they may not be needed (#51593)
1 parent 9a79aeb commit 9f93c67

File tree

46 files changed

+675
-709
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+675
-709
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,7 +1818,7 @@ function tryFileLookup(fileName: string, onlyRecordFailures: boolean, state: Mod
18181818
function loadNodeModuleFromDirectory(extensions: Extensions, candidate: string, onlyRecordFailures: boolean, state: ModuleResolutionState, considerPackageJson = true) {
18191819
const packageInfo = considerPackageJson ? getPackageJsonInfo(candidate, onlyRecordFailures, state) : undefined;
18201820
const packageJsonContent = packageInfo && packageInfo.contents.packageJsonContent;
1821-
const versionPaths = packageInfo && packageInfo.contents.versionPaths;
1821+
const versionPaths = packageInfo && getVersionPathsOfPackageJsonInfo(packageInfo, state);
18221822
return withPackageId(packageInfo, loadNodeModuleFromDirectoryWorker(extensions, candidate, onlyRecordFailures, state, packageJsonContent, versionPaths));
18231823
}
18241824

@@ -1848,7 +1848,7 @@ export function getEntrypointsFromPackageJsonInfo(
18481848
/*onlyRecordFailures*/ false,
18491849
requireState,
18501850
packageJsonInfo.contents.packageJsonContent,
1851-
packageJsonInfo.contents.versionPaths);
1851+
getVersionPathsOfPackageJsonInfo(packageJsonInfo, requireState));
18521852
entrypoints = append(entrypoints, requireResolution?.path);
18531853

18541854
if (features & NodeResolutionFeatures.Exports && packageJsonInfo.contents.packageJsonContent.exports) {
@@ -1952,7 +1952,8 @@ export interface PackageJsonInfo {
19521952
/** @internal */
19531953
export interface PackageJsonInfoContents {
19541954
packageJsonContent: PackageJsonPathFields;
1955-
versionPaths: VersionPaths | undefined;
1955+
/** false: versionPaths are not present. undefined: not yet resolved */
1956+
versionPaths: VersionPaths | false | undefined;
19561957
/** false: resolved to nothing. undefined: not yet resolved */
19571958
resolvedEntrypoints: string[] | false | undefined;
19581959
}
@@ -1975,6 +1976,13 @@ export interface PackageJsonInfoContents {
19751976
return undefined;
19761977
}
19771978

1979+
function getVersionPathsOfPackageJsonInfo(packageJsonInfo: PackageJsonInfo, state: ModuleResolutionState): VersionPaths | undefined {
1980+
if (packageJsonInfo.contents.versionPaths === undefined) {
1981+
packageJsonInfo.contents.versionPaths = readPackageJsonTypesVersionPaths(packageJsonInfo.contents.packageJsonContent, state) || false;
1982+
}
1983+
return packageJsonInfo.contents.versionPaths || undefined;
1984+
}
1985+
19781986
/** @internal */
19791987
export function getPackageJsonInfo(packageDirectory: string, onlyRecordFailures: boolean, state: ModuleResolutionState): PackageJsonInfo | undefined {
19801988
const { host, traceEnabled } = state;
@@ -2005,8 +2013,7 @@ export function getPackageJsonInfo(packageDirectory: string, onlyRecordFailures:
20052013
if (traceEnabled) {
20062014
trace(host, Diagnostics.Found_package_json_at_0, packageJsonPath);
20072015
}
2008-
const versionPaths = readPackageJsonTypesVersionPaths(packageJsonContent, state);
2009-
const result: PackageJsonInfo = { packageDirectory, contents: { packageJsonContent, versionPaths, resolvedEntrypoints: undefined } };
2016+
const result: PackageJsonInfo = { packageDirectory, contents: { packageJsonContent, versionPaths: undefined, resolvedEntrypoints: undefined } };
20102017
state.packageJsonInfoCache?.setPackageJsonInfo(packageJsonPath, result);
20112018
state.affectingLocations.push(packageJsonPath);
20122019
return result;
@@ -2587,7 +2594,7 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu
25872594
!nodeModulesDirectoryExists,
25882595
state,
25892596
packageInfo.contents.packageJsonContent,
2590-
packageInfo.contents.versionPaths
2597+
getVersionPathsOfPackageJsonInfo(packageInfo, state),
25912598
);
25922599
return withPackageId(packageInfo, fromDirectory);
25932600
}
@@ -2602,7 +2609,7 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu
26022609
onlyRecordFailures,
26032610
state,
26042611
packageInfo && packageInfo.contents.packageJsonContent,
2605-
packageInfo && packageInfo.contents.versionPaths
2612+
packageInfo && getVersionPathsOfPackageJsonInfo(packageInfo, state),
26062613
);
26072614
if (
26082615
!pathAndExtension && packageInfo
@@ -2627,12 +2634,13 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu
26272634
if (packageInfo && packageInfo.contents.packageJsonContent.exports && state.features & NodeResolutionFeatures.Exports) {
26282635
return loadModuleFromExports(packageInfo, extensions, combinePaths(".", rest), state, cache, redirectedReference)?.value;
26292636
}
2630-
if (rest !== "" && packageInfo && packageInfo.contents.versionPaths) {
2637+
const versionPaths = rest !== "" && packageInfo ? getVersionPathsOfPackageJsonInfo(packageInfo, state) : undefined;
2638+
if (versionPaths) {
26312639
if (state.traceEnabled) {
2632-
trace(state.host, Diagnostics.package_json_has_a_typesVersions_entry_0_that_matches_compiler_version_1_looking_for_a_pattern_to_match_module_name_2, packageInfo.contents.versionPaths.version, version, rest);
2640+
trace(state.host, Diagnostics.package_json_has_a_typesVersions_entry_0_that_matches_compiler_version_1_looking_for_a_pattern_to_match_module_name_2, versionPaths.version, version, rest);
26332641
}
26342642
const packageDirectoryExists = nodeModulesDirectoryExists && directoryProbablyExists(packageDirectory, state.host);
2635-
const fromPaths = tryLoadModuleUsingPaths(extensions, rest, packageDirectory, packageInfo.contents.versionPaths.paths, /*pathPatterns*/ undefined, loader, !packageDirectoryExists, state);
2643+
const fromPaths = tryLoadModuleUsingPaths(extensions, rest, packageDirectory, versionPaths.paths, /*pathPatterns*/ undefined, loader, !packageDirectoryExists, state);
26362644
if (fromPaths) {
26372645
return fromPaths.value;
26382646
}

tests/baselines/reference/duplicatePackage_relativeImportWithinPackage.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
"Module resolution kind is not specified, using 'NodeJs'.",
3434
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
3535
"Found 'package.json' at '/node_modules/a/node_modules/foo/package.json'.",
36-
"'package.json' does not have a 'typesVersions' field.",
3736
"File '/node_modules/a/node_modules/foo.ts' does not exist.",
3837
"File '/node_modules/a/node_modules/foo.tsx' does not exist.",
3938
"File '/node_modules/a/node_modules/foo.d.ts' does not exist.",
39+
"'package.json' does not have a 'typesVersions' field.",
4040
"'package.json' does not have a 'typings' field.",
4141
"'package.json' does not have a 'types' field.",
4242
"'package.json' does not have a 'main' field.",

tests/baselines/reference/duplicatePackage_relativeImportWithinPackage_scoped.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
"Module resolution kind is not specified, using 'NodeJs'.",
3434
"Loading module '@foo/bar' from 'node_modules' folder, target file types: TypeScript, Declaration.",
3535
"Found 'package.json' at '/node_modules/a/node_modules/@foo/bar/package.json'.",
36-
"'package.json' does not have a 'typesVersions' field.",
3736
"File '/node_modules/a/node_modules/@foo/bar.ts' does not exist.",
3837
"File '/node_modules/a/node_modules/@foo/bar.tsx' does not exist.",
3938
"File '/node_modules/a/node_modules/@foo/bar.d.ts' does not exist.",
39+
"'package.json' does not have a 'typesVersions' field.",
4040
"'package.json' does not have a 'typings' field.",
4141
"'package.json' does not have a 'types' field.",
4242
"'package.json' does not have a 'main' field.",

tests/baselines/reference/library-reference-11.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"Looking up in 'node_modules' folder, initial location '/a/b'.",
55
"Directory '/a/b/node_modules' does not exist, skipping all lookups in it.",
66
"Found 'package.json' at '/a/node_modules/jquery/package.json'.",
7-
"'package.json' does not have a 'typesVersions' field.",
87
"File '/a/node_modules/jquery.d.ts' does not exist.",
8+
"'package.json' does not have a 'typesVersions' field.",
99
"'package.json' has 'typings' field 'jquery.d.ts' that references '/a/node_modules/jquery/jquery.d.ts'.",
1010
"File '/a/node_modules/jquery/jquery.d.ts' exist - use it as a name resolution result.",
1111
"Resolving real path for '/a/node_modules/jquery/jquery.d.ts', result '/a/node_modules/jquery/jquery.d.ts'.",

tests/baselines/reference/library-reference-12.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"Looking up in 'node_modules' folder, initial location '/a/b'.",
55
"Directory '/a/b/node_modules' does not exist, skipping all lookups in it.",
66
"Found 'package.json' at '/a/node_modules/jquery/package.json'.",
7-
"'package.json' does not have a 'typesVersions' field.",
87
"File '/a/node_modules/jquery.d.ts' does not exist.",
8+
"'package.json' does not have a 'typesVersions' field.",
99
"'package.json' does not have a 'typings' field.",
1010
"'package.json' has 'types' field 'dist/jquery.d.ts' that references '/a/node_modules/jquery/dist/jquery.d.ts'.",
1111
"File '/a/node_modules/jquery/dist/jquery.d.ts' exist - use it as a name resolution result.",

tests/baselines/reference/moduleResolutionWithExtensions_unexpected.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"Module resolution kind is not specified, using 'NodeJs'.",
44
"Loading module 'normalize.css' from 'node_modules' folder, target file types: TypeScript, Declaration.",
55
"Found 'package.json' at '/node_modules/normalize.css/package.json'.",
6-
"'package.json' does not have a 'typesVersions' field.",
76
"File '/node_modules/normalize.css.ts' does not exist.",
87
"File '/node_modules/normalize.css.tsx' does not exist.",
98
"File '/node_modules/normalize.css.d.ts' does not exist.",
9+
"'package.json' does not have a 'typesVersions' field.",
1010
"'package.json' does not have a 'typings' field.",
1111
"'package.json' does not have a 'types' field.",
1212
"'package.json' has 'main' field 'normalize.css' that references '/node_modules/normalize.css/normalize.css'.",

tests/baselines/reference/moduleResolutionWithExtensions_unexpected2.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"Module resolution kind is not specified, using 'NodeJs'.",
44
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
55
"Found 'package.json' at '/node_modules/foo/package.json'.",
6-
"'package.json' does not have a 'typesVersions' field.",
76
"File '/node_modules/foo.ts' does not exist.",
87
"File '/node_modules/foo.tsx' does not exist.",
98
"File '/node_modules/foo.d.ts' does not exist.",
9+
"'package.json' does not have a 'typesVersions' field.",
1010
"'package.json' does not have a 'typings' field.",
1111
"'package.json' has 'types' field 'foo.js' that references '/node_modules/foo/foo.js'.",
1212
"File '/node_modules/foo/foo.js' exist - use it as a name resolution result.",

tests/baselines/reference/moduleResolution_packageJson_notAtPackageRoot.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"Module resolution kind is not specified, using 'NodeJs'.",
44
"Loading module 'foo/bar' from 'node_modules' folder, target file types: TypeScript, Declaration.",
55
"Found 'package.json' at '/node_modules/foo/bar/package.json'.",
6-
"'package.json' does not have a 'typesVersions' field.",
76
"File '/node_modules/foo/bar.ts' does not exist.",
87
"File '/node_modules/foo/bar.tsx' does not exist.",
98
"File '/node_modules/foo/bar.d.ts' does not exist.",
9+
"'package.json' does not have a 'typesVersions' field.",
1010
"'package.json' does not have a 'typings' field.",
1111
"'package.json' has 'types' field 'types.d.ts' that references '/node_modules/foo/bar/types.d.ts'.",
1212
"File '/node_modules/foo/bar/types.d.ts' exist - use it as a name resolution result.",

tests/baselines/reference/moduleResolution_packageJson_notAtPackageRoot_fakeScopedPackage.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"Module resolution kind is not specified, using 'NodeJs'.",
44
"Loading module 'foo/@bar' from 'node_modules' folder, target file types: TypeScript, Declaration.",
55
"Found 'package.json' at '/node_modules/foo/@bar/package.json'.",
6-
"'package.json' does not have a 'typesVersions' field.",
76
"File '/node_modules/foo/@bar.ts' does not exist.",
87
"File '/node_modules/foo/@bar.tsx' does not exist.",
98
"File '/node_modules/foo/@bar.d.ts' does not exist.",
9+
"'package.json' does not have a 'typesVersions' field.",
1010
"'package.json' does not have a 'typings' field.",
1111
"'package.json' has 'types' field 'types.d.ts' that references '/node_modules/foo/@bar/types.d.ts'.",
1212
"File '/node_modules/foo/@bar/types.d.ts' exist - use it as a name resolution result.",

tests/baselines/reference/moduleResolution_packageJson_scopedPackage.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"Module resolution kind is not specified, using 'NodeJs'.",
44
"Loading module '@foo/bar' from 'node_modules' folder, target file types: TypeScript, Declaration.",
55
"Found 'package.json' at '/node_modules/@foo/bar/package.json'.",
6-
"'package.json' does not have a 'typesVersions' field.",
76
"File '/node_modules/@foo/bar.ts' does not exist.",
87
"File '/node_modules/@foo/bar.tsx' does not exist.",
98
"File '/node_modules/@foo/bar.d.ts' does not exist.",
9+
"'package.json' does not have a 'typesVersions' field.",
1010
"'package.json' does not have a 'typings' field.",
1111
"'package.json' has 'types' field 'types.d.ts' that references '/node_modules/@foo/bar/types.d.ts'.",
1212
"File '/node_modules/@foo/bar/types.d.ts' exist - use it as a name resolution result.",

0 commit comments

Comments
 (0)