Skip to content

Commit 65df9ad

Browse files
authored
watch: get flags from execArgv
PR-URL: #61779 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent 7215ea4 commit 65df9ad

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

lib/internal/main/watch_mode.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const {
1818
triggerUncaughtException,
1919
exitCodes: { kNoFailure },
2020
} = internalBinding('errors');
21-
const { getOptionValue, getOptionsAsFlagsFromBinding } = require('internal/options');
21+
const { getOptionValue } = require('internal/options');
2222
const { FilesWatcher } = require('internal/watch_mode/files_watcher');
2323
const { green, blue, red, white, clear } = require('internal/util/colors');
2424
const { convertToValidSignal } = require('internal/util');
@@ -44,14 +44,13 @@ const kCommand = ArrayPrototypeSlice(process.argv, 1);
4444
const kCommandStr = inspect(ArrayPrototypeJoin(kCommand, ' '));
4545

4646
const argsWithoutWatchOptions = [];
47-
const argsFromBinding = getOptionsAsFlagsFromBinding();
48-
for (let i = 0; i < argsFromBinding.length; i++) {
49-
const arg = argsFromBinding[i];
47+
for (let i = 0; i < process.execArgv.length; i++) {
48+
const arg = process.execArgv[i];
5049
if (StringPrototypeStartsWith(arg, '--watch=')) {
5150
continue;
5251
}
5352
if (arg === '--watch') {
54-
const nextArg = argsFromBinding[i + 1];
53+
const nextArg = process.execArgv[i + 1];
5554
if (nextArg && nextArg[0] !== '-') {
5655
// If `--watch` doesn't include `=` and the next
5756
// argument is not a flag then it is interpreted as

test/sequential/test-watch-mode.mjs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,4 +893,33 @@ process.on('message', (message) => {
893893
await done();
894894
}
895895
});
896+
897+
it('should respect the order for --env-file and --env-file-if-exists', async () => {
898+
const envKey = `TEST_ENV_${Date.now()}`;
899+
const jsFile = createTmpFile(`console.log('ENV: ' + process.env.${envKey});`);
900+
901+
const envFile = createTmpFile(`${envKey}=base`, '.env');
902+
const envFileIfExists = createTmpFile(`${envKey}=override`, '.env');
903+
904+
const { done, restart } = runInBackground({
905+
args: [
906+
'--watch',
907+
`--env-file=${envFile}`,
908+
`--env-file-if-exists=${envFileIfExists}`,
909+
jsFile,
910+
],
911+
});
912+
913+
try {
914+
const { stdout, stderr } = await restart();
915+
916+
assert.strictEqual(stderr, '');
917+
assert.deepStrictEqual(stdout, [
918+
'ENV: override',
919+
`Completed running ${inspect(jsFile)}. Waiting for file changes before restarting...`,
920+
]);
921+
} finally {
922+
await done();
923+
}
924+
});
896925
});

0 commit comments

Comments
 (0)