From d8ab4cff4b5f017b0c1300d5f556a1733a079b5f Mon Sep 17 00:00:00 2001 From: productdevbook Date: Sat, 3 Oct 2026 19:09:53 +0200 Subject: [PATCH] fix(dev): let a restarted fork reload changed `.env` values Editing `.env` restarts Nuxt in a new process, but the fork inherited the parent's `process.env`, including the values the old `.env` had put there. c12 only overwrites variables it set itself and keeps that record per process, so in the fork the stale values looked like shell variables and won: a changed value was never picked up, and a removed one stayed. Drop the variables c12 set from `.env` before forking, so the fork reads the files afresh. Variables from the shell are untouched. Co-Authored-By: Claude Opus 5.5 --- packages/nuxt-cli/src/dev/pool.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/nuxt-cli/src/dev/pool.ts b/packages/nuxt-cli/src/dev/pool.ts index de1df23a8..1e1660472 100644 --- a/packages/nuxt-cli/src/dev/pool.ts +++ b/packages/nuxt-cli/src/dev/pool.ts @@ -130,7 +130,7 @@ export class ForkPool { execArgv: ['--enable-source-maps'], stdio: pipeOutput ? ['ignore', 'pipe', 'pipe', 'ipc'] : undefined, env: { - ...process.env, + ...withoutDotenvVars(process.env), __NUXT__FORK: 'true', ...pipeOutput ? { @@ -250,6 +250,19 @@ function trackServing(child: ChildProcess): Promise { }) } +/** + * The environment without the variables `.env` files set in this process, so a + * fork reads them afresh. c12 only overwrites variables it set itself, and its + * record of those does not survive into a new process. + */ +function withoutDotenvVars(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv { + const dotenvVars = (globalThis as { __c12_dotenv_vars__?: Map> }).__c12_dotenv_vars__?.get(env) + if (!dotenvVars?.size) { + return env + } + return Object.fromEntries(Object.entries(env).filter(([key]) => !dotenvVars.has(key))) +} + /** Colour settings for a piped fork, which cannot detect colour support itself. */ function forcedColorEnv(): Record { if (process.env.NO_COLOR || process.env.FORCE_COLOR) {