Skip to content

Commit 6e0a587

Browse files
committed
watch: fix --env-file-if-exists crashing on linux if the file is missing
1 parent 4d1557a commit 6e0a587

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

lib/internal/main/watch_mode.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const { green, blue, red, white, clear } = require('internal/util/colors');
2424
const { convertToValidSignal } = require('internal/util');
2525

2626
const { spawn } = require('child_process');
27+
const { existsSync } = require('fs');
2728
const { inspect } = require('util');
2829
const { setTimeout, clearTimeout } = require('timers');
2930
const { resolve } = require('path');
@@ -34,10 +35,8 @@ markBootstrapComplete();
3435

3536
const kKillSignal = convertToValidSignal(getOptionValue('--watch-kill-signal'));
3637
const kShouldFilterModules = getOptionValue('--watch-path').length === 0;
37-
const kEnvFiles = [
38-
...getOptionValue('--env-file'),
39-
...getOptionValue('--env-file-if-exists'),
40-
];
38+
const kEnvFiles = getOptionValue('--env-file');
39+
const kOptionalEnvFiles = getOptionValue('--env-file-if-exists');
4140
const kWatchedPaths = ArrayPrototypeMap(getOptionValue('--watch-path'), (path) => resolve(path));
4241
const kPreserveOutput = getOptionValue('--watch-preserve-output');
4342
const kCommand = ArrayPrototypeSlice(process.argv, 1);
@@ -105,6 +104,14 @@ function start() {
105104
if (kEnvFiles.length > 0) {
106105
ArrayPrototypeForEach(kEnvFiles, (file) => watcher.filterFile(resolve(file)));
107106
}
107+
if (kOptionalEnvFiles.length > 0) {
108+
ArrayPrototypeForEach(kOptionalEnvFiles, (file) => {
109+
const resolvedPath = resolve(file);
110+
if (existsSync(resolvedPath)) {
111+
watcher.filterFile(resolvedPath);
112+
}
113+
});
114+
}
108115
child.once('exit', (code) => {
109116
exited = true;
110117
const waitingForChanges = 'Waiting for file changes before restarting...';

test/sequential/test-watch-mode.mjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,27 @@ describe('watch mode', { concurrency: !process.env.TEST_PARALLEL, timeout: 60_00
277277
}
278278
});
279279

280+
it('should not crash when --env-file-if-exists points to a missing file', async () => {
281+
const envKey = `TEST_ENV_${Date.now()}`;
282+
const jsFile = createTmpFile(`console.log('ENV: ' + process.env.${envKey});`);
283+
const missingEnvFile = path.join(tmpdir.path, `missing-${Date.now()}.env`);
284+
const { done, restart } = runInBackground({
285+
args: ['--watch-path', tmpdir.path, `--env-file-if-exists=${missingEnvFile}`, jsFile],
286+
});
287+
288+
try {
289+
const { stderr, stdout } = await restart();
290+
291+
assert.doesNotMatch(stderr, /ENOENT: no such file or directory, watch/);
292+
assert.deepStrictEqual(stdout, [
293+
'ENV: undefined',
294+
`Completed running ${inspect(jsFile)}. Waiting for file changes before restarting...`,
295+
]);
296+
} finally {
297+
await done();
298+
}
299+
});
300+
280301
it('should watch changes to a failing file', async () => {
281302
const file = createTmpFile('throw new Error("fails");');
282303
const { stderr, stdout } = await runWriteSucceed({

0 commit comments

Comments
 (0)