Skip to content

Commit bec4b73

Browse files
authored
fix: Apply expanded env to process.env (#2267)
1 parent e67bac0 commit bec4b73

6 files changed

Lines changed: 49 additions & 8 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
.env
33
.env.*
4+
!packages/wxt/src/core/utils/__tests__/fixtures/.env
45
.idea
56
.output
67
.webextrc

.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
bun 1.3.12
2+
nodejs 24.14.1

bun.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { beforeEach, describe, expect, it } from 'vitest';
2+
import { loadEnv } from '../env';
3+
4+
const cwd = process.cwd();
5+
6+
describe('Env Utils', () => {
7+
beforeEach(() => {
8+
if (process.cwd() !== cwd) process.chdir(cwd);
9+
delete process.env.TEST_VAR;
10+
delete process.env.EXPANDED;
11+
});
12+
13+
describe('loadEnv', () => {
14+
beforeEach(() => {
15+
process.chdir(`${import.meta.dirname}/fixtures`);
16+
});
17+
18+
it('should load env vars into the real `process.env`', () => {
19+
loadEnv('testing', 'chrome');
20+
expect(process.env.TEST_VAR).toEqual('expected');
21+
});
22+
23+
it('should override blank strings in process.env', () => {
24+
process.env.TEST_VAR = '';
25+
loadEnv('testing', 'chrome');
26+
expect(process.env.TEST_VAR).toEqual('expected');
27+
});
28+
29+
it('should not override non-blank strings in process.env', () => {
30+
process.env.TEST_VAR = 'non-blank';
31+
loadEnv('testing', 'chrome');
32+
expect(process.env.TEST_VAR).toEqual('non-blank');
33+
});
34+
35+
// Node doesn't return vars in the same order as they're defined:
36+
// https://github.com/nodejs/node/issues/62736
37+
it.skip('should expand env vars into the real `process.env`', () => {
38+
loadEnv('testing', 'chrome');
39+
expect(process.env.EXPANDED).toEqual('expected expanded');
40+
});
41+
});
42+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
TEST_VAR="expected"
2+
EXPANDED="$TEST_VAR expanded"

packages/wxt/src/core/utils/env.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { readFileSync, existsSync } from 'node:fs';
2-
import { parseEnv } from 'node:util';
31
import { expand } from 'dotenv-expand';
2+
import { existsSync, readFileSync } from 'node:fs';
3+
import { parseEnv } from 'node:util';
44
import type { TargetBrowser } from '../../types';
55

66
/** Load environment files based on the current mode and browser. */
@@ -32,13 +32,8 @@ export function loadEnv(mode: string, browser: TargetBrowser) {
3232
}),
3333
);
3434

35-
// Make a copy of `process.env` so that `dotenv-expand` doesn't re-assign the
36-
// expanded values to the global `process.env`.
37-
const processEnv = { ...process.env } as Record<string, string>;
38-
3935
expand({
4036
parsed,
41-
processEnv,
4237
});
4338

4439
return parsed;

0 commit comments

Comments
 (0)