Skip to content

Commit 890b6f2

Browse files
committed
Create old buildInfo program from program so host can be passed around correctly
Thist also fixes module resolution state creation so that some of the package.json are not missed
1 parent b71c45a commit 890b6f2

16 files changed

+218
-141
lines changed

src/compiler/builder.ts

+15-29
Original file line numberDiff line numberDiff line change
@@ -1895,17 +1895,18 @@ namespace ts {
18951895
}
18961896
}
18971897

1898-
function createGetProgramOrOldBuildInfoProgramUndefined(state: ReusableBuilderProgramState): () => Program | OldBuildInfoProgram | undefined {
1899-
let oldProgram: OldBuildInfoProgram | undefined;
1900-
return () => state.program ?? oldProgram ?? (oldProgram = memoize(() => createOldBuildInfoProgram(
1898+
function createGetProgramOrOldBuildInfoProgramUndefined(state: ReusableBuilderProgramState): () => Program | OldBuildInfoProgramConstructor | undefined {
1899+
return () => state.program ?? (host => createOldBuildInfoProgram(
1900+
host,
19011901
state.compilerOptions,
19021902
state.cacheResolutions,
19031903
// Prefer cached resolutions over serialized format
19041904
!state.cacheResolutions ? state.resuableCacheResolutions : undefined,
1905-
))());
1905+
));
19061906
}
19071907

19081908
export function createOldBuildInfoProgram(
1909+
host: OldBuildInfoProgramHost,
19091910
compilerOptions: CompilerOptions,
19101911
cacheResolutions: ReusableBuilderProgramState["cacheResolutions"],
19111912
resuableCacheResolutions: ReusableBuilderProgramState["resuableCacheResolutions"],
@@ -1929,8 +1930,7 @@ namespace ts {
19291930

19301931
return {
19311932
getCompilerOptions: () => compilerOptions,
1932-
getResolvedModule: (host, dirPath, name, mode, redirectedReference) => getResolvedFromCache(
1933-
host,
1933+
getResolvedModule: (dirPath, name, mode, redirectedReference) => getResolvedFromCache(
19341934
cacheResolutions?.modules,
19351935
resuableCacheResolutions?.cache.modules,
19361936
decodedResolvedModules,
@@ -1941,8 +1941,7 @@ namespace ts {
19411941
cacheResolutions?.moduleNameToDirectoryMap,
19421942
decodedModuleNameToDirectoryMap,
19431943
),
1944-
getResolvedTypeReferenceDirective: (host, dirPath, name, mode, redirectedReference) => getResolvedFromCache(
1945-
host,
1944+
getResolvedTypeReferenceDirective: (dirPath, name, mode, redirectedReference) => getResolvedFromCache(
19461945
cacheResolutions?.typeRefs,
19471946
resuableCacheResolutions?.cache.typeRefs,
19481947
decodedResolvedTypeRefs,
@@ -1953,24 +1952,15 @@ namespace ts {
19531952
/*moduleNameToDirectoryMap*/ undefined,
19541953
/*decodedModuleNameToDirectoryMap*/ undefined,
19551954
),
1956-
clearRedirectsMap: () => {
1957-
cacheResolutions?.modules?.clearRedirectsMap();
1958-
cacheResolutions?.typeRefs?.clearRedirectsMap();
1959-
cacheResolutions?.moduleNameToDirectoryMap.clearRedirectsMap();
1960-
decodedResolvedModules.clearRedirectsMap();
1961-
decodedResolvedTypeRefs.clearRedirectsMap();
1962-
decodedModuleNameToDirectoryMap.clearRedirectsMap();
1963-
}
19641955
};
19651956

1966-
function fileExists(host: OldBuildInfoProgramResolutionHost, fileName: string) {
1957+
function fileExists(fileName: string) {
19671958
let result = fileExistsMap.get(fileName);
19681959
if (result === undefined) fileExistsMap.set(fileName, result = host.fileExists(fileName));
19691960
return result;
19701961
}
19711962

19721963
function affectingLocationsSame(
1973-
host: OldBuildInfoProgramResolutionHost,
19741964
fileName: string,
19751965
expected: PackageJsonInfoCacheEntry | string | undefined
19761966
): boolean {
@@ -1990,7 +1980,6 @@ namespace ts {
19901980
}
19911981

19921982
function getResolvedFromCache<T extends ResolvedModuleWithFailedLookupLocations | ResolvedTypeReferenceDirectiveWithFailedLookupLocations>(
1993-
host: OldBuildInfoProgramResolutionHost,
19941983
cache: CacheWithRedirects<Path, ModeAwareCache<T>> | undefined,
19951984
reusableCache: ProgramBuildInfoResolutionCacheWithRedirects | undefined,
19961985
decodedReusableCache: DecodedResolvedMap,
@@ -2006,14 +1995,11 @@ namespace ts {
20061995
moduleNameToDirectoryMap?.getMapOfCacheRedirects(redirectedReference)?.get(getModeAwareCacheKey(name, mode))?.get(dirPath);
20071996
if (fromCache) {
20081997
return fileExists(
2009-
host,
20101998
(fromCache as ResolvedModuleWithFailedLookupLocations).resolvedModule?.resolvedFileName ||
20111999
(fromCache as ResolvedTypeReferenceDirectiveWithFailedLookupLocations).resolvedTypeReferenceDirective!.resolvedFileName!
20122000
) && every(
20132001
fromCache.affectingLocations,
2014-
fileName => affectingLocationsSame(
2015-
host, fileName, cacheResolutions!.packageJsonCache?.getPackageJsonInfo(fileName)
2016-
)
2002+
fileName => affectingLocationsSame(fileName, cacheResolutions!.packageJsonCache?.getPackageJsonInfo(fileName))
20172003
) ? fromCache as Resolution : undefined;
20182004
}
20192005
if (!reusableCache) return undefined;
@@ -2043,7 +2029,7 @@ namespace ts {
20432029

20442030
const resolutionId = decodedReusableCache.getMapOfCacheRedirects(redirectedReference)?.get(dirPath)?.get(name, mode) ||
20452031
decodedModuleNameToDirectoryMap?.getMapOfCacheRedirects(redirectedReference)?.get(getModeAwareCacheKey(name, mode))?.get(dirPath);
2046-
return resolutionId ? toResolution(host, resolutionId) : undefined;
2032+
return resolutionId ? toResolution(resolutionId) : undefined;
20472033
}
20482034

20492035
function setBuildInfoResolutionEntries(
@@ -2116,16 +2102,16 @@ namespace ts {
21162102
));
21172103
}
21182104

2119-
function toAffectingFileLocation(host: OldBuildInfoProgramResolutionHost, fileId: ProgramBuildInfoAbsoluteFileId) {
2105+
function toAffectingFileLocation(fileId: ProgramBuildInfoAbsoluteFileId) {
21202106
if (!decodedHashes && resuableCacheResolutions!.cache.hash) {
21212107
decodedHashes = arrayToMap(resuableCacheResolutions!.cache.hash, hash => isArray(hash) ? hash[0] : hash, hash => isArray(hash) ? hash[1] : undefined);
21222108
}
21232109
const hash = decodedHashes?.get(fileId);
21242110
const file = resuableCacheResolutions!.getProgramBuildInfoFilePathDecoder().toFileAbsolutePath(fileId);
2125-
return affectingLocationsSame(host, file, hash) ? file : undefined;
2111+
return affectingLocationsSame(file, hash) ? file : undefined;
21262112
}
21272113

2128-
function toResolution(host: OldBuildInfoProgramResolutionHost, resolutionId: ProgramBuildInfoResolutionId): Resolution | undefined {
2114+
function toResolution(resolutionId: ProgramBuildInfoResolutionId): Resolution | undefined {
21292115
const existing = resolutions?.[resolutionId - 1];
21302116
if (existing !== undefined) return existing || undefined;
21312117
resolutions ??= new Array(resuableCacheResolutions!.cache.resolutions.length);
@@ -2134,8 +2120,8 @@ namespace ts {
21342120
resolution.resolvedModule?.resolvedFileName || resolution.resolvedTypeReferenceDirective!.resolvedFileName
21352121
);
21362122
let affectingLocations: string[] | undefined;
2137-
if (fileExists(host, resolvedFileName) && every(resolution.affectingLocations, fileId => {
2138-
const file = toAffectingFileLocation(host, fileId);
2123+
if (fileExists(resolvedFileName) && every(resolution.affectingLocations, fileId => {
2124+
const file = toAffectingFileLocation(fileId);
21392125
if (file) (affectingLocations ??= []).push(file);
21402126
return !!file;
21412127
})) {

src/compiler/builderPublic.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace ts {
5151
* Returns current program that could be undefined if the program was released, or cached build info program (currently module and type ref cache)
5252
*/
5353
/*@internal*/
54-
getProgramOrOldBuildInfoProgramUndefined(): Program | OldBuildInfoProgram | undefined;
54+
getProgramOrOldBuildInfoProgramUndefined(): Program | OldBuildInfoProgramConstructor | undefined;
5555
/**
5656
* Releases reference to the program, making all the other operations that need program to fail.
5757
*/

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);

0 commit comments

Comments
 (0)