Skip to content

Commit ad19172

Browse files
watch: track worker thread dependencies in --watch mode for cjs files
1 parent fa01219 commit ad19172

3 files changed

Lines changed: 34 additions & 19 deletions

File tree

lib/internal/modules/cjs/loader.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,28 @@ function reportModuleNotFoundToWatchMode(basePath, extensions) {
329329
}
330330
}
331331

332+
/**
333+
* Tell the watch mode that a module was required, from within a worker thread.
334+
* @param {string} filename Absolute path of the module
335+
* @returns {void}
336+
*/
337+
function reportModuleToWatchModeFromWorker(filename) {
338+
if (!shouldReportRequiredModules()) {
339+
return;
340+
}
341+
const { isMainThread } = internalBinding('worker');
342+
if (isMainThread) {
343+
return;
344+
}
345+
// Lazy require to avoid circular dependency: worker_threads is loaded after
346+
// the CJS loader is fully set up.
347+
const { parentPort } = require('worker_threads');
348+
if (!parentPort) {
349+
return;
350+
}
351+
parentPort.postMessage({ 'watch:require': [filename] });
352+
}
353+
332354
/**
333355
* Create a new module instance.
334356
* @param {string} id
@@ -1245,6 +1267,7 @@ Module._load = function(request, parent, isMain, internalResolveOptions = kEmpty
12451267
relResolveCacheIdentifier = `${parent.path}\x00${request}`;
12461268
const filename = relativeResolveCache[relResolveCacheIdentifier];
12471269
reportModuleToWatchMode(filename);
1270+
reportModuleToWatchModeFromWorker(filename);
12481271
if (filename !== undefined) {
12491272
const cachedModule = Module._cache[filename];
12501273
if (cachedModule !== undefined) {
@@ -1335,6 +1358,7 @@ Module._load = function(request, parent, isMain, internalResolveOptions = kEmpty
13351358
}
13361359

13371360
reportModuleToWatchMode(filename);
1361+
reportModuleToWatchModeFromWorker(filename);
13381362
Module._cache[filename] = module;
13391363
module[kIsCachedByESMLoader] = false;
13401364
// If there are resolve hooks, carry the context information into the

lib/internal/watch_mode/files_watcher.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,6 @@ class FilesWatcher extends EventEmitter {
181181
if (ArrayIsArray(message['watch:import'])) {
182182
ArrayPrototypeForEach(message['watch:import'], (file) => this.filterFile(fileURLToPath(file), key));
183183
}
184-
if (ArrayIsArray(message['watch:worker'])) {
185-
ArrayPrototypeForEach(message['watch:worker'], (file) => this.filterFile(file, key));
186-
}
187184
} catch {
188185
// Failed watching file. ignore
189186
}

lib/internal/worker.js

Lines changed: 10 additions & 16 deletions
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,
@@ -201,17 +202,6 @@ class HeapProfileHandle {
201202
}
202203
}
203204

204-
/**
205-
* Tell the watch mode that a worker file was instantiated.
206-
* @param {string} filename Absolute path of the worker file
207-
* @returns {void}
208-
*/
209-
function reportWorkerToWatchMode(filename) {
210-
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
211-
process.send({ 'watch:worker': [filename] });
212-
}
213-
}
214-
215205
class Worker extends EventEmitter {
216206
constructor(filename, options = kEmptyObject) {
217207
throwIfBuildingSnapshot('Creating workers');
@@ -292,11 +282,6 @@ class Worker extends EventEmitter {
292282
name = StringPrototypeTrim(options.name);
293283
}
294284

295-
// Report to watch mode if this is a regular file (not eval, internal, or data URL)
296-
if (!isInternal && doEval === false) {
297-
reportWorkerToWatchMode(filename);
298-
}
299-
300285
debug('instantiating Worker.', `url: ${url}`, `doEval: ${doEval}`);
301286
// Set up the C++ handle for the worker, as well as some internal wiring.
302287
this[kHandle] = new WorkerImpl(url,
@@ -355,6 +340,15 @@ class Worker extends EventEmitter {
355340
this[kPublicPort].on(event, (message) => this.emit(event, message));
356341
});
357342
setupPortReferencing(this[kPublicPort], this, 'message');
343+
344+
// relay events from worker thread to watcher
345+
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
346+
this[kPublicPort].on('message', (message) => {
347+
if (ArrayIsArray(message?.['watch:require'])) {
348+
process.send({ 'watch:require': message['watch:require'] });
349+
}
350+
});
351+
}
358352
this[kPort].postMessage({
359353
argv,
360354
type: messageTypes.LOAD_SCRIPT,

0 commit comments

Comments
 (0)