@@ -563,10 +563,17 @@ namespace ts.server {
563
563
}
564
564
else {
565
565
if ( info && ( ! info . isScriptOpen ( ) ) ) {
566
- // file has been changed which might affect the set of referenced files in projects that include
567
- // this file and set of inferred projects
568
- info . reloadFromFile ( ) ;
569
- this . updateProjectGraphs ( info . containingProjects ) ;
566
+ if ( info . containingProjects . length === 0 ) {
567
+ // Orphan script info, remove it as we can always reload it on next open
568
+ info . stopWatcher ( ) ;
569
+ this . filenameToScriptInfo . remove ( info . path ) ;
570
+ }
571
+ else {
572
+ // file has been changed which might affect the set of referenced files in projects that include
573
+ // this file and set of inferred projects
574
+ info . reloadFromFile ( ) ;
575
+ this . updateProjectGraphs ( info . containingProjects ) ;
576
+ }
570
577
}
571
578
}
572
579
}
@@ -828,8 +835,17 @@ namespace ts.server {
828
835
}
829
836
}
830
837
831
- // Cleanup script infos that are not open and not part of any project
832
- this . deleteOrphanScriptInfoNotInAnyProject ( ) ;
838
+ // Cleanup script infos that arent part of any project is postponed to
839
+ // next file open so that if file from same project is opened we wont end up creating same script infos
840
+ }
841
+
842
+ // If the current info is being just closed - add the watcher file to track changes
843
+ // But if file was deleted, handle that part
844
+ if ( this . host . fileExists ( info . fileName ) ) {
845
+ this . watchClosedScriptInfo ( info ) ;
846
+ }
847
+ else {
848
+ this . handleDeletedFile ( info ) ;
833
849
}
834
850
}
835
851
@@ -1310,6 +1326,14 @@ namespace ts.server {
1310
1326
return this . getScriptInfoForNormalizedPath ( toNormalizedPath ( uncheckedFileName ) ) ;
1311
1327
}
1312
1328
1329
+ watchClosedScriptInfo ( info : ScriptInfo ) {
1330
+ // do not watch files with mixed content - server doesn't know how to interpret it
1331
+ if ( ! info . hasMixedContent ) {
1332
+ const { fileName } = info ;
1333
+ info . setWatcher ( this . host . watchFile ( fileName , _ => this . onSourceFileChanged ( fileName ) ) ) ;
1334
+ }
1335
+ }
1336
+
1313
1337
getOrCreateScriptInfoForNormalizedPath ( fileName : NormalizedPath , openedByClient : boolean , fileContent ?: string , scriptKind ?: ScriptKind , hasMixedContent ?: boolean ) {
1314
1338
let info = this . getScriptInfoForNormalizedPath ( fileName ) ;
1315
1339
if ( ! info ) {
@@ -1325,15 +1349,13 @@ namespace ts.server {
1325
1349
}
1326
1350
}
1327
1351
else {
1328
- // do not watch files with mixed content - server doesn't know how to interpret it
1329
- if ( ! hasMixedContent ) {
1330
- info . setWatcher ( this . host . watchFile ( fileName , _ => this . onSourceFileChanged ( fileName ) ) ) ;
1331
- }
1352
+ this . watchClosedScriptInfo ( info ) ;
1332
1353
}
1333
1354
}
1334
1355
}
1335
1356
if ( info ) {
1336
1357
if ( openedByClient && ! info . isScriptOpen ( ) ) {
1358
+ info . stopWatcher ( ) ;
1337
1359
info . open ( fileContent ) ;
1338
1360
if ( hasMixedContent ) {
1339
1361
info . registerFileUpdate ( ) ;
@@ -1429,7 +1451,6 @@ namespace ts.server {
1429
1451
p . updateGraph ( ) ;
1430
1452
}
1431
1453
1432
- this . deleteOrphanScriptInfoNotInAnyProject ( ) ;
1433
1454
this . printProjects ( ) ;
1434
1455
}
1435
1456
@@ -1463,6 +1484,7 @@ namespace ts.server {
1463
1484
// at this point if file is the part of some configured/external project then this project should be created
1464
1485
const info = this . getOrCreateScriptInfoForNormalizedPath ( fileName , /*openedByClient*/ true , fileContent , scriptKind , hasMixedContent ) ;
1465
1486
this . assignScriptInfoToInferredProjectIfNecessary ( info , /*addToListOfOpenFiles*/ true ) ;
1487
+ this . deleteOrphanScriptInfoNotInAnyProject ( ) ;
1466
1488
this . printProjects ( ) ;
1467
1489
return { configFileName, configFileErrors } ;
1468
1490
}
0 commit comments