Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/internal/main/watch_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ markBootstrapComplete();

const kKillSignal = convertToValidSignal(getOptionValue('--watch-kill-signal'));
const kShouldFilterModules = getOptionValue('--watch-path').length === 0;
const kEnvFile = getOptionValue('--env-file') || getOptionValue('--env-file-if-exists');
const kEnvFiles = [
...getOptionValue('--env-file'),
...getOptionValue('--env-file-if-exists'),
];
const kWatchedPaths = ArrayPrototypeMap(getOptionValue('--watch-path'), (path) => resolve(path));
const kPreserveOutput = getOptionValue('--watch-preserve-output');
const kCommand = ArrayPrototypeSlice(process.argv, 1);
Expand Down Expand Up @@ -100,8 +103,8 @@ function start() {
},
});
watcher.watchChildProcessModules(child);
if (kEnvFile) {
watcher.filterFile(resolve(kEnvFile));
if (kEnvFiles.length > 0) {
ArrayPrototypeForEach(kEnvFiles, (file) => watcher.filterFile(resolve(file)));
}
child.once('exit', (code) => {
exited = true;
Expand Down
4 changes: 2 additions & 2 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ class EnvironmentOptions : public Options {
#endif // HAVE_INSPECTOR
std::string redirect_warnings;
std::string diagnostic_dir;
std::string env_file;
std::string optional_env_file;
std::vector<std::string> env_file;
std::vector<std::string> optional_env_file;
bool has_env_file_string = false;
bool test_runner = false;
uint64_t test_runner_concurrency = 0;
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-dotenv-edge-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ describe('.env supports edge cases', () => {
})));
});

it('should not support comma-separated env files', async () => {
const code = 'assert.strictEqual(1, 1)';
const child = await common.spawnPromisified(
process.execPath,
[`--env-file=${validEnvFilePath},${nodeOptionsEnvFilePath}`, '--eval', code],
{ cwd: __dirname },
);
assert.notStrictEqual(child.stderr, '');
assert.strictEqual(child.code, 9);
});

it('supports absolute paths', async () => {
const code = `
assert.strictEqual(process.env.BASIC, 'basic');
Expand Down
24 changes: 24 additions & 0 deletions test/sequential/test-watch-mode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -860,4 +860,28 @@ process.on('message', (message) => {
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
]);
});

it('should support multiple --env-file flags', async () => {
const envKey = `TEST_ENV_A_${Date.now()}`;
const envKey2 = `TEST_ENV_B_${Date.now()}`;
const jsFile = createTmpFile(`console.log('ENV_A: ' + process.env.${envKey} + '\\n' + 'ENV_B: ' + process.env.${envKey2});`);
const envFileA = createTmpFile(`${envKey}=123`, '.env');
const envFileB = createTmpFile(`${envKey2}=456`, '.env');
const { done, restart } = runInBackground({
args: ['--watch', `--env-file=${envFileA}`, `--env-file=${envFileB}`, jsFile]
});

try {
const { stderr, stdout } = await restart();

assert.strictEqual(stderr, '');
assert.deepStrictEqual(stdout, [
'ENV_A: 123',
'ENV_B: 456',
`Completed running ${inspect(jsFile)}. Waiting for file changes before restarting...`,
]);
} finally {
await done();
}
});
});
Loading