cloudflare / cloudflare/workers-sdk

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

Open
#15,068 2 comments 0 reactions 0 assignees View on GitHub
package:wrangler
Dominant language
TypeScript
Stars
4.5k
Forks
1.5k
Avg merge
3d 8h
Merged PRs (30d)
187

Description

### 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.` / `.dev.vars.` files. `--env` does both. So the two ways of choosing an environment disagree about which env files get loaded.

### Steps to reproduce

```jsonc
// wrangler.jsonc
{
"name": "my-worker",
"main": "src/index.ts",
"compatibility_date": "2026-01-01",
"env": { "staging": {} }
}
```

```
# .dev.vars.staging
MY_SECRET = "from-staging"
```

```sh
# 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.`.

### 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.

Contributor guide

Open the contributing guide

Research direction

Start in Wrangler core's environment and env-file loading paths, comparing how --env and CLOUDFLARE_ENV are resolved. Reproduce the difference with wrangler dev --env staging and CLOUDFLARE_ENV=staging, then review the fix and tests referenced in #15022; done means both selectors load the matching .env. and .dev.vars. files.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.