@@ -264,7 +264,7 @@ export function verifyResolutionCache(
264
264
resolutionToExpected . get ( resolution ) ! . refCount === resolution . refCount ,
265
265
`${ projectName } :: Expected Resolution ref count ${ resolutionToExpected . get ( resolution ) ! . refCount } but got ${ resolution . refCount } ` ,
266
266
) ;
267
- verifySet ( resolutionToExpected . get ( resolution ) ! . files , resolution . files , `Resolution files` ) ;
267
+ verifySet ( resolutionToExpected . get ( resolution ) ! . files , resolution . files , `${ projectName } :: Resolution files` ) ;
268
268
} ) ;
269
269
verifyMapOfResolutionSet ( expected . resolvedFileToResolution , actual . resolvedFileToResolution , `resolvedFileToResolution` ) ;
270
270
verifyResolutionSet ( expected . resolutionsWithFailedLookups , actual . resolutionsWithFailedLookups , `resolutionsWithFailedLookups` ) ;
@@ -339,35 +339,6 @@ export function verifyResolutionCache(
339
339
return expectedResolution ;
340
340
}
341
341
342
- function verifyMap < Expected , Actual > (
343
- expected : Map < string , Expected > | undefined ,
344
- actual : Map < string , Actual > | undefined ,
345
- verifyValue : ( expected : Expected | undefined , actual : Actual | undefined , key : string ) => void ,
346
- caption : string ,
347
- ) {
348
- expected ?. forEach ( ( expected , path ) => verifyValue ( expected , actual ?. get ( path ) , `${ caption } :: ${ path } ` ) ) ;
349
- actual ?. forEach ( ( actual , path ) => verifyValue ( expected ?. get ( path ) , actual , `${ caption } :: ${ path } ` ) ) ;
350
- }
351
-
352
- function verifySet (
353
- expected : Set < string > | undefined ,
354
- actual : Set < string > | undefined ,
355
- caption : string ,
356
- ) {
357
- expected ?. forEach ( expected =>
358
- ts . Debug . assert (
359
- actual ?. has ( expected ) ,
360
- `${ projectName } :: ${ caption } :: Expected should be present in actual` ,
361
- )
362
- ) ;
363
- actual ?. forEach ( actual =>
364
- ts . Debug . assert (
365
- expected ?. has ( actual ) ,
366
- `${ projectName } :: ${ caption } :: Actual should be present in expected` ,
367
- )
368
- ) ;
369
- }
370
-
371
342
function verifyMapOfResolutionSet (
372
343
expected : Map < ts . Path , Set < ts . ResolutionWithFailedLookupLocations > > | undefined ,
373
344
actual : Map < ts . Path , Set < ts . ResolutionWithFailedLookupLocations > > | undefined ,
@@ -421,6 +392,35 @@ export function verifyResolutionCache(
421
392
}
422
393
}
423
394
395
+ function verifyMap < Key extends string , Expected , Actual > (
396
+ expected : Map < Key , Expected > | undefined ,
397
+ actual : Map < Key , Actual > | undefined ,
398
+ verifyValue : ( expected : Expected | undefined , actual : Actual | undefined , key : string ) => void ,
399
+ caption : string ,
400
+ ) {
401
+ expected ?. forEach ( ( expected , path ) => verifyValue ( expected , actual ?. get ( path ) , `${ caption } :: ${ path } ` ) ) ;
402
+ actual ?. forEach ( ( actual , path ) => verifyValue ( expected ?. get ( path ) , actual , `${ caption } :: ${ path } ` ) ) ;
403
+ }
404
+
405
+ function verifySet (
406
+ expected : Set < string > | undefined ,
407
+ actual : Set < string > | undefined ,
408
+ caption : string ,
409
+ ) {
410
+ expected ?. forEach ( expected =>
411
+ ts . Debug . assert (
412
+ actual ?. has ( expected ) ,
413
+ `${ caption } :: Expected should be present in actual` ,
414
+ )
415
+ ) ;
416
+ actual ?. forEach ( actual =>
417
+ ts . Debug . assert (
418
+ expected ?. has ( actual ) ,
419
+ `${ caption } :: Actual should be present in expected` ,
420
+ )
421
+ ) ;
422
+ }
423
+
424
424
function verifyProgram ( service : ts . server . ProjectService , project : ts . server . Project ) {
425
425
if ( service . serverMode === ts . LanguageServiceMode . Syntactic ) return ;
426
426
const options = project . getCompilerOptions ( ) ;
@@ -510,6 +510,74 @@ function verifyProgram(service: ts.server.ProjectService, project: ts.server.Pro
510
510
verifyResolutionCache ( project . resolutionCache , project . getCurrentProgram ( ) ! , resolutionHostCacheHost , project . projectName ) ;
511
511
}
512
512
513
+ interface ResolveSingleModuleNameWithoutWatchingData {
514
+ resolutionToData : Map < ts . ResolutionWithFailedLookupLocations , Pick < ts . ResolvedModuleWithFailedLookupLocations , "failedLookupLocations" | "affectingLocations" | "resolutionDiagnostics" > > ;
515
+ packageJsonMap : Map < ts . Path , ts . PackageJsonInfo | boolean > | undefined ;
516
+ }
517
+
518
+ function beforeResolveSingleModuleNameWithoutWatching (
519
+ moduleResolutionCache : ts . ModuleResolutionCache ,
520
+ ) : ResolveSingleModuleNameWithoutWatchingData {
521
+ const resolutionToData : ResolveSingleModuleNameWithoutWatchingData [ "resolutionToData" ] = new Map ( ) ;
522
+ // Currently it doesnt matter if moduleResolutionCache itself changes or not so just verify resolutions:
523
+ moduleResolutionCache . directoryToModuleNameMap . getOwnMap ( ) . forEach ( cache => {
524
+ cache . forEach ( resolution => {
525
+ if ( resolutionToData . has ( resolution ) ) return ;
526
+ resolutionToData . set ( resolution , {
527
+ failedLookupLocations : resolution . failedLookupLocations ?. slice ( ) ,
528
+ affectingLocations : resolution . affectingLocations ?. slice ( ) ,
529
+ resolutionDiagnostics : resolution . resolutionDiagnostics ?. slice ( ) ,
530
+ } ) ;
531
+ } ) ;
532
+ } ) ;
533
+
534
+ // We also care about package json info cache
535
+ const packageJsonMap = moduleResolutionCache . getPackageJsonInfoCache ( ) . getInternalMap ( ) ;
536
+ return {
537
+ resolutionToData,
538
+ packageJsonMap : packageJsonMap && new Map ( packageJsonMap ) ,
539
+ } ;
540
+ }
541
+
542
+ function afterResolveSingleModuleNameWithoutWatching (
543
+ moduleResolutionCache : ts . ModuleResolutionCache ,
544
+ moduleName : string ,
545
+ containingFile : string ,
546
+ result : ts . ResolvedModuleWithFailedLookupLocations ,
547
+ data : ResolveSingleModuleNameWithoutWatchingData ,
548
+ ) {
549
+ const existing = data . resolutionToData . get ( result ) ;
550
+ if ( existing ) {
551
+ verifyArrayLength ( existing . failedLookupLocations , result . failedLookupLocations , "failedLookupLocations" ) ;
552
+ verifyArrayLength ( existing . affectingLocations , result . affectingLocations , "affectingLocations" ) ;
553
+ verifyArrayLength ( existing . resolutionDiagnostics , result . resolutionDiagnostics , "resolutionDiagnostics" ) ;
554
+ }
555
+
556
+ verifyMap (
557
+ data . packageJsonMap ,
558
+ moduleResolutionCache . getPackageJsonInfoCache ( ) . getInternalMap ( ) ,
559
+ ( expected , actual , caption ) => ts . Debug . assert ( expected === actual , caption ) ,
560
+ `Expected packageJsonInfo to not change: ${ moduleName } ${ containingFile } ` ,
561
+ ) ;
562
+
563
+ function verifyArrayLength < T > ( expected : T [ ] | undefined , actual : T [ ] | undefined , caption : string ) {
564
+ ts . Debug . assert (
565
+ expected ?. length === actual ?. length ,
566
+ `Expected ${ caption } to not change: ${ moduleName } ${ containingFile } ` ,
567
+ ( ) =>
568
+ `Expected: ${ JSON . stringify ( expected , undefined , " " ) } ` +
569
+ `Actual: ${ JSON . stringify ( actual , undefined , " " ) } ` ,
570
+ ) ;
571
+ }
572
+ }
573
+
574
+ function onProjectCreation ( project : ts . server . Project ) {
575
+ if ( project . projectKind !== ts . server . ProjectKind . Auxiliary ) return ;
576
+
577
+ ( project as ts . ResolutionCacheHost ) . beforeResolveSingleModuleNameWithoutWatching = beforeResolveSingleModuleNameWithoutWatching ;
578
+ ( project as ts . ResolutionCacheHost ) . afterResolveSingleModuleNameWithoutWatching = afterResolveSingleModuleNameWithoutWatching ;
579
+ }
580
+
513
581
export interface IncrementalVerifierCallbacks {
514
582
beforeVerification ?( ) : any ;
515
583
afterVerification ?( dataFromBefore : any ) : void ;
@@ -518,6 +586,7 @@ export interface IncrementalVerifierCallbacks {
518
586
export function incrementalVerifier ( service : ts . server . ProjectService ) {
519
587
service . verifyDocumentRegistry = withIncrementalVerifierCallbacks ( service , verifyDocumentRegistry ) ;
520
588
service . verifyProgram = withIncrementalVerifierCallbacks ( service , verifyProgram ) ;
589
+ service . onProjectCreation = onProjectCreation ;
521
590
}
522
591
523
592
function withIncrementalVerifierCallbacks (
0 commit comments