watch: track worker entry files in watch mode · nodejs/node@0e2adb3 · GitHub
Skip to content

Commit 0e2adb3

Browse files
SudhansuBandhaaduh95
authored andcommitted
watch: track worker entry files in watch mode
Currently, --watch mode only tracks dependencies from the main module graph (require/import). Worker thread entry points created via new Worker() are not included, so changes to worker files do not trigger restarts. This change hooks into Worker initialization and registers the worker entry file with watch mode, ensuring restarts when worker files change. Fixes: #62275 Signed-off-by: SudhansuBandha <sudhansu9.vssut@gmail.com> PR-URL: #62368 Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 14a4cb8 commit 0e2adb3

4 files changed

Lines changed: 336 additions & 1 deletion

File tree

lib/internal/modules/cjs/loader.js

Lines changed: 24 additions & 0 deletions

lib/internal/modules/esm/loader.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,16 @@ class ModuleLoader {
515515
const type = requestType === kRequireInImportedCJS ? 'require' : 'import';
516516
process.send({ [`watch:${type}`]: [url] });
517517
}
518+
// Relay Events from worker to main thread
519+
if (process.env.WATCH_REPORT_DEPENDENCIES && !process.send) {
520+
const { isMainThread } = internalBinding('worker');
521+
if (!isMainThread) {
522+
const { parentPort } = require('worker_threads');
523+
if (parentPort) {
524+
parentPort.postMessage({ 'watch:import': [url] });
525+
}
526+
}
527+
}
518528

519529
// TODO(joyeecheung): update the module requests to use importAttributes as property names.
520530
const importAttributes = resolveResult.importAttributes ?? request.attributes;

lib/internal/worker.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
ArrayIsArray,
45
ArrayPrototypeForEach,
56
ArrayPrototypeMap,
67
ArrayPrototypePush,
@@ -329,9 +330,28 @@ class Worker extends EventEmitter {
329330

330331
this[kPublicPort] = publicPortToParent;
331332
ArrayPrototypeForEach(['message', 'messageerror'], (event) => {
332-
this[kPublicPort].on(event, (message) => this.emit(event, message));
333+
this[kPublicPort].on(event, (message) => {
334+
// Extract watch messages first if needed and relay events from worker thread to watcher
335+
if (
336+
event === 'message' &&
337+
process.env.WATCH_REPORT_DEPENDENCIES &&
338+
process.send
339+
) {
340+
const { isMainThread } = internalBinding('worker');
341+
if (isMainThread) {
342+
if (ArrayIsArray(message?.['watch:require'])) {
343+
process.send({ 'watch:require': message['watch:require'] });
344+
}
345+
if (ArrayIsArray(message?.['watch:import'])) {
346+
process.send({ 'watch:import': message['watch:import'] });
347+
}
348+
}
349+
}
350+
this.emit(event, message);
351+
});
333352
});
334353
setupPortReferencing(this[kPublicPort], this, 'message');
354+
335355
this[kPort].postMessage({
336356
argv,
337357
type: messageTypes.LOAD_SCRIPT,
Lines changed: 281 additions & 0 deletions

0 commit comments

Comments
 (0)