Skip to content

Commit 301eb0f

Browse files
committed
fix(@angular/build): deduplicate spurious chokidar polling change events
When using Chokidar in polling mode, non-atomic file writes (such as open with O_TRUNC followed by write) can span across poll intervals and cause Chokidar to emit two 'change' events for a single file modification. If the initial rebuild completes quickly (e.g. in JIT mode), the trailing poll event can flush a second rebuild batch with the same file state. Track the last known mtimeMs and size of watched files in createChokidarWatcher and ignore 'modified' events when the file's mtimeMs and size have not changed since the previous queued or flushed event.
1 parent e023a8e commit 301eb0f

1 file changed

Lines changed: 49 additions & 10 deletions

File tree

‎packages/angular/build/src/utils/watcher.ts‎

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ async function createChokidarWatcher(
752752
): Promise<BuildWatcher> {
753753
const chokidar = chokidarModule ?? (await import('chokidar'));
754754
const watchedFiles = new Set<string>();
755+
const fileStats = new Map<string, { mtimeMs: number; size: number }>();
755756

756757
let queue: WatcherQueue;
757758
if (options?.polling) {
@@ -811,16 +812,36 @@ async function createChokidarWatcher(
811812
return;
812813
}
813814

814-
if (type !== 'removed') {
815-
const stat = fs.statSync(rawPath, { throwIfNoEntry: false });
816-
// Ignore historical events from before watcher initialization, but allow a 1000 ms window
817-
// to account for coarse filesystem timestamp resolution (e.g., ext4/overlayfs integer second
818-
// mtime truncation on Linux) where files modified during startup may have truncated .000 ms mtimes.
819-
if (stat && stat.mtimeMs < initTime - 1000) {
820-
return;
821-
}
815+
if (type === 'removed') {
816+
fileStats.delete(lookupKey);
817+
queue.addChange(type, rawPath);
818+
819+
return;
820+
}
821+
822+
const stat = fs.statSync(rawPath, { throwIfNoEntry: false });
823+
if (!stat) {
824+
return;
822825
}
823826

827+
// Ignore historical events from before watcher initialization, but allow a 1000 ms window
828+
// to account for coarse filesystem timestamp resolution (e.g., ext4/overlayfs integer second
829+
// mtime truncation on Linux) where files modified during startup may have truncated .000 ms mtimes.
830+
if (stat.mtimeMs < initTime - 1000) {
831+
return;
832+
}
833+
834+
const previousStat = fileStats.get(lookupKey);
835+
if (
836+
type === 'modified' &&
837+
previousStat &&
838+
previousStat.mtimeMs === stat.mtimeMs &&
839+
previousStat.size === stat.size
840+
) {
841+
return;
842+
}
843+
844+
fileStats.set(lookupKey, { mtimeMs: stat.mtimeMs, size: stat.size });
824845
queue.addChange(type, rawPath);
825846
};
826847

@@ -833,8 +854,20 @@ async function createChokidarWatcher(
833854
return this;
834855
},
835856

836-
next() {
837-
return queue.next();
857+
async next() {
858+
const result = await queue.next();
859+
if (result.value) {
860+
for (const file of [...result.value.added, ...result.value.modified]) {
861+
const stat = fs.statSync(file, { throwIfNoEntry: false });
862+
if (stat?.isFile()) {
863+
const posixPath = toPosixPathNormalized(file);
864+
const lookupKey = toLookupKey(posixPath, isCaseSensitive);
865+
fileStats.set(lookupKey, { mtimeMs: stat.mtimeMs, size: stat.size });
866+
}
867+
}
868+
}
869+
870+
return result;
838871
},
839872

840873
add(paths) {
@@ -846,6 +879,11 @@ async function createChokidarWatcher(
846879
if (!watchedFiles.has(lookupKey)) {
847880
watchedFiles.add(lookupKey);
848881

882+
const stat = fs.statSync(p, { throwIfNoEntry: false });
883+
if (stat?.isFile()) {
884+
fileStats.set(lookupKey, { mtimeMs: stat.mtimeMs, size: stat.size });
885+
}
886+
849887
// For files inside node_modules, register their package directory. Only newly encountered
850888
// package directories need to be added to Chokidar.
851889
const { isPackage, newPkgDir } = nodeModulesManager.registerPackage(posixPath, lookupKey);
@@ -873,6 +911,7 @@ async function createChokidarWatcher(
873911
const lookupKey = toLookupKey(posixPath, isCaseSensitive);
874912
if (watchedFiles.has(lookupKey)) {
875913
watchedFiles.delete(lookupKey);
914+
fileStats.delete(lookupKey);
876915

877916
// When the last watched file in a package is removed, unwatch the package directory.
878917
const { isPackage, unwatchPkgDir } = nodeModulesManager.removePackageFile(

0 commit comments

Comments
 (0)