Skip to content

Commit 387b84e

Browse files
committed
Add to test harness
1 parent 52da3cc commit 387b84e

File tree

4 files changed

+38
-62
lines changed

4 files changed

+38
-62
lines changed

src/compiler/sys.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,10 +1191,13 @@ export function createSystemWatchFunctions({
11911191
return watchPresentFileSystemEntryWithFsWatchFile();
11921192
}
11931193
try {
1194-
const fsWatchCallback = inodeWatching ? callbackChangingToMissingFileSystemEntry : callback;
1195-
const presentWatcher = !fsWatchWithTimestamp
1196-
? fsWatchWorker(fileOrDirectory, recursive, fsWatchCallback)
1197-
: fsWatchWorkerHandlingTimestamp(fileOrDirectory, entryKind, recursive, fsWatchCallback);
1194+
const presentWatcher = (!fsWatchWithTimestamp ? fsWatchWorker : fsWatchWorkerHandlingTimestamp)(
1195+
fileOrDirectory,
1196+
recursive,
1197+
inodeWatching ?
1198+
callbackChangingToMissingFileSystemEntry :
1199+
callback,
1200+
);
11981201
// Watch the missing file or directory or error
11991202
presentWatcher.on("error", () => {
12001203
callback("rename", "");
@@ -1286,23 +1289,15 @@ export function createSystemWatchFunctions({
12861289
}
12871290
}
12881291

1289-
function fsWatchWorkerHandlingTimestamp(
1290-
fileOrDirectory: string,
1291-
entryKind: FileSystemEntryKind,
1292-
recursive: boolean,
1293-
callback: FsWatchCallback,
1294-
): FsWatchWorkerWatcher {
1295-
const initialModifiedTime = getModifiedTime(fileOrDirectory) ?? missingFileModifiedTime;
1296-
const modifiedTime: Record<string, Date | undefined> = {};
1292+
function fsWatchWorkerHandlingTimestamp(fileOrDirectory: string, recursive: boolean, callback: FsWatchCallback): FsWatchWorkerWatcher {
1293+
let modifiedTime = getModifiedTime(fileOrDirectory) || missingFileModifiedTime;
12971294
return fsWatchWorker(fileOrDirectory, recursive, (eventName, relativeFileName, currentModifiedTime) => {
1298-
const filename = entryKind === FileSystemEntryKind.File
1299-
? fileOrDirectory
1300-
: (relativeFileName ? combinePaths(fileOrDirectory, relativeFileName) : fileOrDirectory);
1301-
currentModifiedTime ??= getModifiedTime(filename) ?? missingFileModifiedTime;
1302-
const prevModifiedTime = modifiedTime[filename] ?? initialModifiedTime;
1303-
if (eventName === "change" && currentModifiedTime.getTime() === prevModifiedTime?.getTime()) return;
1304-
modifiedTime[filename] = currentModifiedTime;
1305-
callback(eventName, relativeFileName, currentModifiedTime);
1295+
if (eventName === "change") {
1296+
currentModifiedTime ||= getModifiedTime(fileOrDirectory) || missingFileModifiedTime;
1297+
if (currentModifiedTime.getTime() === modifiedTime.getTime()) return;
1298+
}
1299+
modifiedTime = currentModifiedTime || getModifiedTime(fileOrDirectory) || missingFileModifiedTime;
1300+
callback(eventName, relativeFileName, modifiedTime);
13061301
});
13071302
}
13081303
}

src/testRunner/unittests/helpers/virtualFileSystemWithWatch.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ export interface WatchInvokeOptions {
294294
skipInodeCheckOnCreate: boolean;
295295
/** When invoking rename event on fs watch, send event with file name suffixed with tilde */
296296
useTildeAsSuffixInRenameEventFileName: boolean;
297+
/** Simulate an fsevent that doesn't come with the modified time */
298+
omitModifiedTime: boolean;
299+
/** Simulate directory mtime not changing on file modification, like macOS Sonoma (GH#57792) */
300+
unmodifiedDirectoryMtime: boolean;
297301
}
298302

299303
export enum Tsc_WatchFile {
@@ -501,15 +505,18 @@ export class TestServerHost implements server.ServerHost, FormatDiagnosticsHost,
501505
else {
502506
currentEntry.content = content;
503507
currentEntry.modifiedTime = this.now();
504-
this.fs.get(getDirectoryPath(currentEntry.path))!.modifiedTime = this.now();
505-
if (options && options.invokeDirectoryWatcherInsteadOfFileChanged) {
508+
const mtime = options?.omitModifiedTime ? undefined : currentEntry.modifiedTime;
509+
if (!options?.unmodifiedDirectoryMtime) {
510+
this.fs.get(getDirectoryPath(currentEntry.path))!.modifiedTime = this.now();
511+
}
512+
if (options?.invokeDirectoryWatcherInsteadOfFileChanged) {
506513
const directoryFullPath = getDirectoryPath(currentEntry.fullPath);
507-
this.invokeFileWatcher(directoryFullPath, FileWatcherEventKind.Changed, currentEntry.modifiedTime);
508-
this.invokeFsWatchesCallbacks(directoryFullPath, "rename", currentEntry.modifiedTime, currentEntry.fullPath, options.useTildeAsSuffixInRenameEventFileName);
509-
this.invokeRecursiveFsWatches(directoryFullPath, "rename", currentEntry.modifiedTime, currentEntry.fullPath, options.useTildeAsSuffixInRenameEventFileName);
514+
this.invokeFileWatcher(directoryFullPath, FileWatcherEventKind.Changed, mtime);
515+
this.invokeFsWatchesCallbacks(directoryFullPath, "rename", mtime, currentEntry.fullPath, options.useTildeAsSuffixInRenameEventFileName);
516+
this.invokeRecursiveFsWatches(directoryFullPath, "rename", mtime, currentEntry.fullPath, options.useTildeAsSuffixInRenameEventFileName);
510517
}
511518
else {
512-
this.invokeFileAndFsWatches(currentEntry.fullPath, FileWatcherEventKind.Changed, currentEntry.modifiedTime, options?.useTildeAsSuffixInRenameEventFileName);
519+
this.invokeFileAndFsWatches(currentEntry.fullPath, FileWatcherEventKind.Changed, mtime, options?.useTildeAsSuffixInRenameEventFileName);
513520
}
514521
}
515522
}

src/testRunner/unittests/tscWatch/watchEnvironment.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ describe("unittests:: tsc-watch:: watchEnvironment:: tsc-watch with different po
725725
verify(/*fsWatchWithTimestamp*/ false);
726726
});
727727

728-
describe("with fsWatch with fsWatchWithTimestamp with useFsEventsOnParentDirectory", () => {
728+
describe("with fsWatch with fsWatchWithTimestamp with useFsEventsOnParentDirectory (GH#57877)", () => {
729729
verifyTscWatch({
730730
scenario,
731731
subScenario: "fsWatch/fsWatchWithTimestamp/useFsEventsOnParentDirectory",
@@ -743,14 +743,13 @@ describe("unittests:: tsc-watch:: watchEnvironment:: tsc-watch with different po
743743
},
744744
),
745745
edits: [
746-
{
747-
caption: "emulate access",
748-
edit: sys => sys.invokeFsWatches("/user/username/projects/myproject/main.ts", "change", /*modifiedTime*/ undefined, /*useTildeSuffix*/ undefined),
749-
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
750-
},
751746
{
752747
caption: "modify file contents",
753-
edit: sys => sys.appendFile("/user/username/projects/myproject/main.ts", "export const y = 10;"),
748+
edit: sys => sys.appendFile(
749+
"/user/username/projects/myproject/main.ts",
750+
"export const y = 10;",
751+
{ omitModifiedTime: true, unmodifiedDirectoryMtime: true },
752+
),
754753
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
755754
},
756755
],

tests/baselines/reference/tscWatch/watchEnvironment/fsWatch/fsWatchWithTimestamp/useFsEventsOnParentDirectory.js

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,6 @@ Shape signatures in builder refreshed for::
8585
/a/lib/lib.d.ts (used version)
8686
/user/username/projects/myproject/main.ts (used version)
8787

88-
exitCode:: ExitStatus.undefined
89-
90-
Change:: emulate access
91-
92-
Input::
93-
94-
Output::
95-
FileWatcher:: Triggered with /user/username/projects/myproject/main.ts 1:: WatchInfo: /user/username/projects/myproject/main.ts 250 {"watchFile":5} Source file
96-
Scheduling update
97-
Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/main.ts 1:: WatchInfo: /user/username/projects/myproject/main.ts 250 {"watchFile":5} Source file
98-
99-
100-
Timeout callback:: count: 1
101-
1: timerToUpdateProgram *new*
102-
103-
Before running Timeout callback:: count: 1
104-
1: timerToUpdateProgram
105-
106-
After running Timeout callback:: count: 0
107-
Output::
108-
Synchronizing program
109-
110-
111-
112-
11388
exitCode:: ExitStatus.undefined
11489

11590
Change:: modify file contents
@@ -126,20 +101,20 @@ Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/mai
126101

127102

128103
Timeout callback:: count: 1
129-
2: timerToUpdateProgram *new*
104+
1: timerToUpdateProgram *new*
130105

131106
Before running Timeout callback:: count: 1
132-
2: timerToUpdateProgram
107+
1: timerToUpdateProgram
133108

134109
After running Timeout callback:: count: 0
135110
Output::
136111
Synchronizing program
137-
[[90m12:00:27 AM[0m] File change detected. Starting incremental compilation...
112+
[[90m12:00:26 AM[0m] File change detected. Starting incremental compilation...
138113

139114
CreatingProgramWith::
140115
roots: ["/user/username/projects/myproject/main.ts"]
141116
options: {"watch":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
142-
[[90m12:00:31 AM[0m] Found 0 errors. Watching for file changes.
117+
[[90m12:00:30 AM[0m] Found 0 errors. Watching for file changes.
143118

144119

145120

0 commit comments

Comments
 (0)