Skip to content

Commit e1b0f44

Browse files
JonasBaaduh95
authored andcommitted
watch: reload env file for --env-file-if-exists
PR-URL: #56643 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 36b02bf commit e1b0f44

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/internal/main/watch_mode.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ markBootstrapComplete();
3333
// TODO(MoLow): Make kill signal configurable
3434
const kKillSignal = 'SIGTERM';
3535
const kShouldFilterModules = getOptionValue('--watch-path').length === 0;
36-
const kEnvFile = getOptionValue('--env-file');
36+
const kEnvFile = getOptionValue('--env-file') || getOptionValue('--env-file-if-exists');
3737
const kWatchedPaths = ArrayPrototypeMap(getOptionValue('--watch-path'), (path) => resolve(path));
3838
const kPreserveOutput = getOptionValue('--watch-preserve-output');
3939
const kCommand = ArrayPrototypeSlice(process.argv, 1);

test/sequential/test-watch-mode.mjs

+26
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,32 @@ describe('watch mode', { concurrency: !process.env.TEST_PARALLEL, timeout: 60_00
242242
}
243243
});
244244

245+
it('should load new env variables when --env-file-if-exists changes', async () => {
246+
const envKey = `TEST_ENV_${Date.now()}`;
247+
const envKey2 = `TEST_ENV_2_${Date.now()}`;
248+
const jsFile = createTmpFile(`console.log('ENV: ' + process.env.${envKey} + '\\n' + 'ENV2: ' + process.env.${envKey2});`);
249+
const envFile = createTmpFile(`${envKey}=value1`, '.env');
250+
const { done, restart } = runInBackground({ args: ['--watch', `--env-file-if-exists=${envFile}`, jsFile] });
251+
252+
try {
253+
await restart();
254+
writeFileSync(envFile, `${envKey}=value1\n${envKey2}=newValue`);
255+
256+
// Second restart, after env change
257+
const { stderr, stdout } = await restart();
258+
259+
assert.strictEqual(stderr, '');
260+
assert.deepStrictEqual(stdout, [
261+
`Restarting ${inspect(jsFile)}`,
262+
'ENV: value1',
263+
'ENV2: newValue',
264+
`Completed running ${inspect(jsFile)}`,
265+
]);
266+
} finally {
267+
await done();
268+
}
269+
});
270+
245271
it('should watch changes to a failing file', async () => {
246272
const file = createTmpFile('throw new Error("fails");');
247273
const { stderr, stdout } = await runWriteSucceed({

0 commit comments

Comments
 (0)