Skip to content

Commit b52e333

Browse files
committed
Pass correct module resolution state when reading package.json info so that they are correctly tracked in the resolution
1 parent bef492e commit b52e333

12 files changed

+171
-74
lines changed

src/compiler/core.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,11 @@ namespace ts {
17241724
/** Does nothing. */
17251725
export function noop(_?: unknown): void { }
17261726

1727+
export const noopPush: Push<any> = {
1728+
push: noop,
1729+
length: 0
1730+
};
1731+
17271732
/** Do nothing and return false */
17281733
export function returnFalse(): false {
17291734
return false;

src/compiler/moduleNameResolver.ts

+28-56
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ namespace ts {
131131
resultFromCache?: ResolvedModuleWithFailedLookupLocations;
132132
packageJsonInfoCache: PackageJsonInfoCache | undefined;
133133
features: NodeResolutionFeatures;
134-
conditions: string[];
134+
conditions: readonly string[];
135135
requestContainingDirectory: string | undefined;
136136
reportResolutionDiagnostic: (d: ResolutionDiagnostic) => void;
137137
}
@@ -506,18 +506,7 @@ namespace ts {
506506
host: ModuleResolutionHost,
507507
cache: ModuleResolutionCache | undefined,
508508
): PackageJsonInfo | undefined {
509-
const moduleResolutionState: ModuleResolutionState = {
510-
compilerOptions: options,
511-
host,
512-
traceEnabled: isTraceEnabled(options, host),
513-
failedLookupLocations: [],
514-
affectingLocations: [],
515-
packageJsonInfoCache: cache?.getPackageJsonInfoCache(),
516-
conditions: emptyArray,
517-
features: NodeResolutionFeatures.None,
518-
requestContainingDirectory: containingDirectory,
519-
reportResolutionDiagnostic: noop
520-
};
509+
const moduleResolutionState = getTemporaryModuleResolutionState(cache?.getPackageJsonInfoCache(), host, options);
521510

522511
return forEachAncestorDirectory(containingDirectory, ancestorDirectory => {
523512
if (getBaseFileName(ancestorDirectory) !== "node_modules") {
@@ -1440,8 +1429,8 @@ namespace ts {
14401429
}
14411430

14421431
function node16ModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions,
1443-
host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference,
1444-
resolutionMode?: ResolutionMode): ResolvedModuleWithFailedLookupLocations {
1432+
host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference,
1433+
resolutionMode?: ResolutionMode): ResolvedModuleWithFailedLookupLocations {
14451434
return nodeNextModuleNameResolverWorker(
14461435
NodeResolutionFeatures.Node16Default,
14471436
moduleName,
@@ -1455,8 +1444,8 @@ namespace ts {
14551444
}
14561445

14571446
function nodeNextModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions,
1458-
host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference,
1459-
resolutionMode?: ResolutionMode): ResolvedModuleWithFailedLookupLocations {
1447+
host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference,
1448+
resolutionMode?: ResolutionMode): ResolvedModuleWithFailedLookupLocations {
14601449
return nodeNextModuleNameResolverWorker(
14611450
NodeResolutionFeatures.NodeNextDefault,
14621451
moduleName,
@@ -1861,18 +1850,9 @@ namespace ts {
18611850
let entrypoints: string[] | undefined;
18621851
const extensions = resolveJs ? Extensions.JavaScript : Extensions.TypeScript;
18631852
const features = getDefaultNodeResolutionFeatures(options);
1864-
const requireState: ModuleResolutionState = {
1865-
compilerOptions: options,
1866-
host,
1867-
traceEnabled: isTraceEnabled(options, host),
1868-
failedLookupLocations: [],
1869-
affectingLocations: [],
1870-
packageJsonInfoCache: cache?.getPackageJsonInfoCache(),
1871-
conditions: ["node", "require", "types"],
1872-
features,
1873-
requestContainingDirectory: packageJsonInfo.packageDirectory,
1874-
reportResolutionDiagnostic: noop
1875-
};
1853+
const requireState = getTemporaryModuleResolutionState(cache?.getPackageJsonInfoCache(), host, options);
1854+
requireState.conditions = ["node", "require", "types"];
1855+
requireState.requestContainingDirectory = packageJsonInfo.packageDirectory;
18761856
const requireResolution = loadNodeModuleFromDirectoryWorker(
18771857
extensions,
18781858
packageJsonInfo.packageDirectory,
@@ -1958,6 +1938,22 @@ namespace ts {
19581938
}
19591939
}
19601940

1941+
/*@internal*/
1942+
export function getTemporaryModuleResolutionState(packageJsonInfoCache: PackageJsonInfoCache | undefined, host: ModuleResolutionHost, options: CompilerOptions): ModuleResolutionState {
1943+
return {
1944+
host,
1945+
compilerOptions: options,
1946+
traceEnabled: isTraceEnabled(options, host),
1947+
failedLookupLocations: noopPush,
1948+
affectingLocations: noopPush,
1949+
packageJsonInfoCache,
1950+
features: NodeResolutionFeatures.None,
1951+
conditions: emptyArray,
1952+
requestContainingDirectory: undefined,
1953+
reportResolutionDiagnostic: noop
1954+
};
1955+
}
1956+
19611957
/*@internal*/
19621958
interface PackageJsonInfo {
19631959
packageDirectory: string;
@@ -1972,31 +1968,7 @@ namespace ts {
19721968
* A function for locating the package.json scope for a given path
19731969
*/
19741970
/*@internal*/
1975-
export function getPackageScopeForPath(fileName: Path, packageJsonInfoCache: PackageJsonInfoCache | undefined, host: ModuleResolutionHost, options: CompilerOptions): PackageJsonInfo | undefined {
1976-
const state: {
1977-
host: ModuleResolutionHost;
1978-
compilerOptions: CompilerOptions;
1979-
traceEnabled: boolean;
1980-
failedLookupLocations: Push<string>;
1981-
affectingLocations: Push<string>;
1982-
resultFromCache?: ResolvedModuleWithFailedLookupLocations;
1983-
packageJsonInfoCache: PackageJsonInfoCache | undefined;
1984-
features: number;
1985-
conditions: never[];
1986-
requestContainingDirectory: string | undefined;
1987-
reportResolutionDiagnostic: (d: ResolutionDiagnostic) => void;
1988-
} = {
1989-
host,
1990-
compilerOptions: options,
1991-
traceEnabled: isTraceEnabled(options, host),
1992-
failedLookupLocations: [],
1993-
affectingLocations: [],
1994-
packageJsonInfoCache,
1995-
features: 0,
1996-
conditions: [],
1997-
requestContainingDirectory: undefined,
1998-
reportResolutionDiagnostic: noop
1999-
};
1971+
export function getPackageScopeForPath(fileName: Path, state: ModuleResolutionState): PackageJsonInfo | undefined {
20001972
const parts = getPathComponents(fileName);
20011973
parts.pop();
20021974
while (parts.length > 0) {
@@ -2175,7 +2147,7 @@ namespace ts {
21752147
function loadModuleFromSelfNameReference(extensions: Extensions, moduleName: string, directory: string, state: ModuleResolutionState, cache: ModuleResolutionCache | undefined, redirectedReference: ResolvedProjectReference | undefined): SearchResult<Resolved> {
21762148
const useCaseSensitiveFileNames = typeof state.host.useCaseSensitiveFileNames === "function" ? state.host.useCaseSensitiveFileNames() : state.host.useCaseSensitiveFileNames;
21772149
const directoryPath = toPath(combinePaths(directory, "dummy"), state.host.getCurrentDirectory?.(), createGetCanonicalFileName(useCaseSensitiveFileNames === undefined ? true : useCaseSensitiveFileNames));
2178-
const scope = getPackageScopeForPath(directoryPath, state.packageJsonInfoCache, state.host, state.compilerOptions);
2150+
const scope = getPackageScopeForPath(directoryPath, state);
21792151
if (!scope || !scope.packageJsonContent.exports) {
21802152
return undefined;
21812153
}
@@ -2237,7 +2209,7 @@ namespace ts {
22372209
}
22382210
const useCaseSensitiveFileNames = typeof state.host.useCaseSensitiveFileNames === "function" ? state.host.useCaseSensitiveFileNames() : state.host.useCaseSensitiveFileNames;
22392211
const directoryPath = toPath(combinePaths(directory, "dummy"), state.host.getCurrentDirectory?.(), createGetCanonicalFileName(useCaseSensitiveFileNames === undefined ? true : useCaseSensitiveFileNames));
2240-
const scope = getPackageScopeForPath(directoryPath, state.packageJsonInfoCache, state.host, state.compilerOptions);
2212+
const scope = getPackageScopeForPath(directoryPath, state);
22412213
if (!scope) {
22422214
if (state.traceEnabled) {
22432215
trace(state.host, Diagnostics.Directory_0_has_no_containing_package_json_scope_Imports_will_not_resolve, directoryPath);

src/compiler/program.ts

+2-13
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ namespace ts {
870870
return undefined;
871871
}
872872
function lookupFromPackageJson(): NonNullable<ResolutionMode> {
873-
const scope = getPackageScopeForPath(fileName, packageJsonInfoCache, host, options);
873+
const scope = getPackageScopeForPath(fileName, getTemporaryModuleResolutionState(packageJsonInfoCache, host, options));
874874
return scope?.packageJsonContent.type === "module" ? ModuleKind.ESNext : ModuleKind.CommonJS;
875875
}
876876
}
@@ -1174,18 +1174,7 @@ namespace ts {
11741174
let oldProgram = typeof oldProgramOrOldBuildInfoProgramConstructor === "object" ? oldProgramOrOldBuildInfoProgramConstructor : undefined;
11751175
let oldBuildInfoProgram: OldBuildInfoProgram | undefined;
11761176
if (!oldProgram && typeof oldProgramOrOldBuildInfoProgramConstructor === "function") {
1177-
const state: ModuleResolutionState = {
1178-
host,
1179-
compilerOptions: options,
1180-
traceEnabled: isTraceEnabled(options, host),
1181-
failedLookupLocations: [],
1182-
affectingLocations: [],
1183-
packageJsonInfoCache: moduleResolutionCache?.getPackageJsonInfoCache(),
1184-
features: 0,
1185-
conditions: [],
1186-
requestContainingDirectory: undefined,
1187-
reportResolutionDiagnostic: noop
1188-
};
1177+
const state = getTemporaryModuleResolutionState(moduleResolutionCache?.getPackageJsonInfoCache(), host, options);
11891178
oldBuildInfoProgram = oldProgramOrOldBuildInfoProgramConstructor({
11901179
fileExists: fileName => host.fileExists(fileName),
11911180
createHash: maybeBind(host, host.createHash),

src/server/session.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ namespace ts.server {
13781378
const packageDirectory = fileName.substring(0, nodeModulesPathParts.packageRootIndex);
13791379
const packageJsonCache = project.getModuleResolutionCache()?.getPackageJsonInfoCache();
13801380
const compilerOptions = project.getCompilationSettings();
1381-
const packageJson = getPackageScopeForPath(project.toPath(packageDirectory + "/package.json"), packageJsonCache, project, compilerOptions);
1381+
const packageJson = getPackageScopeForPath(project.toPath(packageDirectory + "/package.json"), getTemporaryModuleResolutionState(packageJsonCache, project, compilerOptions));
13821382
if (!packageJson) return undefined;
13831383
// Use fake options instead of actual compiler options to avoid following export map if the project uses node16 or nodenext -
13841384
// Mapping from an export map entry across packages is out of scope for now. Returned entrypoints will only be what can be

0 commit comments

Comments
 (0)