Skip to content

Commit

Permalink
Fix incorrect usage of package.json types/main in package subdire…
Browse files Browse the repository at this point in the history
…ctories (#60696)
  • Loading branch information
andrewbranch authored Dec 6, 2024
1 parent 239a2a9 commit 421f5c5
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 75 deletions.
26 changes: 11 additions & 15 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2208,9 +2208,7 @@ function tryFileLookup(fileName: string, onlyRecordFailures: boolean, state: Mod

function loadNodeModuleFromDirectory(extensions: Extensions, candidate: string, onlyRecordFailures: boolean, state: ModuleResolutionState, considerPackageJson = true) {
const packageInfo = considerPackageJson ? getPackageJsonInfo(candidate, onlyRecordFailures, state) : undefined;
const packageJsonContent = packageInfo && packageInfo.contents.packageJsonContent;
const versionPaths = packageInfo && getVersionPathsOfPackageJsonInfo(packageInfo, state);
return withPackageId(packageInfo, loadNodeModuleFromDirectoryWorker(extensions, candidate, onlyRecordFailures, state, packageJsonContent, versionPaths), state);
return withPackageId(packageInfo, loadNodeModuleFromDirectoryWorker(extensions, candidate, onlyRecordFailures, state, packageInfo), state);
}

/** @internal */
Expand Down Expand Up @@ -2243,8 +2241,7 @@ export function getEntrypointsFromPackageJsonInfo(
packageJsonInfo.packageDirectory,
/*onlyRecordFailures*/ false,
loadPackageJsonMainState,
packageJsonInfo.contents.packageJsonContent,
getVersionPathsOfPackageJsonInfo(packageJsonInfo, loadPackageJsonMainState),
packageJsonInfo,
);
entrypoints = append(entrypoints, mainResolution?.path);

Expand Down Expand Up @@ -2481,15 +2478,16 @@ function getPackageJsonInfo(packageDirectory: string, onlyRecordFailures: boolea
}
}

function loadNodeModuleFromDirectoryWorker(extensions: Extensions, candidate: string, onlyRecordFailures: boolean, state: ModuleResolutionState, jsonContent: PackageJsonPathFields | undefined, versionPaths: VersionPaths | undefined): PathAndExtension | undefined {
function loadNodeModuleFromDirectoryWorker(extensions: Extensions, candidate: string, onlyRecordFailures: boolean, state: ModuleResolutionState, packageJson: PackageJsonInfo | undefined): PathAndExtension | undefined {
const versionPaths = packageJson && getVersionPathsOfPackageJsonInfo(packageJson, state);
let packageFile: string | undefined;
if (jsonContent) {
if (packageJson && arePathsEqual(packageJson?.packageDirectory, candidate, state.host)) {
if (state.isConfigLookup) {
packageFile = readPackageJsonTSConfigField(jsonContent, candidate, state);
packageFile = readPackageJsonTSConfigField(packageJson.contents.packageJsonContent, packageJson.packageDirectory, state);
}
else {
packageFile = extensions & Extensions.Declaration && readPackageJsonTypesFields(jsonContent, candidate, state) ||
extensions & (Extensions.ImplementationFiles | Extensions.Declaration) && readPackageJsonMainField(jsonContent, candidate, state) ||
packageFile = extensions & Extensions.Declaration && readPackageJsonTypesFields(packageJson.contents.packageJsonContent, packageJson.packageDirectory, state) ||
extensions & (Extensions.ImplementationFiles | Extensions.Declaration) && readPackageJsonMainField(packageJson.contents.packageJsonContent, packageJson.packageDirectory, state) ||
undefined;
}
}
Expand All @@ -2510,7 +2508,7 @@ function loadNodeModuleFromDirectoryWorker(extensions: Extensions, candidate: st
const features = state.features;
const candidateIsFromPackageJsonField = state.candidateIsFromPackageJsonField;
state.candidateIsFromPackageJsonField = true;
if (jsonContent?.type !== "module") {
if (packageJson?.contents.packageJsonContent.type !== "module") {
state.features &= ~NodeResolutionFeatures.EsmMode;
}
const result = nodeLoadModuleByRelativeName(expandedExtensions, candidate, onlyRecordFailures, state, /*considerPackageJson*/ false);
Expand Down Expand Up @@ -3100,8 +3098,7 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu
candidate,
!nodeModulesDirectoryExists,
state,
packageInfo.contents.packageJsonContent,
getVersionPathsOfPackageJsonInfo(packageInfo, state),
packageInfo,
);
return withPackageId(packageInfo, fromDirectory, state);
}
Expand All @@ -3113,8 +3110,7 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu
candidate,
onlyRecordFailures,
state,
packageInfo && packageInfo.contents.packageJsonContent,
packageInfo && getVersionPathsOfPackageJsonInfo(packageInfo, state),
packageInfo,
);
if (
!pathAndExtension && packageInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@
"File '/node_modules/foo/bar.ts' does not exist.",
"File '/node_modules/foo/bar.tsx' does not exist.",
"File '/node_modules/foo/bar.d.ts' does not exist.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'types.d.ts' that references '/node_modules/foo/bar/types.d.ts'.",
"File '/node_modules/foo/bar/types.d.ts' does not exist.",
"Loading module as file / folder, candidate module location '/node_modules/foo/bar/types.d.ts', target file types: TypeScript, Declaration.",
"File name '/node_modules/foo/bar/types.d.ts' has a '.d.ts' extension - stripping it.",
"File '/node_modules/foo/bar/types.ts' does not exist.",
"File '/node_modules/foo/bar/types.tsx' does not exist.",
"File '/node_modules/foo/bar/types.d.ts' does not exist.",
"File '/node_modules/foo/bar/types.d.ts.ts' does not exist.",
"File '/node_modules/foo/bar/types.d.ts.tsx' does not exist.",
"File '/node_modules/foo/bar/types.d.ts.d.ts' does not exist.",
"Directory '/node_modules/foo/bar/types.d.ts' does not exist, skipping all lookups in it.",
"File '/node_modules/foo/bar/index.ts' does not exist.",
"File '/node_modules/foo/bar/index.tsx' does not exist.",
"File '/node_modules/foo/bar/index.d.ts' does not exist.",
Expand All @@ -31,7 +19,6 @@
"File '/node_modules/foo/package.json' exists according to earlier cached lookups.",
"File '/node_modules/foo/bar.js' does not exist.",
"File '/node_modules/foo/bar.jsx' does not exist.",
"'package.json' does not have a 'main' field.",
"File '/node_modules/foo/bar/index.js' exists - use it as a name resolution result.",
"'package.json' does not have a 'peerDependencies' field.",
"Resolution of non-relative name failed; trying with '--moduleResolution bundler' to see if project may need configuration update.",
Expand All @@ -42,18 +29,6 @@
"File '/node_modules/foo/bar.ts' does not exist.",
"File '/node_modules/foo/bar.tsx' does not exist.",
"File '/node_modules/foo/bar.d.ts' does not exist.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'types.d.ts' that references '/node_modules/foo/bar/types.d.ts'.",
"File '/node_modules/foo/bar/types.d.ts' does not exist.",
"Loading module as file / folder, candidate module location '/node_modules/foo/bar/types.d.ts', target file types: TypeScript, Declaration.",
"File name '/node_modules/foo/bar/types.d.ts' has a '.d.ts' extension - stripping it.",
"File '/node_modules/foo/bar/types.ts' does not exist.",
"File '/node_modules/foo/bar/types.tsx' does not exist.",
"File '/node_modules/foo/bar/types.d.ts' does not exist.",
"File '/node_modules/foo/bar/types.d.ts.ts' does not exist.",
"File '/node_modules/foo/bar/types.d.ts.tsx' does not exist.",
"File '/node_modules/foo/bar/types.d.ts.d.ts' does not exist.",
"Directory '/node_modules/foo/bar/types.d.ts' does not exist, skipping all lookups in it.",
"File '/node_modules/foo/bar/index.ts' does not exist.",
"File '/node_modules/foo/bar/index.tsx' does not exist.",
"File '/node_modules/foo/bar/index.d.ts' does not exist.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@
"File '/node_modules/foo/@bar.ts' does not exist.",
"File '/node_modules/foo/@bar.tsx' does not exist.",
"File '/node_modules/foo/@bar.d.ts' does not exist.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'types.d.ts' that references '/node_modules/foo/@bar/types.d.ts'.",
"File '/node_modules/foo/@bar/types.d.ts' does not exist.",
"Loading module as file / folder, candidate module location '/node_modules/foo/@bar/types.d.ts', target file types: TypeScript, Declaration.",
"File name '/node_modules/foo/@bar/types.d.ts' has a '.d.ts' extension - stripping it.",
"File '/node_modules/foo/@bar/types.ts' does not exist.",
"File '/node_modules/foo/@bar/types.tsx' does not exist.",
"File '/node_modules/foo/@bar/types.d.ts' does not exist.",
"File '/node_modules/foo/@bar/types.d.ts.ts' does not exist.",
"File '/node_modules/foo/@bar/types.d.ts.tsx' does not exist.",
"File '/node_modules/foo/@bar/types.d.ts.d.ts' does not exist.",
"Directory '/node_modules/foo/@bar/types.d.ts' does not exist, skipping all lookups in it.",
"File '/node_modules/foo/@bar/index.ts' does not exist.",
"File '/node_modules/foo/@bar/index.tsx' does not exist.",
"File '/node_modules/foo/@bar/index.d.ts' does not exist.",
Expand All @@ -31,7 +19,6 @@
"File '/node_modules/foo/package.json' exists according to earlier cached lookups.",
"File '/node_modules/foo/@bar.js' does not exist.",
"File '/node_modules/foo/@bar.jsx' does not exist.",
"'package.json' does not have a 'main' field.",
"File '/node_modules/foo/@bar/index.js' exists - use it as a name resolution result.",
"'package.json' does not have a 'peerDependencies' field.",
"Resolution of non-relative name failed; trying with '--moduleResolution bundler' to see if project may need configuration update.",
Expand All @@ -42,18 +29,6 @@
"File '/node_modules/foo/@bar.ts' does not exist.",
"File '/node_modules/foo/@bar.tsx' does not exist.",
"File '/node_modules/foo/@bar.d.ts' does not exist.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'types.d.ts' that references '/node_modules/foo/@bar/types.d.ts'.",
"File '/node_modules/foo/@bar/types.d.ts' does not exist.",
"Loading module as file / folder, candidate module location '/node_modules/foo/@bar/types.d.ts', target file types: TypeScript, Declaration.",
"File name '/node_modules/foo/@bar/types.d.ts' has a '.d.ts' extension - stripping it.",
"File '/node_modules/foo/@bar/types.ts' does not exist.",
"File '/node_modules/foo/@bar/types.tsx' does not exist.",
"File '/node_modules/foo/@bar/types.d.ts' does not exist.",
"File '/node_modules/foo/@bar/types.d.ts.ts' does not exist.",
"File '/node_modules/foo/@bar/types.d.ts.tsx' does not exist.",
"File '/node_modules/foo/@bar/types.d.ts.d.ts' does not exist.",
"Directory '/node_modules/foo/@bar/types.d.ts' does not exist, skipping all lookups in it.",
"File '/node_modules/foo/@bar/index.ts' does not exist.",
"File '/node_modules/foo/@bar/index.tsx' does not exist.",
"File '/node_modules/foo/@bar/index.d.ts' does not exist.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
"File '/.src/node_modules/ext/ts3.1/other.ts' does not exist.",
"File '/.src/node_modules/ext/ts3.1/other.tsx' does not exist.",
"File '/.src/node_modules/ext/ts3.1/other.d.ts' does not exist.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'index' that references '/.src/node_modules/ext/ts3.1/other/index'.",
"'package.json' has a 'typesVersions' entry '>=3.1.0-0' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'index'.",
"Module name 'index', matched pattern '*'.",
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/index'.",
Expand All @@ -47,7 +45,6 @@
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/other'.",
"File '/.src/node_modules/ext/ts3.1/other.js' does not exist.",
"File '/.src/node_modules/ext/ts3.1/other.jsx' does not exist.",
"'package.json' does not have a 'main' field.",
"'package.json' has a 'typesVersions' entry '>=3.1.0-0' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'index'.",
"Module name 'index', matched pattern '*'.",
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/index'.",
Expand All @@ -63,8 +60,6 @@
"File '/.src/node_modules/ext/ts3.1/other.ts' does not exist.",
"File '/.src/node_modules/ext/ts3.1/other.tsx' does not exist.",
"File '/.src/node_modules/ext/ts3.1/other.d.ts' does not exist.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'index' that references '/.src/node_modules/ext/ts3.1/other/index'.",
"'package.json' has a 'typesVersions' entry '>=3.1.0-0' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'index'.",
"Module name 'index', matched pattern '*'.",
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/index'.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
"File '/.src/node_modules/ext/ts3.1/other.ts' does not exist.",
"File '/.src/node_modules/ext/ts3.1/other.tsx' does not exist.",
"File '/.src/node_modules/ext/ts3.1/other.d.ts' does not exist.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'index' that references '/.src/node_modules/ext/ts3.1/other/index'.",
"'package.json' has a 'typesVersions' entry '>=3.1.0-0' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'index'.",
"Module name 'index', matched pattern '*'.",
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/index'.",
Expand All @@ -47,7 +45,6 @@
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/other'.",
"File '/.src/node_modules/ext/ts3.1/other.js' does not exist.",
"File '/.src/node_modules/ext/ts3.1/other.jsx' does not exist.",
"'package.json' does not have a 'main' field.",
"'package.json' has a 'typesVersions' entry '>=3.1.0-0' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'index'.",
"Module name 'index', matched pattern '*'.",
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/index'.",
Expand All @@ -63,8 +60,6 @@
"File '/.src/node_modules/ext/ts3.1/other.ts' does not exist.",
"File '/.src/node_modules/ext/ts3.1/other.tsx' does not exist.",
"File '/.src/node_modules/ext/ts3.1/other.d.ts' does not exist.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'index' that references '/.src/node_modules/ext/ts3.1/other/index'.",
"'package.json' has a 'typesVersions' entry '>=3.1.0-0' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'index'.",
"Module name 'index', matched pattern '*'.",
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/index'.",
Expand Down

0 comments on commit 421f5c5

Please sign in to comment.