Skip to content

CLOUDFLARE_ENV does not select .env.<environment> / .dev.vars.<environment> files, but --env does #15068

Description

@LeSingh1

Which Cloudflare product(s) does this pertain to?

Wrangler core

What version(s) of the tool(s) are you using?

wrangler@latest (reproduced on main)

What is the issue or question you have?

CLOUDFLARE_ENV selects the config environment, but it does not select the matching .env.<environment> / .dev.vars.<environment> files. --env does both. So the two ways of choosing an environment disagree about which env files get loaded.

Steps to reproduce

// wrangler.jsonc
{
  "name": "my-worker",
  "main": "src/index.ts",
  "compatibility_date": "2026-01-01",
  "env": { "staging": {} }
}
# .dev.vars.staging
MY_SECRET = "from-staging"
# loads .dev.vars.staging  ->  MY_SECRET === "from-staging"
npx wrangler dev --env staging

# applies the staging config environment, but does NOT load .dev.vars.staging
CLOUDFLARE_ENV=staging npx wrangler dev

The same applies to .env.<environment>.

Why this looks like a bug rather than intended behaviour

The config loader already treats the two as equivalent — it resolves the environment as --env first, then CLOUDFLARE_ENV, and its own error message says "You have specified the environment ... via the CLOUDFLARE_ENV environment variable." The env-file lookup is the one place that only reads --env.

--env's help text describes it as "Environment to use for operations, and for selecting .env and .dev.vars files", so the file selection is documented as part of choosing an environment, not as an --env-only feature.

Impact

A Worker deployed from CI with CLOUDFLARE_ENV set silently gets the wrong secrets and vars locally — no warning, the values just come from the base .dev.vars instead of the environment-specific one.

I have a fix and tests for this in #15022; happy to close that and work from whatever you decide here.

Activity

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

    package:wranglerRelating to the `wrangler` package

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions