Skip to content

[wrangler] Respect CLOUDFLARE_ENV when selecting .env and .dev.vars files - #15022

Open
LeSingh1 wants to merge 1 commit into
cloudflare:mainfrom
LeSingh1:fix/cloudflare-env-dot-env-files
Open

[wrangler] Respect CLOUDFLARE_ENV when selecting .env and .dev.vars files#15022
LeSingh1 wants to merge 1 commit into
cloudflare:mainfrom
LeSingh1:fix/cloudflare-env-dot-env-files

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

CLOUDFLARE_ENV is supported as an alternative to --env, and the config loader resolves the active environment as args.env ?? getCloudflareEnv(). The .env and .dev.vars file lookups only consulted args.env, so CLOUDFLARE_ENV=staging wrangler dev activated the staging config environment but still loaded the top-level .env/.dev.vars files instead of .env.staging/.dev.vars.staging.

Two call sites were affected:

  • packages/wrangler/src/index.tsgetDefaultEnvFiles(args.env), which populates process.env for every command.
  • packages/wrangler/src/dev/start-dev.tsenv: args.env, which reaches .dev.vars.<env> / .env.<env> selection via ConfigControllergetBindings()getVarsForDev().

Both now use the same args.env ?? getCloudflareEnv() ordering as the config loader, so --env continues to take precedence when both are set. A test covers that precedence explicitly — it passes before and after, as a regression guard.


  • Tests
    • Tests included/updated
  • Public documentation
    • Documentation not necessary because: this aligns behaviour with the documented meaning of CLOUDFLARE_ENV and with the --env flag's own help text ("Environment to use for operations, and for selecting .env and .dev.vars files"). No documented behaviour changes.

Note

This is a contribution from an AI agent: Claude Code (Claude Opus 4.5), working on behalf of @LeSingh1. Review comments will be read and responded to.


Open in Devin Review

@changeset-bot

changeset-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9e67844

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
wrangler Patch
@cloudflare/vite-plugin Patch
@cloudflare/vitest-pool-workers Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-project-automation github-project-automation Bot moved this to Untriaged in workers-sdk Aug 4, 2026
@workers-devprod
workers-devprod requested review from a team and jamesopstad and removed request for a team August 4, 2026 17:58
@workers-devprod

workers-devprod commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/wrangler
Show detailed file reviewers
  • .changeset/cloudflare-env-dot-env-files.md: [@cloudflare/wrangler]
  • packages/wrangler/src/tests/dev.test.ts: [@cloudflare/wrangler]
  • packages/wrangler/src/api/startDevWorker/ConfigController.ts: [@cloudflare/wrangler]
  • packages/wrangler/src/index.ts: [@cloudflare/wrangler]

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

@pkg-pr-new

pkg-pr-new Bot commented Aug 4, 2026

Copy link
Copy Markdown
@cloudflare/autoconfig

npm i https://pkg.pr.new/@cloudflare/autoconfig@15022

@cloudflare/build-output-utils

npm i https://pkg.pr.new/@cloudflare/build-output-utils@15022

@cloudflare/config

npm i https://pkg.pr.new/@cloudflare/config@15022

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@15022

@cloudflare/deploy-helpers

npm i https://pkg.pr.new/@cloudflare/deploy-helpers@15022

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@15022

miniflare

npm i https://pkg.pr.new/miniflare@15022

@cloudflare/pages-functions

npm i https://pkg.pr.new/@cloudflare/pages-functions@15022

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@15022

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@15022

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@15022

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@15022

@cloudflare/workers-auth

npm i https://pkg.pr.new/@cloudflare/workers-auth@15022

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@15022

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@15022

wrangler

npm i https://pkg.pr.new/wrangler@15022

commit: 9e67844

@LeSingh1
LeSingh1 force-pushed the fix/cloudflare-env-dot-env-files branch from 1fc9f12 to 291f6ca Compare August 4, 2026 22:28
@LeSingh1

LeSingh1 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

Pushed a correction: the first version applied the CLOUDFLARE_ENV fallback to StartDevWorkerInput.env, which also feeds config loading, so the redirected-config fixture started reporting a mismatched environment as coming from --env/-e instead of the env var.

The fallback now sits at the two places that actually select the files — the getBindings() call and getDefaultEnvFiles() — and input.env is left alone, so the validator can still tell the two sources apart. fixtures/redirected-config-worker passes 6/6 locally.

…iles

`CLOUDFLARE_ENV` is supported as an alternative to `--env`, and the config
loader falls back to it via `args.env ?? getCloudflareEnv()`. The `.env` and
`.dev.vars` file lookups only consulted `args.env`, so
`CLOUDFLARE_ENV=staging wrangler dev` activated the `staging` config
environment but still loaded the top-level `.env`/`.dev.vars` files rather
than `.env.staging`/`.dev.vars.staging`.

Apply the same fallback where those files are selected: at the `getBindings()`
call, whose `env` argument is only used to pick `.dev.vars.<env>`, and at
`getDefaultEnvFiles()`. `--env` continues to take precedence when both are
set.

The fallback is deliberately not applied to `StartDevWorkerInput.env`, which
also feeds config loading — the config validator distinguishes the two
sources to report whether an environment came from `--env/-e` or from
`CLOUDFLARE_ENV`, and collapsing them there would misattribute that error.
@LeSingh1
LeSingh1 force-pushed the fix/cloudflare-env-dot-env-files branch from 291f6ca to 9e67844 Compare August 5, 2026 05:25
@LeSingh1

LeSingh1 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main. The one remaining red check, Vite Plugin Playground (macos-latest, vite-8), is a 50s setup-hook timeout in react-spa/__tests__/experimental-headers-and-redirects — the same suite timed out on #15010's run too, which touches unrelated code. Everything else is green.

Locally: packages/wrangler dev.test.ts 4/4 for the new cases, and the redirected-config-worker fixture 6/6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Untriaged

Development

Successfully merging this pull request may close these issues.

2 participants