@@ -107,6 +107,7 @@ namespace ts.server {
107107 private rootFilesMap : Map < ScriptInfo > = createMap < ScriptInfo > ( ) ;
108108 private program : ts . Program ;
109109 private externalFiles : SortedReadonlyArray < string > ;
110+ private missingFilesMap : FileMap < FileWatcher > = createFileMap < FileWatcher > ( ) ;
110111
111112 private cachedUnresolvedImportsPerFile = new UnresolvedImportsMap ( ) ;
112113 private lastCachedUnresolvedImportsList : SortedReadonlyArray < string > ;
@@ -606,6 +607,39 @@ namespace ts.server {
606607 }
607608 }
608609
610+ if ( hasChanges && this . program . getMissingFilePaths ) {
611+ const missingFilePaths = this . program . getMissingFilePaths ( ) || emptyArray ;
612+ const missingFilePathsSet = createMap < true > ( ) ;
613+ missingFilePaths . forEach ( p => missingFilePathsSet . set ( p , true ) ) ;
614+
615+ // Files that are no longer missing (e.g. because they are no longer required)
616+ // should no longer be watched.
617+ this . missingFilesMap . getKeys ( ) . forEach ( p => {
618+ if ( ! missingFilePathsSet . has ( p ) ) {
619+ this . missingFilesMap . get ( p ) . close ( ) ;
620+ this . missingFilesMap . remove ( p ) ;
621+ }
622+ } ) ;
623+
624+ // Missing files that are not yet watched should be added to the map.
625+ missingFilePaths . forEach ( p => {
626+ if ( ! this . missingFilesMap . contains ( p ) ) {
627+ const fileWatcher = ts . sys . watchFile ( p , ( _filename : string , removed ?: boolean ) => {
628+ // removed = deleted ? true : (added ? false : undefined)
629+ if ( removed === false && this . missingFilesMap . contains ( p ) ) {
630+ fileWatcher . close ( ) ;
631+ this . missingFilesMap . remove ( p ) ;
632+
633+ // When a missing file is created, we should update the graph.
634+ this . markAsDirty ( ) ;
635+ this . updateGraph ( ) ;
636+ }
637+ } ) ;
638+ this . missingFilesMap . set ( p , fileWatcher ) ;
639+ }
640+ } ) ;
641+ }
642+
609643 const oldExternalFiles = this . externalFiles || emptyArray as SortedReadonlyArray < string > ;
610644 this . externalFiles = this . getExternalFiles ( ) ;
611645 enumerateInsertsAndDeletes ( this . externalFiles , oldExternalFiles ,
@@ -626,6 +660,10 @@ namespace ts.server {
626660 return hasChanges ;
627661 }
628662
663+ isWatchedMissingFile ( path : Path ) {
664+ return this . missingFilesMap . contains ( path ) ;
665+ }
666+
629667 getScriptInfoLSHost ( fileName : string ) {
630668 const scriptInfo = this . projectService . getOrCreateScriptInfo ( fileName , /*openedByClient*/ false ) ;
631669 if ( scriptInfo ) {
0 commit comments