@@ -204,6 +204,7 @@ export function verifyResolutionCache(
204
204
actualProgram : ts . Program ,
205
205
resolutionHostCacheHost : ts . ResolutionCacheHost ,
206
206
projectName : string ,
207
+ userResolvedModuleNames ?: true ,
207
208
) {
208
209
const currentDirectory = resolutionHostCacheHost . getCurrentDirectory ! ( ) ;
209
210
const expected = ts . createResolutionCache ( resolutionHostCacheHost , actual . rootDirForResolution ) ;
@@ -214,6 +215,7 @@ export function verifyResolutionCache(
214
215
const expectedToResolution = new Map < ExpectedResolution , ts . ResolutionWithFailedLookupLocations > ( ) ;
215
216
const resolutionToExpected = new Map < ts . ResolutionWithFailedLookupLocations , ExpectedResolution > ( ) ;
216
217
const resolutionToRefs = new Map < ts . ResolutionWithFailedLookupLocations , ResolutionInfo [ ] > ( ) ;
218
+ const inferredTypesPath = resolutionHostCacheHost . toPath ( ts . getAutomaticTypeDirectiveContainingFile ( actualProgram . getCompilerOptions ( ) , currentDirectory ) ) ;
217
219
actual . resolvedModuleNames . forEach ( ( resolutions , path ) =>
218
220
collectResolutionToRefFromCache (
219
221
"Modules" ,
@@ -222,6 +224,7 @@ export function verifyResolutionCache(
222
224
getResolvedModuleFileName ,
223
225
/*deferWatchingNonRelativeResolution*/ true ,
224
226
expected . resolvedModuleNames ,
227
+ ( name , mode ) => actualProgram . getResolvedModule ( actualProgram . getSourceFileByPath ( path ) ! , name , mode ) ,
225
228
)
226
229
) ;
227
230
actual . resolvedTypeReferenceDirectives . forEach ( ( resolutions , path ) =>
@@ -232,6 +235,10 @@ export function verifyResolutionCache(
232
235
getResolvedTypeRefFileName ,
233
236
/*deferWatchingNonRelativeResolution*/ false ,
234
237
expected . resolvedTypeReferenceDirectives ,
238
+ ( name , mode ) =>
239
+ path !== inferredTypesPath ?
240
+ actualProgram . getResolvedTypeReferenceDirective ( actualProgram . getSourceFileByPath ( path ) ! , name , mode ) :
241
+ actualProgram . getAutomaticTypeDirectiveResolutions ( ) . get ( name , mode ) ,
235
242
)
236
243
) ;
237
244
actual . resolvedLibraries . forEach ( ( resolved , libFileName ) => {
@@ -248,6 +255,39 @@ export function verifyResolutionCache(
248
255
) ;
249
256
expected . resolvedLibraries . set ( libFileName , expectedResolution ) ;
250
257
} ) ;
258
+ // Check for resolutions in program but not in cache to empty resolutions
259
+ if ( ! userResolvedModuleNames ) {
260
+ actualProgram . forEachResolvedModule ( ( resolution , name , mode , filePath ) =>
261
+ verifyResolutionIsInCache (
262
+ "Modules" ,
263
+ actual . resolvedModuleNames . get ( filePath ) ,
264
+ resolution ,
265
+ name ,
266
+ mode ,
267
+ filePath ,
268
+ )
269
+ ) ;
270
+ }
271
+ actualProgram . forEachResolvedTypeReferenceDirective ( ( resolution , name , mode , filePath ) =>
272
+ verifyResolutionIsInCache (
273
+ "TypeRefs" ,
274
+ actual . resolvedTypeReferenceDirectives . get ( filePath ) ,
275
+ resolution ,
276
+ name ,
277
+ mode ,
278
+ filePath ,
279
+ )
280
+ ) ;
281
+ actualProgram . getAutomaticTypeDirectiveResolutions ( ) . forEach ( ( resolution , name , mode ) =>
282
+ verifyResolutionIsInCache (
283
+ "AutoTypeRefs" ,
284
+ actual . resolvedTypeReferenceDirectives . get ( inferredTypesPath ) ,
285
+ resolution ,
286
+ name ,
287
+ mode ,
288
+ inferredTypesPath ,
289
+ )
290
+ ) ;
251
291
252
292
expected . finishCachingPerDirectoryResolution ( actualProgram , /*oldProgram*/ undefined ) ;
253
293
@@ -260,6 +300,10 @@ export function verifyResolutionCache(
260
300
`Expected from:: ${ JSON . stringify ( info , undefined , " " ) } ` +
261
301
`Actual from: ${ resolution . refCount } ` ,
262
302
) ;
303
+ ts . Debug . assert (
304
+ ! resolution . isInvalidated ,
305
+ `${ projectName } :: Resolution should not be invalidated` ,
306
+ ) ;
263
307
ts . Debug . assert (
264
308
resolutionToExpected . get ( resolution ) ! . refCount === resolution . refCount ,
265
309
`${ projectName } :: Expected Resolution ref count ${ resolutionToExpected . get ( resolution ) ! . refCount } but got ${ resolution . refCount } ` ,
@@ -298,24 +342,54 @@ export function verifyResolutionCache(
298
342
ts . Debug . assert ( expected . countResolutionsResolvedWithGlobalCache ( ) === 0 , `${ projectName } :: ResolutionsResolvedWithGlobalCache should be cleared` ) ;
299
343
ts . Debug . assert ( expected . countResolutionsResolvedWithoutGlobalCache ( ) === 0 , `${ projectName } :: ResolutionsResolvedWithoutGlobalCache should be cleared` ) ;
300
344
345
+ function verifyResolutionIsInCache < T extends ts . ResolutionWithFailedLookupLocations > (
346
+ cacheType : string ,
347
+ cache : ts . ModeAwareCache < T > | undefined ,
348
+ resolution : T ,
349
+ name : string ,
350
+ mode : ts . ResolutionMode ,
351
+ fileName : string ,
352
+ ) {
353
+ if ( resolution as unknown !== ts . emptyResolution ) {
354
+ // Resolutions should match
355
+ ts . Debug . assert (
356
+ cache ?. get ( name , mode ) === resolution ,
357
+ `${ projectName } :: ${ cacheType } :: ${ name } :: ${ mode } Expected resolution in program to be in cache ${ fileName } ` ,
358
+ ) ;
359
+ }
360
+ else {
361
+ // EmptyResolution is place holder and shouldnt be in the cache
362
+ ts . Debug . assert (
363
+ ! cache ?. has ( name , mode ) ,
364
+ `${ projectName } :: ${ cacheType } :: ${ name } :: ${ mode } Ambient moduleResolution, should not be watched ${ fileName } ` ,
365
+ ) ;
366
+ }
367
+ }
368
+
301
369
function collectResolutionToRefFromCache < T extends ts . ResolutionWithFailedLookupLocations > (
302
370
cacheType : string ,
303
371
fileName : ts . Path ,
304
372
cache : ts . ModeAwareCache < T > | undefined ,
305
373
getResolvedFileName : ( resolution : T ) => string | undefined ,
306
374
deferWatchingNonRelativeResolution : boolean ,
307
- storeExpcted : Map < ts . Path , ts . ModeAwareCache < ts . ResolutionWithFailedLookupLocations > > ,
375
+ storeExpected : Map < ts . Path , ts . ModeAwareCache < ts . ResolutionWithFailedLookupLocations > > ,
376
+ getProgramResolutions : ( name : string , mode : ts . ResolutionMode ) => T | undefined ,
308
377
) {
309
378
ts . Debug . assert (
310
- actualProgram . getSourceFileByPath ( fileName ) || ts . endsWith ( fileName , ts . inferredTypesContainingFile ) ,
379
+ actualProgram . getSourceFileByPath ( fileName ) || inferredTypesPath === fileName ,
311
380
`${ projectName } :: ${ cacheType } ${ fileName } Expect cache for file in program or auto type ref` ,
312
381
) ;
313
382
let expectedCache : ts . ModeAwareCache < ts . ResolutionWithFailedLookupLocations > | undefined ;
314
383
cache ?. forEach ( ( resolved , name , mode ) => {
315
384
const resolvedFileName = getResolvedFileName ( resolved ) ;
316
385
const expected = collectResolution ( cacheType , fileName , resolved , resolvedFileName , name , mode , deferWatchingNonRelativeResolution ) ;
317
- if ( ! expectedCache ) storeExpcted . set ( fileName , expectedCache = ts . createModeAwareCache ( ) ) ;
386
+ if ( ! expectedCache ) storeExpected . set ( fileName , expectedCache = ts . createModeAwareCache ( ) ) ;
318
387
expectedCache . set ( name , mode , expected ) ;
388
+ // Resolution in cache should be same as that is in program
389
+ ts . Debug . assert (
390
+ resolved === getProgramResolutions ( name , mode ) ,
391
+ `${ projectName } :: ${ cacheType } ${ fileName } ${ name } ${ mode } Expected resolution in cache to be matched to that in the program` ,
392
+ ) ;
319
393
} ) ;
320
394
}
321
395
0 commit comments