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) {