Skip to content

Commit

Permalink
watch: watch for missing dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Nov 10, 2022
1 parent d55a7c3 commit 56ec098
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 20 deletions.
10 changes: 9 additions & 1 deletion lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
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, ArrayPrototypeConcat([''], exts));
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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()] });
63 changes: 53 additions & 10 deletions test/sequential/test-watch-mode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import { once } from 'node:events';
if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');

function deferred() {
let res;
const promise = new Promise((resolve) => res = resolve);
return { resolve: res, promise };
}

function restart(file) {
// To avoid flakiness, we save the file repeatedly until test is done
writeFileSync(file, readFileSync(file));
Expand Down Expand Up @@ -59,8 +65,8 @@ async function spawnWithRestarts({
}

let tmpFiles = 0;
function createTmpFile(content = 'console.log("running");') {
const file = path.join(tmpdir.path, `${tmpFiles++}.js`);
function createTmpFile(content = 'console.log("running");', ext = '.js') {
const file = path.join(tmpdir.path, `${tmpFiles++}${ext}`);
writeFileSync(file, content);
return file;
}
Expand All @@ -74,6 +80,25 @@ function assertRestartedCorrectly({ stdout, messages: { inner, completed, restar
assert.deepStrictEqual(lines.slice(-end.length), end);
}

async function failWriteSucceed({ file, watchedFile }) {
let stdout = '';
const notFound = deferred();
const completed = deferred();
const child = spawn(execPath, ['--watch', '--no-warnings', file], { encoding: 'utf8' });

child.stdout.on('data', (data) => {
stdout += data;
if (data.toString().startsWith('Failed running')) notFound.resolve();
if (data.toString().startsWith('Completed running')) completed.resolve();
});

await notFound.promise;
writeFileSync(watchedFile, 'console.log("test has ran");');
await completed.promise;
child.kill();
assert.match(stdout, /test has ran/);
}

tmpdir.refresh();

// Warning: this suite can run safely with concurrency: true
Expand Down Expand Up @@ -104,14 +129,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 Expand Up @@ -220,4 +237,30 @@ describe('watch mode', { concurrency: true, timeout: 60_0000 }, () => {
messages: { restarted: `Restarting ${inspect(file)}`, completed: `Completed running ${inspect(file)}` },
});
});

it('should not watch when running an missing file', async () => {
const nonExistingfile = path.join(tmpdir.path, `${tmpFiles++}.js`);
await failWriteSucceed({ file: nonExistingfile, watchedFile: nonExistingfile });
});

it('should not watch when running an missing mjs file', async () => {
const nonExistingfile = path.join(tmpdir.path, `${tmpFiles++}.mjs`);
await failWriteSucceed({ file: nonExistingfile, watchedFile: nonExistingfile });
});

it('should watch changes to previously missing dependency', async () => {
const dependency = path.join(tmpdir.path, `${tmpFiles++}.js`);
const relativeDependencyPath = `./${path.basename(dependency)}`;
const dependant = createTmpFile(`console.log(require('${relativeDependencyPath}'))`);

await failWriteSucceed({ file: dependant, watchedFile: dependency });
});

it('should watch changes to previously missing ESM dependency', async () => {
const dependency = path.join(tmpdir.path, `${tmpFiles++}.mjs`);
const relativeDependencyPath = `./${path.basename(dependency)}`;
const dependant = createTmpFile(`import '${relativeDependencyPath}'`, '.mjs');

await failWriteSucceed({ file: dependant, watchedFile: dependency });
});
});

0 comments on commit 56ec098

Please sign in to comment.