From 6a5b702ee044ee6503f78669fe290a99e6d020cc Mon Sep 17 00:00:00 2001 From: HBSPS Date: Thu, 25 Jul 2024 15:59:50 +0900 Subject: [PATCH 1/3] watch: watch env files in `--watch` mode observe env files in `--watch` mode through the watcher's filterFile and forward changes to the childProcess Fixes: https://github.com/nodejs/node/issues/54001 --- lib/internal/main/watch_mode.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/internal/main/watch_mode.js b/lib/internal/main/watch_mode.js index d70908e6e3cd9b..33fd2fb3e1b93b 100644 --- a/lib/internal/main/watch_mode.js +++ b/lib/internal/main/watch_mode.js @@ -7,6 +7,7 @@ const { ArrayPrototypePushApply, ArrayPrototypeSlice, StringPrototypeStartsWith, + StringPrototypeSplit, } = primordials; const { @@ -22,10 +23,11 @@ const { FilesWatcher } = require('internal/watch_mode/files_watcher'); const { green, blue, red, white, clear } = require('internal/util/colors'); const { spawn } = require('child_process'); -const { inspect } = require('util'); +const { inspect, parseEnv } = require('util'); const { setTimeout, clearTimeout } = require('timers'); const { resolve } = require('path'); const { once } = require('events'); +const { readFileSync } = require('fs'); prepareMainThreadExecution(false, false); markBootstrapComplete(); @@ -64,11 +66,25 @@ let exited; function start() { exited = false; + + let kEnv = {}; + for (let i = 0; i < process.execArgv.length; i++) { + const arg = process.execArgv[i]; + if (StringPrototypeStartsWith(arg, '--env-file')) { + const [,envFileName] = StringPrototypeSplit(arg, '='); + const envFile = resolve(envFileName); + watcher.filterFile(envFile, null); + const envContent = parseEnv(readFileSync(envFile, 'utf8')); + kEnv = {...kEnv, ...envContent} + } + } + const stdio = kShouldFilterModules ? ['inherit', 'inherit', 'inherit', 'ipc'] : 'inherit'; child = spawn(process.execPath, argsWithoutWatchOptions, { stdio, env: { ...process.env, + ...kEnv, WATCH_REPORT_DEPENDENCIES: '1', }, }); From 45539cdd02e42bef0f3839f36cce8c7b2d32a5c1 Mon Sep 17 00:00:00 2001 From: HBSPS Date: Thu, 25 Jul 2024 23:21:46 +0900 Subject: [PATCH 2/3] fix: fix for loop to use `for...of` instead of index --- lib/internal/main/watch_mode.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/internal/main/watch_mode.js b/lib/internal/main/watch_mode.js index 33fd2fb3e1b93b..713a469a8818a4 100644 --- a/lib/internal/main/watch_mode.js +++ b/lib/internal/main/watch_mode.js @@ -68,8 +68,7 @@ function start() { exited = false; let kEnv = {}; - for (let i = 0; i < process.execArgv.length; i++) { - const arg = process.execArgv[i]; + for (const arg of process.execArgv) { if (StringPrototypeStartsWith(arg, '--env-file')) { const [,envFileName] = StringPrototypeSplit(arg, '='); const envFile = resolve(envFileName); From b14e1d4ec62ebf5903a7f482dd196939c7df5331 Mon Sep 17 00:00:00 2001 From: HBSPS Date: Thu, 25 Jul 2024 23:37:46 +0900 Subject: [PATCH 3/3] fix: apply lint --- lib/internal/main/watch_mode.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/internal/main/watch_mode.js b/lib/internal/main/watch_mode.js index 713a469a8818a4..4a2482f854d74c 100644 --- a/lib/internal/main/watch_mode.js +++ b/lib/internal/main/watch_mode.js @@ -6,8 +6,8 @@ const { ArrayPrototypePush, ArrayPrototypePushApply, ArrayPrototypeSlice, - StringPrototypeStartsWith, StringPrototypeSplit, + StringPrototypeStartsWith, } = primordials; const { @@ -70,11 +70,10 @@ function start() { let kEnv = {}; for (const arg of process.execArgv) { if (StringPrototypeStartsWith(arg, '--env-file')) { - const [,envFileName] = StringPrototypeSplit(arg, '='); - const envFile = resolve(envFileName); + const envFile = resolve(StringPrototypeSplit(arg, '=')[1]); watcher.filterFile(envFile, null); const envContent = parseEnv(readFileSync(envFile, 'utf8')); - kEnv = {...kEnv, ...envContent} + kEnv = { ...kEnv, ...envContent }; } }