Repository navigation
Description
Describe the bug
We recently upgraded WXT, and the new .env variable processing hit us with a significant bug, breaking our application. We have automation that populates variables in the user's .env.local based upon the developer's deployment/testing environment, while we define general VITE_ variables in .env (committed in Git) which reference those values defined in .env.local. That was working great.
After upgrading to WXT 0.21.4 this approach breaks because the new WXT .env loading implementation doesn't allow "forward references" to values that are defined in .env files later in the inheritance hierarchy. It also introduces some artificial alphabetical ordering requirement even for references within the same file. Apparently some of this is known, because WXT tests that would catch this regression seem to be marked as ignored. I don't know if there is a bug filed for it though, so I'm filing this one.
Below is a full report after hours of deliberation with GPT 5.6 Astra, with drafting assisted by Claude Sonnet 5.5.
Overview
In 0.21.4, a ${VAR} reference in a .env file resolves only if the referenced variable has already been processed, a behavior introduced by the loader rewrite in 0.20.21 and 0.20.22. A reference to a variable that first appears in a more specific file (for example .env.local), or that sorts alphabetically after the referencing variable in the same file, resolves to an empty string. No warning is emitted, and the build contains the empty value. The documentation states that WXT supports dotenv files "the same way as Vite" (https://wxt.dev/guide/essentials/config/environment-variables.html), and Vite resolves both cases.
Background
WXT replaced the dotenv package with Node's util.parseEnv in 0.20.21 (#2181) and kept dotenv-expand for ${VAR} substitution. The PR description states that this was meant to keep variable expansion working. The old loader put every file's values into process.env before expanding, so a reference could point at any variable. The new loader first merges the files into one ordered map, and dotenv-expand only resolves references to variables it has already processed. Because parseEnv also returns each file's variables in alphabetical order, references that resolved before now resolve to empty strings.
Changes in WXT, in order:
- February 2025: fix: regression on variable expansion in dotenv files #1449 fixed an earlier regression in "variable expansion in dotenv files".
- Replace
dotenvwith builtins #2176 requested replacingdotenvwith Node builtins, citing Vite's use ofutil.parseEnvanddotenv-expand. An earlier PR with the same approach, chore(deps): replace dotenv with Node.js builtins #2185, was closed unmerged. - 0.20.21: feat: replace
dotenvwith nativenode:utilparseEnv#2181 replaceddotenvwithparseEnv. Its description states: "Kept dotenv-expand to ensure support for variable expansion (e.g., VAR=$OTHER_VAR) remains functional for users." - 0.20.22:
.envfile is no longer auto-imported #2266 reported that.envvalues were no longer applied toprocess.env. The fix, fix: Apply expanded env toprocess.env#2267, writes the expanded values toprocess.envand adds an expansion test markedit.skip, with the comment "Node doesn't return vars in the same order as they're defined". - 0.21.0:
dotenv-expand12 → 13 (chore(deps)!: Upgrade major dependencies #2508, described as "non-breaking for WXT's purposes"). - 0.21.4: the expansion test is still skipped.
Upstream behavior that applies:
dotenv-expand12 and later resolve references only to variables processed earlier. The maintainer states this is intentional (v12 does not support reverse order expansion dotenvx/dotenv-expand#134).util.parseEnvreturns keys in alphabetical order, not file order (parseEnvresult properties order does not match order in input string nodejs/node#62736, closed as not planned). A proposed fix, src: preserve dotenv key insertion order nodejs/node#62796, was closed unmerged.- Vite's bundled expander resolves references against the full parsed set.
| Version | Loader behavior |
|---|---|
| ≤ 0.20.20 | dotenv.config() loaded all files into process.env, then dotenv-expand ran |
| 0.20.21 | parseEnv per file, merged general → specific, then dotenv-expand (#2181) |
| 0.20.22 | Expanded values are written to process.env (#2267) |
| 0.21.x | dotenv-expand 13 (#2508); expansion test still skipped |
Cause
- WXT reads the files in precedence order:
.env,.env.local,.env.[mode], and so on. parseEnvreturns each file's variables sorted alphabetically by byte value (digits, then uppercase letters, then_, then lowercase letters), not in file order.- WXT adds the pairs to one insertion-ordered map. If a name already exists, the new value replaces the old one, but the name keeps the position where it first appeared.
expandwalks the map from the top. A${NAME}is replaced only ifNAMEis already above it in the map. Otherwise it becomes an empty string.
A variable's position is therefore determined by the first file that mentions it, then by alphabetical order within that file. It is not determined by line order or by which file supplies the final value. The expanded values are then written to process.env, which Vite prefers over its own values.
Examples
Case 1: the target first appears in a more specific file.
# .env
VITE_API_URL=${DEPLOY_API_URL}
# .env.local (written by a deployment tool)
DEPLOY_API_URL=https://api.example.com/Expected: VITE_API_URL is https://api.example.com/ (the result in 0.20.20). Actual: "".
Case 2: the target sorts after the reference in the same file.
# .env
VITE_SITE_URL=https://app.example.com/
VITE_APP_CALLBACK_URL=${VITE_SITE_URL}callback/Expected: https://app.example.com/callback/. Actual: "callback/". VITE_APP_… sorts before VITE_SITE_…, although VITE_SITE_URL is written first.
To reproduce, put either case in a new WXT project, read the variable through import.meta.env in an entrypoint, and run wxt build.
Workaround
- Case 1: also declare the name, with an empty value, in the same file as the reference (
DEPLOY_API_URL=in.env). This fixes its position, and.env.localstill supplies the value. - Case 2: rename the referenced variable so it sorts before the variable that uses it.
Both depend on the processing order. If the value is never supplied, the reference still resolves to an empty string.
Possible fix (untested)
Resolve references against the complete merged set, as Vite's loader does, instead of only against already-processed variables. #2575 (inline dotenv-expand) changes the same code.
System Info
wxt: 0.21.4
Node: 26.7.0
OS: Microsoft Windows [Version 10.0.26200.9457]Used Package Manager
npm
Validations
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Activity
[-]`.env` interpolation resolves `${VAR}` to empty when the variable is defined in a more specific file or sorts later in the same file (regression in 0.20.21)[/-][+]`.env` interpolation resolves `${VAR}` to empty when the variable is defined in a more specific file or sorts later in the same file (regression)[/+]