@@ -233,9 +233,9 @@ namespace ts.server {
233233 this . realpath = path => host . realpath ( path ) ;
234234 }
235235
236- this . languageService = createLanguageService ( this , this . documentRegistry ) ;
237236 // Use the current directory as resolution root only if the project created using current directory string
238237 this . resolutionCache = createResolutionCache ( this , currentDirectory && this . currentDirectory ) ;
238+ this . languageService = createLanguageService ( this , this . documentRegistry ) ;
239239 if ( ! languageServiceEnabled ) {
240240 this . disableLanguageService ( ) ;
241241 }
@@ -498,25 +498,23 @@ namespace ts.server {
498498
499499 close ( ) {
500500 if ( this . program ) {
501- // if we have a program - release all files that are enlisted in program
501+ // if we have a program - release all files that are enlisted in program but arent root
502+ // The releasing of the roots happens later
503+ // The project could have pending update remaining and hence the info could be in the files but not in program graph
502504 for ( const f of this . program . getSourceFiles ( ) ) {
503- const info = this . projectService . getScriptInfo ( f . fileName ) ;
504- // We might not find the script info in case its not associated with the project any more
505- // and project graph was not updated (eg delayed update graph in case of files changed/deleted on the disk)
506- if ( info ) {
507- info . detachFromProject ( this ) ;
508- }
505+ this . detachScriptInfoIfNotRoot ( f . fileName ) ;
509506 }
510507 }
511- if ( ! this . program || ! this . languageServiceEnabled ) {
512- // release all root files either if there is no program or language service is disabled.
513- // in the latter case set of root files can be larger than the set of files in program.
514- for ( const root of this . rootFiles ) {
515- root . detachFromProject ( this ) ;
516- }
508+ // Release external files
509+ forEach ( this . externalFiles , externalFile => this . detachScriptInfoIfNotRoot ( externalFile ) ) ;
510+ // Always remove root files from the project
511+ for ( const root of this . rootFiles ) {
512+ root . detachFromProject ( this ) ;
517513 }
514+
518515 this . rootFiles = undefined ;
519516 this . rootFilesMap = undefined ;
517+ this . externalFiles = undefined ;
520518 this . program = undefined ;
521519 this . builder = undefined ;
522520 this . resolutionCache . clear ( ) ;
@@ -535,6 +533,15 @@ namespace ts.server {
535533 this . languageService = undefined ;
536534 }
537535
536+ private detachScriptInfoIfNotRoot ( uncheckedFilename : string ) {
537+ const info = this . projectService . getScriptInfo ( uncheckedFilename ) ;
538+ // We might not find the script info in case its not associated with the project any more
539+ // and project graph was not updated (eg delayed update graph in case of files changed/deleted on the disk)
540+ if ( info && ! this . isRoot ( info ) ) {
541+ info . detachFromProject ( this ) ;
542+ }
543+ }
544+
538545 isClosed ( ) {
539546 return this . rootFiles === undefined ;
540547 }
@@ -735,7 +742,6 @@ namespace ts.server {
735742 */
736743 updateGraph ( ) : boolean {
737744 this . resolutionCache . startRecordingFilesWithChangedResolutions ( ) ;
738- this . hasInvalidatedResolution = this . resolutionCache . createHasInvalidatedResolution ( ) ;
739745
740746 let hasChanges = this . updateGraphWorker ( ) ;
741747
@@ -795,9 +801,10 @@ namespace ts.server {
795801
796802 private updateGraphWorker ( ) {
797803 const oldProgram = this . program ;
798-
804+ Debug . assert ( ! this . isClosed ( ) , "Called update graph worker of closed project" ) ;
799805 this . writeLog ( `Starting updateGraphWorker: Project: ${ this . getProjectName ( ) } ` ) ;
800806 const start = timestamp ( ) ;
807+ this . hasInvalidatedResolution = this . resolutionCache . createHasInvalidatedResolution ( ) ;
801808 this . resolutionCache . startCachingPerDirectoryResolution ( ) ;
802809 this . program = this . languageService . getProgram ( ) ;
803810 this . resolutionCache . finishCachingPerDirectoryResolution ( ) ;
@@ -1320,14 +1327,13 @@ namespace ts.server {
13201327 }
13211328
13221329 close ( ) {
1323- super . close ( ) ;
1324-
13251330 if ( this . configFileWatcher ) {
13261331 this . configFileWatcher . close ( ) ;
13271332 this . configFileWatcher = undefined ;
13281333 }
13291334
13301335 this . stopWatchingWildCards ( ) ;
1336+ super . close ( ) ;
13311337 }
13321338
13331339 addOpenRef ( ) {
0 commit comments