@@ -730,6 +730,7 @@ namespace ts {
730
730
731
731
const nodeVersion = getNodeMajorVersion ( ) ;
732
732
const isNode4OrLater = nodeVersion ! >= 4 ;
733
+ const isLinuxOrMacOs = process . platform === "linux" || process . platform === "darwin" ;
733
734
734
735
const platform : string = _os . platform ( ) ;
735
736
const useCaseSensitiveFileNames = isFileSystemCaseSensitive ( ) ;
@@ -1029,6 +1030,12 @@ namespace ts {
1029
1030
1030
1031
function fsWatch ( fileOrDirectory : string , entryKind : FileSystemEntryKind . File | FileSystemEntryKind . Directory , callback : FsWatchCallback , recursive : boolean , fallbackPollingWatchFile : HostWatchFile , pollingInterval ?: number ) : FileWatcher {
1031
1032
let options : any ;
1033
+ const lastDirectoryPartWithDirectorySeparator = isLinuxOrMacOs ?
1034
+ fileOrDirectory . substr ( fileOrDirectory . lastIndexOf ( directorySeparator ) ) :
1035
+ undefined ;
1036
+ const lastDirectoryPart = isLinuxOrMacOs ?
1037
+ lastDirectoryPartWithDirectorySeparator ! . slice ( directorySeparator . length ) :
1038
+ undefined ;
1032
1039
/** Watcher for the file system entry depending on whether it is missing or present */
1033
1040
let watcher = ! fileSystemEntryExists ( fileOrDirectory , entryKind ) ?
1034
1041
watchMissingFileSystemEntry ( ) :
@@ -1072,11 +1079,12 @@ namespace ts {
1072
1079
}
1073
1080
}
1074
1081
try {
1075
-
1076
1082
const presentWatcher = _fs . watch (
1077
1083
fileOrDirectory ,
1078
1084
options ,
1079
- callback
1085
+ isLinuxOrMacOs ?
1086
+ callbackChangingToMissingFileSystemEntry :
1087
+ callback
1080
1088
) ;
1081
1089
// Watch the missing file or directory or error
1082
1090
presentWatcher . on ( "error" , ( ) => invokeCallbackAndUpdateWatcher ( watchMissingFileSystemEntry ) ) ;
@@ -1090,6 +1098,18 @@ namespace ts {
1090
1098
}
1091
1099
}
1092
1100
1101
+ function callbackChangingToMissingFileSystemEntry ( event : "rename" | "change" , relativeName : string | undefined ) {
1102
+ // because relativeName is not guaranteed to be correct we need to check on each rename with few combinations
1103
+ // Eg on ubuntoo while watchin app/node_modules the relativeName is "node_modules" which is neither relative nor full path
1104
+ return event === "rename" &&
1105
+ ( ! relativeName ||
1106
+ relativeName === lastDirectoryPart ||
1107
+ relativeName . lastIndexOf ( lastDirectoryPartWithDirectorySeparator ! ) === relativeName . length - lastDirectoryPartWithDirectorySeparator ! . length ) &&
1108
+ ! fileSystemEntryExists ( fileOrDirectory , entryKind ) ?
1109
+ invokeCallbackAndUpdateWatcher ( watchMissingFileSystemEntry ) :
1110
+ callback ( event , relativeName ) ;
1111
+ }
1112
+
1093
1113
/**
1094
1114
* Watch the file or directory using fs.watchFile since fs.watch threw exception
1095
1115
* Eg. on linux the number of watches are limited and one could easily exhaust watches and the exception ENOSPC is thrown when creating watcher at that point
0 commit comments