Skip to content

Commit 23d0e67

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 23d0e67

2 files changed

Lines changed: 12 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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,16 @@ async function createChokidarWatcher(
663663
usePolling: !!options?.polling,
664664
interval: options?.interval,
665665
});
666+
667+
// Wait for the watcher to complete its initial filesystem scan before returning.
668+
// With `ignoreInitial: true`, any file visited during the initial scan is treated as the initial baseline
669+
// and will not emit 'add' or 'change' events. Awaiting 'ready' ensures that rapid file modifications
670+
// made right after watcher setup (e.g. in rebuild tests) are not swallowed as initial files.
671+
await new Promise<void>((resolve, reject) => {
672+
watcher.once('ready', resolve);
673+
watcher.once('error', reject);
674+
});
675+
666676
const initTime = Date.now();
667677

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

0 commit comments

Comments
 (0)