Skip to content

.env interpolation resolves ${VAR} to empty when the variable is defined in a more specific file or sorts later in the same file (regression) #2655

Description

@garretwilson

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:

  1. February 2025: fix: regression on variable expansion in dotenv files #1449 fixed an earlier regression in "variable expansion in dotenv files".
  2. Replace dotenv with builtins #2176 requested replacing dotenv with Node builtins, citing Vite's use of util.parseEnv and dotenv-expand. An earlier PR with the same approach, chore(deps): replace dotenv with Node.js builtins #2185, was closed unmerged.
  3. 0.20.21: feat: replace dotenv with native node:util parseEnv #2181 replaced dotenv with parseEnv. Its description states: "Kept dotenv-expand to ensure support for variable expansion (e.g., VAR=$OTHER_VAR) remains functional for users."
  4. 0.20.22: .env file is no longer auto-imported #2266 reported that .env values were no longer applied to process.env. The fix, fix: Apply expanded env to process.env #2267, writes the expanded values to process.env and adds an expansion test marked it.skip, with the comment "Node doesn't return vars in the same order as they're defined".
  5. 0.21.0: dotenv-expand 12 → 13 (chore(deps)!: Upgrade major dependencies #2508, described as "non-breaking for WXT's purposes").
  6. 0.21.4: the expansion test is still skipped.

Upstream behavior that applies:

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

  1. WXT reads the files in precedence order: .env, .env.local, .env.[mode], and so on.
  2. parseEnv returns each file's variables sorted alphabetically by byte value (digits, then uppercase letters, then _, then lowercase letters), not in file order.
  3. 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.
  4. expand walks the map from the top. A ${NAME} is replaced only if NAME is 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.local still 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

Activity

  1. added theissue type on Oct 7, 2026
  2. added
    pending-triage
    on Oct 7, 2026
  3. changed the title [-]`.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)[/+] on Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending-triage

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions