@@ -393,7 +393,7 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
393393 private packageJsonsForAutoImport : Set < string > | undefined ;
394394
395395 /** @internal */
396- private noDtsResolutionProject ?: AuxiliaryProject | undefined ;
396+ noDtsResolutionProject ?: AuxiliaryProject | undefined ;
397397
398398 /** @internal */
399399 getResolvedProjectReferenceToRedirect ( _fileName : string ) : ResolvedProjectReference | undefined {
@@ -969,13 +969,27 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
969969 this . projectService . onUpdateLanguageServiceStateForProject ( this , /*languageServiceEnabled*/ true ) ;
970970 }
971971
972+ /** @internal */
973+ cleanupProgram ( ) {
974+ if ( this . program ) {
975+ // Root files are always attached to the project irrespective of program
976+ for ( const f of this . program . getSourceFiles ( ) ) {
977+ this . detachScriptInfoIfNotRoot ( f . fileName ) ;
978+ }
979+ this . program . forEachResolvedProjectReference ( ref =>
980+ this . detachScriptInfoFromProject ( ref . sourceFile . fileName ) ) ;
981+ this . program = undefined ;
982+ }
983+ }
984+
972985 disableLanguageService ( lastFileExceededProgramSize ?: string ) {
973986 if ( ! this . languageServiceEnabled ) {
974987 return ;
975988 }
976989 Debug . assert ( this . projectService . serverMode !== LanguageServiceMode . Syntactic ) ;
977990 this . languageService . cleanupSemanticCache ( ) ;
978991 this . languageServiceEnabled = false ;
992+ this . cleanupProgram ( ) ;
979993 this . lastFileExceededProgramSize = lastFileExceededProgramSize ;
980994 this . builderState = undefined ;
981995 if ( this . autoImportProviderHost ) {
@@ -984,6 +998,7 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
984998 this . autoImportProviderHost = undefined ;
985999 this . resolutionCache . closeTypeRootsWatch ( ) ;
9861000 this . clearGeneratedFileWatch ( ) ;
1001+ this . projectService . verifyDocumentRegistry ( ) ;
9871002 this . projectService . onUpdateLanguageServiceStateForProject ( this , /*languageServiceEnabled*/ false ) ;
9881003 }
9891004
@@ -1030,17 +1045,10 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
10301045 close ( ) {
10311046 this . projectService . typingsCache . onProjectClosed ( this ) ;
10321047 this . closeWatchingTypingLocations ( ) ;
1033- if ( this . program ) {
1034- // if we have a program - release all files that are enlisted in program but arent root
1035- // The releasing of the roots happens later
1036- // The project could have pending update remaining and hence the info could be in the files but not in program graph
1037- for ( const f of this . program . getSourceFiles ( ) ) {
1038- this . detachScriptInfoIfNotRoot ( f . fileName ) ;
1039- }
1040- this . program . forEachResolvedProjectReference ( ref =>
1041- this . detachScriptInfoFromProject ( ref . sourceFile . fileName ) ) ;
1042- }
1043-
1048+ // if we have a program - release all files that are enlisted in program but arent root
1049+ // The releasing of the roots happens later
1050+ // The project could have pending update remaining and hence the info could be in the files but not in program graph
1051+ this . cleanupProgram ( ) ;
10441052 // Release external files
10451053 forEach ( this . externalFiles , externalFile => this . detachScriptInfoIfNotRoot ( externalFile ) ) ;
10461054 // Always remove root files from the project
@@ -1492,6 +1500,7 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
14921500
14931501 private updateGraphWorker ( ) {
14941502 const oldProgram = this . languageService . getCurrentProgram ( ) ;
1503+ Debug . assert ( oldProgram === this . program ) ;
14951504 Debug . assert ( ! this . isClosed ( ) , "Called update graph worker of closed project" ) ;
14961505 this . writeLog ( `Starting updateGraphWorker: Project: ${ this . getProjectName ( ) } ` ) ;
14971506 const start = timestamp ( ) ;
@@ -1633,6 +1642,8 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
16331642 else if ( this . program !== oldProgram ) {
16341643 this . writeLog ( `Different program with same set of files` ) ;
16351644 }
1645+ // Verify the document registry count
1646+ this . projectService . verifyDocumentRegistry ( ) ;
16361647 return hasNewProgram ;
16371648 }
16381649
@@ -2342,7 +2353,8 @@ export class InferredProject extends Project {
23422353 }
23432354}
23442355
2345- class AuxiliaryProject extends Project {
2356+ /** @internal */
2357+ export class AuxiliaryProject extends Project {
23462358 constructor ( projectService : ProjectService , documentRegistry : DocumentRegistry , compilerOptions : CompilerOptions , currentDirectory : string ) {
23472359 super ( projectService . newAuxiliaryProjectName ( ) ,
23482360 ProjectKind . Auxiliary ,
@@ -2361,7 +2373,6 @@ class AuxiliaryProject extends Project {
23612373 return true ;
23622374 }
23632375
2364- /** @internal */
23652376 override scheduleInvalidateResolutionsOfFailedLookupLocations ( ) : void {
23662377 // Invalidation will happen on-demand as part of updateGraph
23672378 return ;
0 commit comments