fix: Apply expanded env to process.env (#2267)

This commit is contained in:
Aaron
2026-04-14 09:33:36 -05:00
committed by GitHub
parent e67bac0868
commit bec4b73839
6 changed files with 49 additions and 8 deletions
+1
View File
@@ -1,6 +1,7 @@
.DS_Store
.env
.env.*
!packages/wxt/src/core/utils/__tests__/fixtures/.env
.idea
.output
.webextrc
+1
View File
@@ -1 +1,2 @@
bun 1.3.12
nodejs 24.14.1
Generated
+1 -1
View File
@@ -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",
@@ -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');
});
});
});
@@ -0,0 +1,2 @@
TEST_VAR="expected"
EXPANDED="$TEST_VAR expanded"
+2 -7
View File
@@ -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<string, string>;
expand({
parsed,
processEnv,
});
return parsed;