@@ -204,6 +204,7 @@ export function verifyResolutionCache(
204204 actualProgram : ts . Program ,
205205 resolutionHostCacheHost : ts . ResolutionCacheHost ,
206206 projectName : string ,
207+ userResolvedModuleNames ?: true ,
207208) {
208209 const currentDirectory = resolutionHostCacheHost . getCurrentDirectory ! ( ) ;
209210 const expected = ts . createResolutionCache ( resolutionHostCacheHost , actual . rootDirForResolution ) ;
@@ -214,6 +215,12 @@ export function verifyResolutionCache(
214215 const expectedToResolution = new Map < ExpectedResolution , ts . ResolutionWithFailedLookupLocations > ( ) ;
215216 const resolutionToExpected = new Map < ts . ResolutionWithFailedLookupLocations , ExpectedResolution > ( ) ;
216217 const resolutionToRefs = new Map < ts . ResolutionWithFailedLookupLocations , ResolutionInfo [ ] > ( ) ;
218+ const inferredTypesPath = resolutionHostCacheHost . toPath (
219+ ts . getAutomaticTypeDirectiveContainingFile (
220+ actualProgram . getCompilerOptions ( ) ,
221+ currentDirectory ,
222+ ) ,
223+ ) ;
217224 actual . resolvedModuleNames . forEach ( ( resolutions , path ) =>
218225 collectResolutionToRefFromCache (
219226 "Modules" ,
@@ -222,6 +229,7 @@ export function verifyResolutionCache(
222229 getResolvedModuleFileName ,
223230 /*deferWatchingNonRelativeResolution*/ true ,
224231 expected . resolvedModuleNames ,
232+ ( name , mode ) => actualProgram . getResolvedModule ( actualProgram . getSourceFileByPath ( path ) ! , name , mode ) ,
225233 )
226234 ) ;
227235 actual . resolvedTypeReferenceDirectives . forEach ( ( resolutions , path ) =>
@@ -232,6 +240,10 @@ export function verifyResolutionCache(
232240 getResolvedTypeRefFileName ,
233241 /*deferWatchingNonRelativeResolution*/ false ,
234242 expected . resolvedTypeReferenceDirectives ,
243+ ( name , mode ) =>
244+ path !== inferredTypesPath ?
245+ actualProgram . getResolvedTypeReferenceDirective ( actualProgram . getSourceFileByPath ( path ) ! , name , mode ) :
246+ actualProgram . getAutomaticTypeDirectiveResolutions ( ) . get ( name , mode ) ,
235247 )
236248 ) ;
237249 actual . resolvedLibraries . forEach ( ( resolved , libFileName ) => {
@@ -248,6 +260,39 @@ export function verifyResolutionCache(
248260 ) ;
249261 expected . resolvedLibraries . set ( libFileName , expectedResolution ) ;
250262 } ) ;
263+ // Check for resolutions in program but not in cache to empty resolutions
264+ if ( ! userResolvedModuleNames ) {
265+ actualProgram . forEachResolvedModule ( ( resolution , name , mode , filePath ) =>
266+ verifyResolutionIsInCache (
267+ "Modules" ,
268+ actual . resolvedModuleNames . get ( filePath ) ,
269+ resolution ,
270+ name ,
271+ mode ,
272+ filePath ,
273+ )
274+ ) ;
275+ }
276+ actualProgram . forEachResolvedTypeReferenceDirective ( ( resolution , name , mode , filePath ) =>
277+ verifyResolutionIsInCache (
278+ "TypeRefs" ,
279+ actual . resolvedTypeReferenceDirectives . get ( filePath ) ,
280+ resolution ,
281+ name ,
282+ mode ,
283+ filePath ,
284+ )
285+ ) ;
286+ actualProgram . getAutomaticTypeDirectiveResolutions ( ) . forEach ( ( resolution , name , mode ) =>
287+ verifyResolutionIsInCache (
288+ "AutoTypeRefs" ,
289+ actual . resolvedTypeReferenceDirectives . get ( inferredTypesPath ) ,
290+ resolution ,
291+ name ,
292+ mode ,
293+ inferredTypesPath ,
294+ )
295+ ) ;
251296
252297 expected . finishCachingPerDirectoryResolution ( actualProgram , /*oldProgram*/ undefined ) ;
253298
@@ -260,6 +305,10 @@ export function verifyResolutionCache(
260305 `Expected from:: ${ JSON . stringify ( info , undefined , " " ) } ` +
261306 `Actual from: ${ resolution . files ?. size } ` ,
262307 ) ;
308+ ts . Debug . assert (
309+ ! resolution . isInvalidated ,
310+ `${ projectName } :: Resolution should not be invalidated` ,
311+ ) ;
263312 verifySet ( resolutionToExpected . get ( resolution ) ! . files , resolution . files , `${ projectName } :: Resolution files` ) ;
264313 } ) ;
265314 verifyMapOfResolutionSet ( expected . resolvedFileToResolution , actual . resolvedFileToResolution , `resolvedFileToResolution` ) ;
@@ -295,24 +344,54 @@ export function verifyResolutionCache(
295344 ts . Debug . assert ( expected . countResolutionsResolvedWithGlobalCache ( ) === 0 , `${ projectName } :: ResolutionsResolvedWithGlobalCache should be cleared` ) ;
296345 ts . Debug . assert ( expected . countResolutionsResolvedWithoutGlobalCache ( ) === 0 , `${ projectName } :: ResolutionsResolvedWithoutGlobalCache should be cleared` ) ;
297346
347+ function verifyResolutionIsInCache < T extends ts . ResolutionWithFailedLookupLocations > (
348+ cacheType : string ,
349+ cache : ts . ModeAwareCache < T > | undefined ,
350+ resolution : T ,
351+ name : string ,
352+ mode : ts . ResolutionMode ,
353+ fileName : string ,
354+ ) {
355+ if ( resolution as unknown !== ts . emptyResolution ) {
356+ // Resolutions should match
357+ ts . Debug . assert (
358+ cache ?. get ( name , mode ) === resolution ,
359+ `${ projectName } :: ${ cacheType } :: ${ name } :: ${ mode } Expected resolution in program to be in cache ${ fileName } ` ,
360+ ) ;
361+ }
362+ else {
363+ // EmptyResolution is place holder and shouldnt be in the cache
364+ ts . Debug . assert (
365+ ! cache ?. has ( name , mode ) ,
366+ `${ projectName } :: ${ cacheType } :: ${ name } :: ${ mode } Ambient moduleResolution, should not be watched ${ fileName } ` ,
367+ ) ;
368+ }
369+ }
370+
298371 function collectResolutionToRefFromCache < T extends ts . ResolutionWithFailedLookupLocations > (
299372 cacheType : string ,
300373 fileName : ts . Path ,
301374 cache : ts . ModeAwareCache < T > | undefined ,
302375 getResolvedFileName : ( resolution : T ) => string | undefined ,
303376 deferWatchingNonRelativeResolution : boolean ,
304- storeExpcted : Map < ts . Path , ts . ModeAwareCache < ts . ResolutionWithFailedLookupLocations > > ,
377+ storeExpected : Map < ts . Path , ts . ModeAwareCache < ts . ResolutionWithFailedLookupLocations > > ,
378+ getProgramResolutions : ( name : string , mode : ts . ResolutionMode ) => T | undefined ,
305379 ) {
306380 ts . Debug . assert (
307- actualProgram . getSourceFileByPath ( fileName ) || ts . endsWith ( fileName , ts . inferredTypesContainingFile ) ,
381+ actualProgram . getSourceFileByPath ( fileName ) || inferredTypesPath === fileName ,
308382 `${ projectName } :: ${ cacheType } ${ fileName } Expect cache for file in program or auto type ref` ,
309383 ) ;
310384 let expectedCache : ts . ModeAwareCache < ts . ResolutionWithFailedLookupLocations > | undefined ;
311385 cache ?. forEach ( ( resolved , name , mode ) => {
312386 const resolvedFileName = getResolvedFileName ( resolved ) ;
313387 const expected = collectResolution ( cacheType , fileName , resolved , resolvedFileName , name , mode , deferWatchingNonRelativeResolution ) ;
314- if ( ! expectedCache ) storeExpcted . set ( fileName , expectedCache = ts . createModeAwareCache ( ) ) ;
388+ if ( ! expectedCache ) storeExpected . set ( fileName , expectedCache = ts . createModeAwareCache ( ) ) ;
315389 expectedCache . set ( name , mode , expected ) ;
390+ // Resolution in cache should be same as that is in program
391+ ts . Debug . assert (
392+ resolved === getProgramResolutions ( name , mode ) ,
393+ `${ projectName } :: ${ cacheType } ${ fileName } ${ name } ${ mode } Expected resolution in cache to be matched to that in the program` ,
394+ ) ;
316395 } ) ;
317396 }
318397
0 commit comments