Skip to content

Commit 6964f8a

Browse files
watch: fix interaction with multiple env files
1 parent e72761f commit 6964f8a

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ describe('.env supports edge cases', () => {
3939
})));
4040
});
4141

42+
it('should not support comma-separated env files', async () => {
43+
const code = `
44+
assert.strictEqual(1, 1)
45+
`.trim();
46+
const child = await common.spawnPromisified(
47+
process.execPath,
48+
[ `--env-file=${validEnvFilePath},${nodeOptionsEnvFilePath}`, '--eval', code ],
49+
{ cwd: __dirname },
50+
);
51+
// Comma-separated paths are not supported, so this should fail
52+
// because it will try to load a file with a comma in its name
53+
assert.notStrictEqual(child.stderr, '');
54+
assert.strictEqual(child.code, 9);
55+
});
56+
4257
it('supports absolute paths', async () => {
4358
const code = `
4459
assert.strictEqual(process.env.BASIC, 'basic');

test/sequential/test-watch-mode.mjs

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

0 commit comments

Comments
 (0)