Skip to content

Commit 5dfdbdb

Browse files
committed
On rename event for the file, replace file watcher irrespective of file presence
1 parent 189bbc2 commit 5dfdbdb

8 files changed

+78
-29
lines changed

src/compiler/sys.ts

+13-16
Original file line numberDiff line numberDiff line change
@@ -1096,17 +1096,10 @@ namespace ts {
10961096
}
10971097
};
10981098

1099-
/**
1100-
* Invoke the callback with rename and update the watcher if not closed
1101-
* @param createWatcher
1102-
*/
1103-
function invokeCallbackAndUpdateWatcher(createWatcher: () => FileWatcher) {
1104-
sysLog(`sysLog:: ${fileOrDirectory}:: Changing watcher to ${createWatcher === watchPresentFileSystemEntry ? "Present" : "Missing"}FileSystemEntryWatcher`);
1105-
// Call the callback for current directory
1106-
callback("rename", "");
1107-
1099+
function updateWatcher(createWatcher: () => FileWatcher) {
11081100
// If watcher is not closed, update it
11091101
if (watcher) {
1102+
sysLog(`sysLog:: ${fileOrDirectory}:: Changing watcher to ${createWatcher === watchPresentFileSystemEntry ? "Present" : "Missing"}FileSystemEntryWatcher`);
11101103
watcher.close();
11111104
watcher = createWatcher();
11121105
}
@@ -1130,7 +1123,10 @@ namespace ts {
11301123
callback
11311124
);
11321125
// Watch the missing file or directory or error
1133-
presentWatcher.on("error", () => invokeCallbackAndUpdateWatcher(watchMissingFileSystemEntry));
1126+
presentWatcher.on("error", () => {
1127+
callback("rename", "");
1128+
updateWatcher(watchMissingFileSystemEntry);
1129+
});
11341130
return presentWatcher;
11351131
}
11361132
catch (e) {
@@ -1144,15 +1140,15 @@ namespace ts {
11441140
}
11451141

11461142
function callbackChangingToMissingFileSystemEntry(event: "rename" | "change", relativeName: string | undefined) {
1143+
callback(event, relativeName);
11471144
// because relativeName is not guaranteed to be correct we need to check on each rename with few combinations
11481145
// Eg on ubuntu while watching app/node_modules the relativeName is "node_modules" which is neither relative nor full path
1149-
return event === "rename" &&
1146+
if (event === "rename" &&
11501147
(!relativeName ||
11511148
relativeName === lastDirectoryPart ||
1152-
(relativeName.lastIndexOf(lastDirectoryPartWithDirectorySeparator!) !== -1 && relativeName.lastIndexOf(lastDirectoryPartWithDirectorySeparator!) === relativeName.length - lastDirectoryPartWithDirectorySeparator!.length)) &&
1153-
!fileSystemEntryExists(fileOrDirectory, entryKind) ?
1154-
invokeCallbackAndUpdateWatcher(watchMissingFileSystemEntry) :
1155-
callback(event, relativeName);
1149+
(relativeName.lastIndexOf(lastDirectoryPartWithDirectorySeparator!) !== -1 && relativeName.lastIndexOf(lastDirectoryPartWithDirectorySeparator!) === relativeName.length - lastDirectoryPartWithDirectorySeparator!.length))) {
1150+
updateWatcher(!fileSystemEntryExists(fileOrDirectory, entryKind) ? watchMissingFileSystemEntry : watchPresentFileSystemEntry);
1151+
}
11561152
}
11571153

11581154
/**
@@ -1177,10 +1173,11 @@ namespace ts {
11771173
fileOrDirectory,
11781174
(_fileName, eventKind) => {
11791175
if (eventKind === FileWatcherEventKind.Created && fileSystemEntryExists(fileOrDirectory, entryKind)) {
1176+
callback("rename", "");
11801177
// Call the callback for current file or directory
11811178
// For now it could be callback for the inner directory creation,
11821179
// but just return current directory, better than current no-op
1183-
invokeCallbackAndUpdateWatcher(watchPresentFileSystemEntry);
1180+
updateWatcher(watchPresentFileSystemEntry);
11841181
}
11851182
},
11861183
fallbackPollingInterval,

src/testRunner/unittests/tscWatch/watchEnvironment.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ namespace ts.tscWatch {
647647
{
648648
caption: "Replace file with rename event that fixes error",
649649
change: sys => sys.modifyFile(`${projectRoot}/foo.ts`, `export declare function foo(): string;`, { invokeFileDeleteCreateAsPartInsteadOfChange: true, }),
650-
timeouts: sys => sys.checkTimeoutQueueLengthAndRun(0),
650+
timeouts: sys => sys.checkTimeoutQueueLengthAndRun(1),
651651
},
652652
]
653653
});

tests/baselines/reference/tscWatch/resolutionCache/when-types-in-compiler-option-are-global-and-installed-at-later-point.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ declare namespace myapp {
8787

8888

8989
Output::
90-
sysLog:: /user/username/projects/myproject/node_modules:: Changing watcher to PresentFileSystemEntryWatcher
90+
sysLog:: /user/username/projects/myproject/node_modules:: Changing watcher to PresentFileSystemEntryWatcher
91+
9192
>> Screen clear
9293
[12:00:39 AM] File change detected. Starting incremental compilation...
9394

tests/baselines/reference/tscWatch/resolutionCache/works-when-module-resolution-changes-to-ambient-module.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ declare module "fs" {
8787

8888

8989
Output::
90-
sysLog:: /a/b/node_modules:: Changing watcher to PresentFileSystemEntryWatchersysLog:: /a/b/node_modules/@types:: Changing watcher to PresentFileSystemEntryWatcher
90+
sysLog:: /a/b/node_modules:: Changing watcher to PresentFileSystemEntryWatcher
91+
sysLog:: /a/b/node_modules/@types:: Changing watcher to PresentFileSystemEntryWatcher
92+
9193
>> Screen clear
9294
[12:00:27 AM] File change detected. Starting incremental compilation...
9395

tests/baselines/reference/tscWatch/resolutionCache/works-when-renaming-node_modules-folder-that-already-contains-@types-folder.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ export {}
7979
//// [/user/username/projects/myproject/node_modules2/@types/qqq/index.d.ts] deleted
8080

8181
Output::
82-
sysLog:: /user/username/projects/myproject/node_modules:: Changing watcher to PresentFileSystemEntryWatchersysLog:: /user/username/projects/myproject/node_modules/@types:: Changing watcher to PresentFileSystemEntryWatcher
82+
sysLog:: /user/username/projects/myproject/node_modules:: Changing watcher to PresentFileSystemEntryWatcher
83+
sysLog:: /user/username/projects/myproject/node_modules/@types:: Changing watcher to PresentFileSystemEntryWatcher
84+
8385
>> Screen clear
8486
[12:00:34 AM] File change detected. Starting incremental compilation...
8587

tests/baselines/reference/tscWatch/watchEnvironment/fsWatch/when-using-file-watching-thats-on-inode-when-rename-occurs-when-file-is-still-on-the-disk.js

+44-4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Output::
101101
FileWatcher:: Triggered with /user/username/projects/myproject/foo.ts 0:: WatchInfo: /user/username/projects/myproject/foo.ts 250 {"watchFile":4} Source file
102102
Scheduling update
103103
Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/foo.ts 0:: WatchInfo: /user/username/projects/myproject/foo.ts 250 {"watchFile":4} Source file
104+
sysLog:: /user/username/projects/myproject/foo.ts:: Changing watcher to PresentFileSystemEntryWatcher
104105
Synchronizing program
105106
[12:00:32 AM] File change detected. Starting incremental compilation...
106107

@@ -144,12 +145,12 @@ WatchedFiles::
144145
FsWatches::
145146
/user/username/projects/myproject/tsconfig.json:
146147
{"directoryName":"/user/username/projects/myproject/tsconfig.json","inode":10}
147-
/user/username/projects/myproject/foo.ts:
148-
{"directoryName":"/user/username/projects/myproject/foo.ts","inode":9}
149148
/user/username/projects/myproject/main.ts:
150149
{"directoryName":"/user/username/projects/myproject/main.ts","inode":8}
151150
/a/lib/lib.d.ts:
152151
{"directoryName":"/a/lib/lib.d.ts","inode":3}
152+
/user/username/projects/myproject/foo.ts:
153+
{"directoryName":"/user/username/projects/myproject/foo.ts","inode":13}
153154

154155
FsWatchesRecursive::
155156

@@ -166,6 +167,43 @@ export declare function foo(): string;
166167

167168

168169
Output::
170+
FileWatcher:: Triggered with /user/username/projects/myproject/foo.ts 2:: WatchInfo: /user/username/projects/myproject/foo.ts 250 {"watchFile":4} Source file
171+
Scheduling update
172+
Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/foo.ts 2:: WatchInfo: /user/username/projects/myproject/foo.ts 250 {"watchFile":4} Source file
173+
sysLog:: /user/username/projects/myproject/foo.ts:: Changing watcher to MissingFileSystemEntryWatcher
174+
FileWatcher:: Triggered with /user/username/projects/myproject/foo.ts 0:: WatchInfo: /user/username/projects/myproject/foo.ts 250 {"watchFile":4} Source file
175+
Scheduling update
176+
Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/foo.ts 0:: WatchInfo: /user/username/projects/myproject/foo.ts 250 {"watchFile":4} Source file
177+
sysLog:: /user/username/projects/myproject/foo.ts:: Changing watcher to PresentFileSystemEntryWatcher
178+
FileWatcher:: Triggered with /user/username/projects/myproject/foo.ts 0:: WatchInfo: /user/username/projects/myproject/foo.ts 250 {"watchFile":4} Source file
179+
Scheduling update
180+
Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/foo.ts 0:: WatchInfo: /user/username/projects/myproject/foo.ts 250 {"watchFile":4} Source file
181+
sysLog:: /user/username/projects/myproject/foo.ts:: Changing watcher to PresentFileSystemEntryWatcher
182+
Synchronizing program
183+
[12:00:43 AM] File change detected. Starting incremental compilation...
184+
185+
CreatingProgramWith::
186+
roots: ["/user/username/projects/myproject/foo.ts","/user/username/projects/myproject/main.ts"]
187+
options: {"watch":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
188+
[12:00:50 AM] Found 0 errors. Watching for file changes.
189+
190+
191+
192+
Program root files: ["/user/username/projects/myproject/foo.ts","/user/username/projects/myproject/main.ts"]
193+
Program options: {"watch":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
194+
Program structureReused: SafeModules
195+
Program files::
196+
/a/lib/lib.d.ts
197+
/user/username/projects/myproject/foo.ts
198+
/user/username/projects/myproject/main.ts
199+
200+
Semantic diagnostics in builder refreshed for::
201+
/user/username/projects/myproject/foo.ts
202+
/user/username/projects/myproject/main.ts
203+
204+
Shape signatures in builder refreshed for::
205+
/user/username/projects/myproject/foo.ts (computed .d.ts)
206+
/user/username/projects/myproject/main.ts (computed .d.ts)
169207

170208
WatchedFiles::
171209
/user/username/projects/myproject/node_modules/@types:
@@ -174,14 +212,16 @@ WatchedFiles::
174212
FsWatches::
175213
/user/username/projects/myproject/tsconfig.json:
176214
{"directoryName":"/user/username/projects/myproject/tsconfig.json","inode":10}
177-
/user/username/projects/myproject/foo.ts:
178-
{"directoryName":"/user/username/projects/myproject/foo.ts","inode":9}
179215
/user/username/projects/myproject/main.ts:
180216
{"directoryName":"/user/username/projects/myproject/main.ts","inode":8}
181217
/a/lib/lib.d.ts:
182218
{"directoryName":"/a/lib/lib.d.ts","inode":3}
219+
/user/username/projects/myproject/foo.ts:
220+
{"directoryName":"/user/username/projects/myproject/foo.ts","inode":14}
183221

184222
FsWatchesRecursive::
185223

186224
exitCode:: ExitStatus.undefined
187225

226+
//// [/user/username/projects/myproject/foo.js] file written with same contents Inode:: 11
227+
//// [/user/username/projects/myproject/main.js] file written with same contents Inode:: 12

tests/baselines/reference/tscWatch/watchEnvironment/fsWatch/when-using-file-watching-thats-on-inode.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,21 @@ export function foo2(): string;
9999

100100

101101
Output::
102-
sysLog:: /user/username/projects/myproject/foo.d.ts:: Changing watcher to MissingFileSystemEntryWatcherFileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 2:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
102+
FileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 2:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
103103
Scheduling update
104104
Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 2:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
105+
sysLog:: /user/username/projects/myproject/foo.d.ts:: Changing watcher to MissingFileSystemEntryWatcher
105106
DirectoryWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts :: WatchInfo: /user/username/projects/myproject 0 {"watchFile":4} Failed Lookup Locations
106107
Scheduling invalidateFailedLookup
107108
Elapsed:: *ms DirectoryWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts :: WatchInfo: /user/username/projects/myproject 0 {"watchFile":4} Failed Lookup Locations
108-
sysLog:: /user/username/projects/myproject/foo.d.ts:: Changing watcher to PresentFileSystemEntryWatcherFileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 0:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
109+
FileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 0:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
109110
Scheduling update
110111
Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 0:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
112+
sysLog:: /user/username/projects/myproject/foo.d.ts:: Changing watcher to PresentFileSystemEntryWatcher
111113
FileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 0:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
112114
Scheduling update
113115
Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 0:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
116+
sysLog:: /user/username/projects/myproject/foo.d.ts:: Changing watcher to PresentFileSystemEntryWatcher
114117
DirectoryWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts :: WatchInfo: /user/username/projects/myproject 0 {"watchFile":4} Failed Lookup Locations
115118
Scheduling invalidateFailedLookup, Cancelled earlier one
116119
Elapsed:: *ms DirectoryWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts :: WatchInfo: /user/username/projects/myproject 0 {"watchFile":4} Failed Lookup Locations
@@ -180,18 +183,21 @@ export function foo(): string;
180183

181184

182185
Output::
183-
sysLog:: /user/username/projects/myproject/foo.d.ts:: Changing watcher to MissingFileSystemEntryWatcherFileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 2:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
186+
FileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 2:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
184187
Scheduling update
185188
Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 2:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
189+
sysLog:: /user/username/projects/myproject/foo.d.ts:: Changing watcher to MissingFileSystemEntryWatcher
186190
DirectoryWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts :: WatchInfo: /user/username/projects/myproject 0 {"watchFile":4} Failed Lookup Locations
187191
Scheduling invalidateFailedLookup
188192
Elapsed:: *ms DirectoryWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts :: WatchInfo: /user/username/projects/myproject 0 {"watchFile":4} Failed Lookup Locations
189-
sysLog:: /user/username/projects/myproject/foo.d.ts:: Changing watcher to PresentFileSystemEntryWatcherFileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 0:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
193+
FileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 0:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
190194
Scheduling update
191195
Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 0:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
196+
sysLog:: /user/username/projects/myproject/foo.d.ts:: Changing watcher to PresentFileSystemEntryWatcher
192197
FileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 0:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
193198
Scheduling update
194199
Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts 0:: WatchInfo: /user/username/projects/myproject/foo.d.ts 250 {"watchFile":4} Source file
200+
sysLog:: /user/username/projects/myproject/foo.d.ts:: Changing watcher to PresentFileSystemEntryWatcher
195201
DirectoryWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts :: WatchInfo: /user/username/projects/myproject 0 {"watchFile":4} Failed Lookup Locations
196202
Scheduling invalidateFailedLookup, Cancelled earlier one
197203
Elapsed:: *ms DirectoryWatcher:: Triggered with /user/username/projects/myproject/foo.d.ts :: WatchInfo: /user/username/projects/myproject 0 {"watchFile":4} Failed Lookup Locations

tests/baselines/reference/tscWatch/watchEnvironment/watchOptions/with-fallbackPolling-option.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ Output::
2929

3030
[12:00:22 AM] Found 0 errors. Watching for file changes.
3131

32-
sysLog:: /a/b:: Changing to watchFile
32+
sysLog:: /a/b:: Changing to watchFile
33+
3334

3435
Program root files: ["/a/b/commonFile1.ts","/a/b/commonFile2.ts"]
3536
Program options: {"watch":true,"project":"/a/b/tsconfig.json","configFilePath":"/a/b/tsconfig.json"}

0 commit comments

Comments
 (0)