Skip to content

Commit 1cf5e81

Browse files
committed
Wire through hasInvalidatedResolutions
Fixes #48057
1 parent 8a4b96d commit 1cf5e81

File tree

6 files changed

+19
-27
lines changed

6 files changed

+19
-27
lines changed

src/compiler/resolutionCache.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace ts {
1414
removeResolutionsOfFile(filePath: Path): void;
1515
removeResolutionsFromProjectReferenceRedirects(filePath: Path): void;
1616
setFilesWithInvalidatedNonRelativeUnresolvedImports(filesWithUnresolvedImports: ESMap<Path, readonly string[]>): void;
17-
createHasInvalidatedResolution(forceAllFilesAsInvalidated?: boolean): HasInvalidatedResolution;
17+
createHasInvalidatedResolution(userProvidedHasInvalidatedResolution?: HasInvalidatedResolution): HasInvalidatedResolution;
1818
hasChangedAutomaticTypeDirectiveNames(): boolean;
1919
isFileWithInvalidatedNonRelativeUnresolvedImports(path: Path): boolean;
2020

@@ -300,17 +300,13 @@ namespace ts {
300300
return !!value && !!value.length;
301301
}
302302

303-
function createHasInvalidatedResolution(forceAllFilesAsInvalidated?: boolean): HasInvalidatedResolution {
303+
function createHasInvalidatedResolution(userProvidedHasInvalidatedResolution?: HasInvalidatedResolution): HasInvalidatedResolution {
304304
// Ensure pending resolutions are applied
305305
invalidateResolutionsOfFailedLookupLocations();
306-
if (forceAllFilesAsInvalidated) {
307-
// Any file asked would have invalidated resolution
308-
filesWithInvalidatedResolutions = undefined;
309-
return returnTrue;
310-
}
311306
const collected = filesWithInvalidatedResolutions;
312307
filesWithInvalidatedResolutions = undefined;
313-
return path => (!!collected && collected.has(path)) ||
308+
return path => userProvidedHasInvalidatedResolution?.(path) ||
309+
!!collected?.has(path) ||
314310
isFileWithInvalidatedNonRelativeUnresolvedImports(path);
315311
}
316312

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7214,7 +7214,7 @@ namespace ts {
72147214
getEnvironmentVariable?(name: string): string | undefined;
72157215
/* @internal */ onReleaseOldSourceFile?(oldSourceFile: SourceFile, oldOptions: CompilerOptions, hasSourceFileByPath: boolean): void;
72167216
/* @internal */ onReleaseParsedCommandLine?(configFileName: string, oldResolvedRef: ResolvedProjectReference | undefined, optionOptions: CompilerOptions): void;
7217-
/* @internal */ hasInvalidatedResolution?: HasInvalidatedResolution;
7217+
hasInvalidatedResolution?(filePath: Path): boolean;
72187218
/* @internal */ hasChangedAutomaticTypeDirectiveNames?: HasChangedAutomaticTypeDirectiveNames;
72197219
createHash?(data: string): string;
72207220
getParsedCommandLine?(fileName: string): ParsedCommandLine | undefined;

src/compiler/watchPublic.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ namespace ts {
112112
resolveModuleNames?(moduleNames: string[], containingFile: string, reusedNames: string[] | undefined, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingSourceFile?: SourceFile): (ResolvedModule | undefined)[];
113113
/** If provided, used to resolve type reference directives, otherwise typescript's default resolution */
114114
resolveTypeReferenceDirectives?(typeReferenceDirectiveNames: string[] | readonly FileReference[], containingFile: string, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingFileMode?: SourceFile["impliedNodeFormat"] | undefined): (ResolvedTypeReferenceDirective | undefined)[];
115-
/*@internal*/ hasInvalidatedResolution?: HasInvalidatedResolution;
115+
/** If provided along with custom resolveModuleNames or resolveTypeReferenceDirectives, used to determine if unchanged file path needs to re-resolve modules/type reference directives */
116+
hasInvalidatedResolution?(filePath: Path): boolean;
116117
/**
117118
* Returns the module resolution cache used by a provided `resolveModuleNames` implementation so that any non-name module resolution operations (eg, package.json lookup) can reuse it
118119
*/
@@ -372,6 +373,7 @@ namespace ts {
372373
maybeBind(host, host.getModuleResolutionCache) :
373374
(() => resolutionCache.getModuleResolutionCache());
374375
const userProvidedResolution = !!host.resolveModuleNames || !!host.resolveTypeReferenceDirectives;
376+
const userProvidedHasInvalidatedResolution = maybeBind(host, host.hasInvalidatedResolution) || returnTrue;
375377

376378
builderProgram = readBuilderProgram(compilerOptions, compilerHost) as any as T;
377379
synchronizeProgram();
@@ -445,7 +447,9 @@ namespace ts {
445447
}
446448

447449
// All resolutions are invalid if user provided resolutions
448-
const hasInvalidatedResolution = resolutionCache.createHasInvalidatedResolution(userProvidedResolution);
450+
const hasInvalidatedResolution = resolutionCache.createHasInvalidatedResolution(
451+
userProvidedResolution ? userProvidedHasInvalidatedResolution : undefined
452+
);
449453
const {
450454
originalReadFile, originalFileExists, originalDirectoryExists,
451455
originalCreateDirectory, originalWriteFile,

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3320,6 +3320,7 @@ declare namespace ts {
33203320
*/
33213321
resolveTypeReferenceDirectives?(typeReferenceDirectiveNames: string[] | readonly FileReference[], containingFile: string, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingFileMode?: SourceFile["impliedNodeFormat"] | undefined): (ResolvedTypeReferenceDirective | undefined)[];
33223322
getEnvironmentVariable?(name: string): string | undefined;
3323+
hasInvalidatedResolution?(filePath: Path): boolean;
33233324
createHash?(data: string): string;
33243325
getParsedCommandLine?(fileName: string): ParsedCommandLine | undefined;
33253326
}
@@ -5442,6 +5443,8 @@ declare namespace ts {
54425443
resolveModuleNames?(moduleNames: string[], containingFile: string, reusedNames: string[] | undefined, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingSourceFile?: SourceFile): (ResolvedModule | undefined)[];
54435444
/** If provided, used to resolve type reference directives, otherwise typescript's default resolution */
54445445
resolveTypeReferenceDirectives?(typeReferenceDirectiveNames: string[] | readonly FileReference[], containingFile: string, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingFileMode?: SourceFile["impliedNodeFormat"] | undefined): (ResolvedTypeReferenceDirective | undefined)[];
5446+
/** If provided along with custom resolveModuleNames or resolveTypeReferenceDirectives, used to determine if unchanged file path needs to re-resolve modules/type reference directives */
5447+
hasInvalidatedResolution?(filePath: Path): boolean;
54455448
/**
54465449
* Returns the module resolution cache used by a provided `resolveModuleNames` implementation so that any non-name module resolution operations (eg, package.json lookup) can reuse it
54475450
*/

tests/baselines/reference/api/typescript.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3320,6 +3320,7 @@ declare namespace ts {
33203320
*/
33213321
resolveTypeReferenceDirectives?(typeReferenceDirectiveNames: string[] | readonly FileReference[], containingFile: string, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingFileMode?: SourceFile["impliedNodeFormat"] | undefined): (ResolvedTypeReferenceDirective | undefined)[];
33223322
getEnvironmentVariable?(name: string): string | undefined;
3323+
hasInvalidatedResolution?(filePath: Path): boolean;
33233324
createHash?(data: string): string;
33243325
getParsedCommandLine?(fileName: string): ParsedCommandLine | undefined;
33253326
}
@@ -5442,6 +5443,8 @@ declare namespace ts {
54425443
resolveModuleNames?(moduleNames: string[], containingFile: string, reusedNames: string[] | undefined, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingSourceFile?: SourceFile): (ResolvedModule | undefined)[];
54435444
/** If provided, used to resolve type reference directives, otherwise typescript's default resolution */
54445445
resolveTypeReferenceDirectives?(typeReferenceDirectiveNames: string[] | readonly FileReference[], containingFile: string, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingFileMode?: SourceFile["impliedNodeFormat"] | undefined): (ResolvedTypeReferenceDirective | undefined)[];
5446+
/** If provided along with custom resolveModuleNames or resolveTypeReferenceDirectives, used to determine if unchanged file path needs to re-resolve modules/type reference directives */
5447+
hasInvalidatedResolution?(filePath: Path): boolean;
54455448
/**
54465449
* Returns the module resolution cache used by a provided `resolveModuleNames` implementation so that any non-name module resolution operations (eg, package.json lookup) can reuse it
54475450
*/

tests/baselines/reference/tscWatch/watchApi/host-implements-hasInvalidatedResolution.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,13 @@ Synchronizing program
106106
CreatingProgramWith::
107107
roots: ["/user/username/projects/myproject/main.ts"]
108108
options: {"traceResolution":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
109-
======== Resolving module './other' from '/user/username/projects/myproject/main.ts'. ========
110-
Module resolution kind is not specified, using 'NodeJs'.
111-
Loading module as file / folder, candidate module location '/user/username/projects/myproject/other', target file type 'TypeScript'.
112-
File '/user/username/projects/myproject/other.ts' does not exist.
113-
File '/user/username/projects/myproject/other.tsx' does not exist.
114-
File '/user/username/projects/myproject/other.d.ts' exist - use it as a name resolution result.
115-
======== Module name './other' was successfully resolved to '/user/username/projects/myproject/other.d.ts'. ========
116109
[12:00:30 AM] Found 0 errors. Watching for file changes.
117110

118111

119112

120113
Program root files: ["/user/username/projects/myproject/main.ts"]
121114
Program options: {"traceResolution":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
122-
Program structureReused: SafeModules
115+
Program structureReused: Completely
123116
Program files::
124117
/a/lib/lib.d.ts
125118
/user/username/projects/myproject/other.d.ts
@@ -165,20 +158,13 @@ Synchronizing program
165158
CreatingProgramWith::
166159
roots: ["/user/username/projects/myproject/main.ts"]
167160
options: {"traceResolution":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
168-
======== Resolving module './other' from '/user/username/projects/myproject/main.ts'. ========
169-
Module resolution kind is not specified, using 'NodeJs'.
170-
Loading module as file / folder, candidate module location '/user/username/projects/myproject/other', target file type 'TypeScript'.
171-
File '/user/username/projects/myproject/other.ts' does not exist.
172-
File '/user/username/projects/myproject/other.tsx' does not exist.
173-
File '/user/username/projects/myproject/other.d.ts' exist - use it as a name resolution result.
174-
======== Module name './other' was successfully resolved to '/user/username/projects/myproject/other.d.ts'. ========
175161
[12:00:37 AM] Found 0 errors. Watching for file changes.
176162

177163

178164

179165
Program root files: ["/user/username/projects/myproject/main.ts"]
180166
Program options: {"traceResolution":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
181-
Program structureReused: SafeModules
167+
Program structureReused: Completely
182168
Program files::
183169
/a/lib/lib.d.ts
184170
/user/username/projects/myproject/other.d.ts

0 commit comments

Comments
 (0)