From bec4b73839f4b15ced8939ad46bc0d3ccbb8f8c7 Mon Sep 17 00:00:00 2001 From: Aaron Date: Tue, 14 Apr 2026 09:33:36 -0500 Subject: [PATCH] fix: Apply expanded env to `process.env` (#2267) --- .gitignore | 1 + .tool-versions | 1 + bun.lock | 2 +- .../wxt/src/core/utils/__tests__/env.test.ts | 42 +++++++++++++++++++ .../src/core/utils/__tests__/fixtures/.env | 2 + packages/wxt/src/core/utils/env.ts | 9 +--- 6 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 packages/wxt/src/core/utils/__tests__/env.test.ts create mode 100644 packages/wxt/src/core/utils/__tests__/fixtures/.env diff --git a/.gitignore b/.gitignore index 709b1f22..422ab5c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store .env .env.* +!packages/wxt/src/core/utils/__tests__/fixtures/.env .idea .output .webextrc diff --git a/.tool-versions b/.tool-versions index 461bdea7..066a07c5 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1,2 @@ bun 1.3.12 +nodejs 24.14.1 diff --git a/bun.lock b/bun.lock index 59926ba1..32b305cb 100644 --- a/bun.lock +++ b/bun.lock @@ -91,7 +91,7 @@ "@types/har-format": "*", }, "devDependencies": { - "@types/chrome": "^0.1.40", + "@types/chrome": "0.1.40", "@types/node": "^20.0.0", "nano-spawn": "^2.0.0", "typescript": "^5.9.3", diff --git a/packages/wxt/src/core/utils/__tests__/env.test.ts b/packages/wxt/src/core/utils/__tests__/env.test.ts new file mode 100644 index 00000000..21cac381 --- /dev/null +++ b/packages/wxt/src/core/utils/__tests__/env.test.ts @@ -0,0 +1,42 @@ +import { beforeEach, describe, expect, it } from 'vitest'; +import { loadEnv } from '../env'; + +const cwd = process.cwd(); + +describe('Env Utils', () => { + beforeEach(() => { + if (process.cwd() !== cwd) process.chdir(cwd); + delete process.env.TEST_VAR; + delete process.env.EXPANDED; + }); + + describe('loadEnv', () => { + beforeEach(() => { + process.chdir(`${import.meta.dirname}/fixtures`); + }); + + it('should load env vars into the real `process.env`', () => { + loadEnv('testing', 'chrome'); + expect(process.env.TEST_VAR).toEqual('expected'); + }); + + it('should override blank strings in process.env', () => { + process.env.TEST_VAR = ''; + loadEnv('testing', 'chrome'); + expect(process.env.TEST_VAR).toEqual('expected'); + }); + + it('should not override non-blank strings in process.env', () => { + process.env.TEST_VAR = 'non-blank'; + loadEnv('testing', 'chrome'); + expect(process.env.TEST_VAR).toEqual('non-blank'); + }); + + // Node doesn't return vars in the same order as they're defined: + // https://github.com/nodejs/node/issues/62736 + it.skip('should expand env vars into the real `process.env`', () => { + loadEnv('testing', 'chrome'); + expect(process.env.EXPANDED).toEqual('expected expanded'); + }); + }); +}); diff --git a/packages/wxt/src/core/utils/__tests__/fixtures/.env b/packages/wxt/src/core/utils/__tests__/fixtures/.env new file mode 100644 index 00000000..a6fd32b1 --- /dev/null +++ b/packages/wxt/src/core/utils/__tests__/fixtures/.env @@ -0,0 +1,2 @@ +TEST_VAR="expected" +EXPANDED="$TEST_VAR expanded" diff --git a/packages/wxt/src/core/utils/env.ts b/packages/wxt/src/core/utils/env.ts index 38871ca5..17bb8eb2 100644 --- a/packages/wxt/src/core/utils/env.ts +++ b/packages/wxt/src/core/utils/env.ts @@ -1,6 +1,6 @@ -import { readFileSync, existsSync } from 'node:fs'; -import { parseEnv } from 'node:util'; import { expand } from 'dotenv-expand'; +import { existsSync, readFileSync } from 'node:fs'; +import { parseEnv } from 'node:util'; import type { TargetBrowser } from '../../types'; /** Load environment files based on the current mode and browser. */ @@ -32,13 +32,8 @@ export function loadEnv(mode: string, browser: TargetBrowser) { }), ); - // Make a copy of `process.env` so that `dotenv-expand` doesn't re-assign the - // expanded values to the global `process.env`. - const processEnv = { ...process.env } as Record; - expand({ parsed, - processEnv, }); return parsed;