Skip to content

Commit f22bff9

Browse files
committed
fix(@angular/build): ensure chokidar watcher is ready before returning
When initializing chokidar.watch with ignoreInitial: true, files visited during the initial scan are treated as the initial baseline and do not emit change events. If createChokidarWatcher returns before the initial scan completes, subsequent file modifications made shortly after setup can be visited for the first time during the initial scan, causing the change event to be dropped. Awaiting the ready event ensures the watcher's initial scan is completed and the watcher is actively listening before returning. This resolves test flakiness in rebuild error detection tests where files are modified immediately after the initial build.
1 parent 8c43889 commit f22bff9

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

‎packages/angular/build/src/builders/dev-server/tests/behavior/build-errors_spec.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
3838
expectNoLog(logs, 'Unexpected character "EOF"');
3939
},
4040
],
41-
{ outputLogsOnFailure: false, timeout: 90_000 },
41+
{ outputLogsOnFailure: false },
4242
);
43-
}, 120_000);
43+
});
4444
});
4545
});

‎packages/angular/build/src/tools/esbuild/watcher.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import type * as ParcelWatcher from '@parcel/watcher';
1010
import type * as Chokidar from 'chokidar';
11+
import { once } from 'node:events';
1112
import * as fs from 'node:fs';
1213
import * as path from 'node:path';
1314
import picomatch from 'picomatch';
@@ -663,6 +664,13 @@ async function createChokidarWatcher(
663664
usePolling: !!options?.polling,
664665
interval: options?.interval,
665666
});
667+
668+
// Wait for the watcher to complete its initial filesystem scan before returning.
669+
// With `ignoreInitial: true`, any file visited during the initial scan is treated as the initial baseline
670+
// and will not emit 'add' or 'change' events. Awaiting 'ready' ensures that rapid file modifications
671+
// made right after watcher setup (e.g. in rebuild tests) are not swallowed as initial files.
672+
await once(watcher, 'ready');
673+
666674
const initTime = Date.now();
667675

668676
const handleEvent = (type: 'added' | 'modified' | 'removed', rawPath: string) => {

0 commit comments

Comments
 (0)