Skip to content

Commit

Permalink
Handle change event with same time stamp on mac os
Browse files Browse the repository at this point in the history
Fixes #52876
  • Loading branch information
sheetalkamat committed Nov 15, 2023
1 parent 455b356 commit 4671cce
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
22 changes: 19 additions & 3 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ export interface CreateSystemWatchFunctions {
useNonPollingWatchers?: boolean;
tscWatchDirectory: string | undefined;
inodeWatching: boolean;
fsWatchWithTimestamp: boolean | undefined;
sysLog: (s: string) => void;
}

Expand All @@ -934,6 +935,7 @@ export function createSystemWatchFunctions({
useNonPollingWatchers,
tscWatchDirectory,
inodeWatching,
fsWatchWithTimestamp,
sysLog,
}: CreateSystemWatchFunctions): { watchFile: HostWatchFile; watchDirectory: HostWatchDirectory; } {
const pollingWatches = new Map<string, SingleFileWatcher<FileWatcherCallback>>();
Expand Down Expand Up @@ -1189,7 +1191,7 @@ export function createSystemWatchFunctions({
return watchPresentFileSystemEntryWithFsWatchFile();
}
try {
const presentWatcher = fsWatchWorker(
const presentWatcher = (!fsWatchWithTimestamp ? fsWatchWorker : fsWatchWorkerHandlingTimestamp)(
fileOrDirectory,
recursive,
inodeWatching ?
Expand Down Expand Up @@ -1286,6 +1288,18 @@ export function createSystemWatchFunctions({
);
}
}

function fsWatchWorkerHandlingTimestamp(fileOrDirectory: string, recursive: boolean, callback: FsWatchCallback): FsWatchWorkerWatcher {
let modifiedTime = getModifiedTime(fileOrDirectory) || missingFileModifiedTime;
return fsWatchWorker(fileOrDirectory, recursive, (eventName, relativeFileName, currentModifiedTime) => {
if (eventName === "change") {
currentModifiedTime ||= getModifiedTime(fileOrDirectory) || missingFileModifiedTime;
if (currentModifiedTime.getTime() === modifiedTime.getTime()) return;
}
modifiedTime = currentModifiedTime || getModifiedTime(fileOrDirectory) || missingFileModifiedTime;
callback(eventName, relativeFileName, modifiedTime);
});
}
}

/**
Expand Down Expand Up @@ -1482,7 +1496,8 @@ export let sys: System = (() => {
from?(input: string, encoding?: string): any;
} = require("buffer").Buffer;

const isLinuxOrMacOs = process.platform === "linux" || process.platform === "darwin";
const isMacOs = process.platform === "darwin";
const isLinuxOrMacOs = process.platform === "linux" || isMacOs;

const platform: string = _os.platform();
const useCaseSensitiveFileNames = isFileSystemCaseSensitive();
Expand All @@ -1495,7 +1510,7 @@ export let sys: System = (() => {
// Note that if we ever emit as files like cjs/mjs, this check will be wrong.
const executingFilePath = __filename.endsWith("sys.js") ? _path.join(_path.dirname(__dirname), "__fake__.js") : __filename;

const fsSupportsRecursiveFsWatch = process.platform === "win32" || process.platform === "darwin";
const fsSupportsRecursiveFsWatch = process.platform === "win32" || isMacOs;
const getCurrentDirectory = memoize(() => process.cwd());
const { watchFile, watchDirectory } = createSystemWatchFunctions({
pollingWatchFileWorker: fsWatchFileWorker,
Expand All @@ -1515,6 +1530,7 @@ export let sys: System = (() => {
useNonPollingWatchers: !!process.env.TSC_NONPOLLING_WATCHER,
tscWatchDirectory: process.env.TSC_WATCHDIRECTORY,
inodeWatching: isLinuxOrMacOs,
fsWatchWithTimestamp: isMacOs,
sysLog,
});
const nodeSystem: System = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export interface TestServerHostCreationParameters {
runWithoutRecursiveWatches?: boolean;
runWithFallbackPolling?: boolean;
inodeWatching?: boolean;
fsWatchWithTimestamp?: boolean;
}

export function createWatchedSystem(fileOrFolderList: FileOrFolderOrSymLinkMap | readonly FileOrFolderOrSymLink[], params?: TestServerHostCreationParameters): TestServerHost {
Expand Down Expand Up @@ -377,6 +378,7 @@ export class TestServerHost implements server.ServerHost, FormatDiagnosticsHost,
runWithoutRecursiveWatches,
runWithFallbackPolling,
inodeWatching,
fsWatchWithTimestamp,
}: TestServerHostCreationParameters = {},
) {
this.useCaseSensitiveFileNames = !!useCaseSensitiveFileNames;
Expand Down Expand Up @@ -414,6 +416,7 @@ export class TestServerHost implements server.ServerHost, FormatDiagnosticsHost,
tscWatchFile,
tscWatchDirectory,
inodeWatching: !!this.inodeWatching,
fsWatchWithTimestamp,
sysLog: s => this.write(s + this.newLine),
});
this.watchFile = watchFile;
Expand Down
1 change: 1 addition & 0 deletions src/testRunner/unittests/tscWatch/watchEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ describe("unittests:: tsc-watch:: watchEnvironment:: tsc-watch with different po
},
{
currentDirectory: "/user/username/projects/myproject",
fsWatchWithTimestamp,
},
),
edits: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,9 @@ Change:: emulate access

Input::

Output::
FileWatcher:: Triggered with /user/username/projects/myproject/main.ts 1:: WatchInfo: /user/username/projects/myproject/main.ts 250 undefined Source file
Scheduling update
Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/main.ts 1:: WatchInfo: /user/username/projects/myproject/main.ts 250 undefined Source file


Timeout callback:: count: 1
1: timerToUpdateProgram *new*

Before running Timeout callback:: count: 1
1: timerToUpdateProgram
Before running Timeout callback:: count: 0

After running Timeout callback:: count: 0
Output::
Synchronizing program




exitCode:: ExitStatus.undefined
Expand All @@ -128,10 +114,10 @@ Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/mai


Timeout callback:: count: 1
2: timerToUpdateProgram *new*
1: timerToUpdateProgram *new*

Before running Timeout callback:: count: 1
2: timerToUpdateProgram
1: timerToUpdateProgram

After running Timeout callback:: count: 0
Output::
Expand Down

0 comments on commit 4671cce

Please sign in to comment.