Skip to content

Commit a2caf19

Browse files
marco-ippolitotargos
authored andcommitted
watch: fix interaction with multiple env files
PR-URL: #60605Fixes: #60599 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Stefan Stojanovic <[email protected]> Reviewed-By: Pietro Marchini <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent e7da5b4 commit a2caf19

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
constkKillSignal=convertToValidSignal(getOptionValue('--watch-kill-signal'));
3636
constkShouldFilterModules=getOptionValue('--watch-path').length===0;
37-
constkEnvFile=getOptionValue('--env-file')||getOptionValue('--env-file-if-exists');
37+
constkEnvFiles=[
38+
...getOptionValue('--env-file'),
39+
...getOptionValue('--env-file-if-exists'),
40+
];
3841
constkWatchedPaths=ArrayPrototypeMap(getOptionValue('--watch-path'),(path)=>resolve(path));
3942
constkPreserveOutput=getOptionValue('--watch-preserve-output');
4043
constkCommand=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
@@ -182,8 +182,8 @@ class EnvironmentOptions : public Options{
182182
#endif// HAVE_INSPECTOR
183183
std::string redirect_warnings;
184184
std::string diagnostic_dir;
185-
std::string env_file;
186-
std::string optional_env_file;
185+
std::vector<std::string> env_file;
186+
std::vector<std::string> optional_env_file;
187187
bool has_env_file_string = false;
188188
bool test_runner = false;
189189
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+
constcode='assert.strictEqual(1, 1)';
44+
constchild=awaitcommon.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
constcode=`
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+
constenvKey=`TEST_ENV_A_${Date.now()}`;
866+
constenvKey2=`TEST_ENV_B_${Date.now()}`;
867+
constjsFile=createTmpFile(`console.log('ENV_A: ' + process.env.${envKey} + '\\n' + 'ENV_B: ' + process.env.${envKey2});`);
868+
constenvFileA=createTmpFile(`${envKey}=123`,'.env');
869+
constenvFileB=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 }=awaitrestart();
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+
awaitdone();
885+
}
886+
});
863887
});

0 commit comments

Comments
(0)