Skip to content

Commit d64795b

Browse files
marco-ippolitoaduh95
authored andcommitted
watch: fix interaction with multiple env files
PR-URL: #60605 Fixes: #60599 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Stefan Stojanovic <stefan.stojanovic@janeasystems.com> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 727560a commit d64795b

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

lib/internal/main/watch_mode.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ markBootstrapComplete();
3434

3535
const kKillSignal = convertToValidSignal(getOptionValue('--watch-kill-signal'));
3636
const kShouldFilterModules = getOptionValue('--watch-path').length === 0;
37-
const kEnvFile = getOptionValue('--env-file') || getOptionValue('--env-file-if-exists');
37+
const kEnvFiles = [
38+
...getOptionValue('--env-file'),
39+
...getOptionValue('--env-file-if-exists'),
40+
];
3841
const kWatchedPaths = ArrayPrototypeMap(getOptionValue('--watch-path'), (path) => resolve(path));
3942
const kPreserveOutput = getOptionValue('--watch-preserve-output');
4043
const kCommand = ArrayPrototypeSlice(process.argv, 1);
@@ -100,8 +103,8 @@ function start() {
100103
},
101104
});
102105
watcher.watchChildProcessModules(child);
103-
if (kEnvFile) {
104-
watcher.filterFile(resolve(kEnvFile));
106+
if (kEnvFiles.length > 0) {
107+
ArrayPrototypeForEach(kEnvFiles, (file) => watcher.filterFile(resolve(file)));
105108
}
106109
child.once('exit', (code) => {
107110
exited = true;

src/node_options.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ class EnvironmentOptions : public Options {
185185
#endif // HAVE_INSPECTOR
186186
std::string redirect_warnings;
187187
std::string diagnostic_dir;
188-
std::string env_file;
189-
std::string optional_env_file;
188+
std::vector<std::string> env_file;
189+
std::vector<std::string> optional_env_file;
190190
bool has_env_file_string = false;
191191
bool test_runner = false;
192192
uint64_t test_runner_concurrency = 0;

test/parallel/test-dotenv-edge-cases.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ describe('.env supports edge cases', () => {
3939
})));
4040
});
4141

42+
it('should not support comma-separated env files', async () => {
43+
const code = 'assert.strictEqual(1, 1)';
44+
const child = await common.spawnPromisified(
45+
process.execPath,
46+
[`--env-file=${validEnvFilePath},${nodeOptionsEnvFilePath}`, '--eval', code],
47+
{ cwd: __dirname },
48+
);
49+
assert.notStrictEqual(child.stderr, '');
50+
assert.strictEqual(child.code, 9);
51+
});
52+
4253
it('supports absolute paths', async () => {
4354
const code = `
4455
assert.strictEqual(process.env.BASIC, 'basic');

test/sequential/test-watch-mode.mjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,4 +860,28 @@ process.on('message', (message) => {
860860
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
861861
]);
862862
});
863+
864+
it('should support multiple --env-file flags', async () => {
865+
const envKey = `TEST_ENV_A_${Date.now()}`;
866+
const envKey2 = `TEST_ENV_B_${Date.now()}`;
867+
const jsFile = createTmpFile(`console.log('ENV_A: ' + process.env.${envKey} + '\\n' + 'ENV_B: ' + process.env.${envKey2});`);
868+
const envFileA = createTmpFile(`${envKey}=123`, '.env');
869+
const envFileB = createTmpFile(`${envKey2}=456`, '.env');
870+
const { done, restart } = runInBackground({
871+
args: ['--watch', `--env-file=${envFileA}`, `--env-file=${envFileB}`, jsFile]
872+
});
873+
874+
try {
875+
const { stderr, stdout } = await restart();
876+
877+
assert.strictEqual(stderr, '');
878+
assert.deepStrictEqual(stdout, [
879+
'ENV_A: 123',
880+
'ENV_B: 456',
881+
`Completed running ${inspect(jsFile)}. Waiting for file changes before restarting...`,
882+
]);
883+
} finally {
884+
await done();
885+
}
886+
});
863887
});

0 commit comments

Comments
 (0)