Skip to content

Commit e293232

Browse files
committed
Change to missing file watcher on linux and darwin explicitly to avoid watching deleted inode
1 parent d757402 commit e293232

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/compiler/sys.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,7 @@ namespace ts {
730730

731731
const nodeVersion = getNodeMajorVersion();
732732
const isNode4OrLater = nodeVersion! >= 4;
733+
const isLinuxOrMacOs = process.platform === "linux" || process.platform === "darwin";
733734

734735
const platform: string = _os.platform();
735736
const useCaseSensitiveFileNames = isFileSystemCaseSensitive();
@@ -1029,6 +1030,12 @@ namespace ts {
10291030

10301031
function fsWatch(fileOrDirectory: string, entryKind: FileSystemEntryKind.File | FileSystemEntryKind.Directory, callback: FsWatchCallback, recursive: boolean, fallbackPollingWatchFile: HostWatchFile, pollingInterval?: number): FileWatcher {
10311032
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;
10321039
/** Watcher for the file system entry depending on whether it is missing or present */
10331040
let watcher = !fileSystemEntryExists(fileOrDirectory, entryKind) ?
10341041
watchMissingFileSystemEntry() :
@@ -1072,11 +1079,12 @@ namespace ts {
10721079
}
10731080
}
10741081
try {
1075-
10761082
const presentWatcher = _fs.watch(
10771083
fileOrDirectory,
10781084
options,
1079-
callback
1085+
isLinuxOrMacOs ?
1086+
callbackChangingToMissingFileSystemEntry :
1087+
callback
10801088
);
10811089
// Watch the missing file or directory or error
10821090
presentWatcher.on("error", () => invokeCallbackAndUpdateWatcher(watchMissingFileSystemEntry));
@@ -1090,6 +1098,18 @@ namespace ts {
10901098
}
10911099
}
10921100

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+
10931113
/**
10941114
* Watch the file or directory using fs.watchFile since fs.watch threw exception
10951115
* 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

Comments
 (0)