Skip to content

Commit

Permalink
watch: watch for unexsiting dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Nov 6, 2022
1 parent 1f51713 commit d686854
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
10 changes: 9 additions & 1 deletion lib/internal/modules/cjs/loader.js
Expand Up @@ -28,6 +28,7 @@ const {
ArrayPrototypeIncludes,
ArrayPrototypeIndexOf,
ArrayPrototypeJoin,
ArrayPrototypeMap,
ArrayPrototypePush,
ArrayPrototypeSlice,
ArrayPrototypeSplice,
Expand Down Expand Up @@ -191,7 +192,13 @@ function updateChildren(parent, child, scan) {

function reportModuleToWatchMode(filename) {
if (shouldReportRequiredModules && process.send) {
process.send({ 'watch:require': filename });
process.send({ 'watch:require': [filename] });
}
}

function reportModuleNotFoundToWatchMode(basePath, extensions) {
if (shouldReportRequiredModules && process.send) {
process.send({ 'watch:require': ArrayPrototypeMap(extensions, (ext) => path.resolve(`${basePath}${ext}`)) });
}
}

Expand Down Expand Up @@ -648,6 +655,7 @@ Module._findPath = function(request, paths, isMain) {
Module._pathCache[cacheKey] = filename;
return filename;
}
reportModuleNotFoundToWatchMode(basePath, exts);
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/loader.js
Expand Up @@ -463,7 +463,7 @@ class ESMLoader {
);

if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
process.send({ 'watch:import': url });
process.send({ 'watch:import': [url] });
}

const job = new ModuleJob(
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/modules/esm/resolve.js
Expand Up @@ -254,6 +254,9 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
err.url = String(resolved);
throw err;
} else if (!stats.isFile()) {
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
process.send({ 'watch:require': [path || resolved.pathname] });
}
throw new ERR_MODULE_NOT_FOUND(
path || resolved.pathname, base && fileURLToPath(base), 'module');
}
Expand Down
11 changes: 7 additions & 4 deletions lib/internal/watch_mode/files_watcher.js
@@ -1,6 +1,8 @@
'use strict';

const {
ArrayIsArray,
ArrayPrototypeForEach,
SafeMap,
SafeSet,
StringPrototypeStartsWith,
Expand Down Expand Up @@ -94,6 +96,7 @@ class FilesWatcher extends EventEmitter {
}

filterFile(file) {
if (!file) return;
if (supportsRecursiveWatching) {
this.watchPath(dirname(file));
} else {
Expand All @@ -109,11 +112,11 @@ class FilesWatcher extends EventEmitter {
}
child.on('message', (message) => {
try {
if (message['watch:require']) {
this.filterFile(message['watch:require']);
if (ArrayIsArray(message['watch:require'])) {
ArrayPrototypeForEach(message['watch:require'], (file) => this.filterFile(file));
}
if (message['watch:import']) {
this.filterFile(fileURLToPath(message['watch:import']));
if (ArrayIsArray(message['watch:import'])) {
ArrayPrototypeForEach(message['watch:import'], (file) => this.filterFile(fileURLToPath(file)));
}
} catch {
// Failed watching file. ignore
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/watch-mode/ipc.js
Expand Up @@ -6,7 +6,7 @@ const tmpdir = require('../../common/tmpdir');
const tmpfile = path.join(tmpdir.path, 'file');
fs.writeFileSync(tmpfile, '');

process.send({ 'watch:require': path.resolve(__filename) });
process.send({ 'watch:import': url.pathToFileURL(path.resolve(__filename)).toString() });
process.send({ 'watch:import': url.pathToFileURL(tmpfile).toString() });
process.send({ 'watch:import': new URL('http://invalid.com').toString() });
process.send({ 'watch:require': [path.resolve(__filename)] });
process.send({ 'watch:import': [url.pathToFileURL(path.resolve(__filename)).toString()] });
process.send({ 'watch:import': [url.pathToFileURL(tmpfile).toString()] });
process.send({ 'watch:import': [new URL('http://invalid.com').toString()] });
8 changes: 0 additions & 8 deletions test/sequential/test-watch-mode.mjs
Expand Up @@ -104,14 +104,6 @@ describe('watch mode', { concurrency: true, timeout: 60_0000 }, () => {
});
});

it('should not watch when running an non-existing file', async () => {
const file = fixtures.path('watch-mode/non-existing.js');
const { stderr, stdout } = await spawnWithRestarts({ file, restarts: 0 });

assert.match(stderr, /code: 'MODULE_NOT_FOUND'/);
assert.strictEqual(stdout, `Failed running ${inspect(file)}\n`);
});

it('should watch when running an non-existing file - when specified under --watch-path', {
skip: !common.isOSX && !common.isWindows
}, async () => {
Expand Down

0 comments on commit d686854

Please sign in to comment.