Skip to content

Support CLOUDFLARE_ENV environment variable for selecting the active environment#11228

Merged
petebacondarwin merged 12 commits intomainfrom
pbd/support-CLOUDFLARE_ENV-in-wrangler
Nov 24, 2025
Merged

Support CLOUDFLARE_ENV environment variable for selecting the active environment#11228
petebacondarwin merged 12 commits intomainfrom
pbd/support-CLOUDFLARE_ENV-in-wrangler

Conversation

@petebacondarwin
Copy link
Contributor

@petebacondarwin petebacondarwin commented Nov 9, 2025

Fixes #10928
Replaces #10940 (thanks @eduardo-vargas).

This change enables users to select the environment for commands such as CLOUDFLARE_ENV=prod wrangler versions upload. The --env command line argument takes precedence.

The CLOUDFLARE_ENV enviroment variable is mostly used with the @cloudflare/vite-plugin to select the environment for building the Worker to be deployed. This build also generates a "redirected deploy config" that is flattened to only contain the active environment.
To avoid accidentally deploying a version that is built for one environment to a different environment, there is an additional
check to ensure that if the user specifies an environment in Wrangler it matches the original selected environment from the build.

Reviewer Notes

  • The first 5 commits are tidy ups from me trying to get this PR to be green. If necessary I can pull them out in a separate PR that gets landed first.
  • The 6th commit is a big one but it is mostly just moving the environment variables handling over to workers-utils and updating all the imports
  • The 7th (and last commit barring any fixups) is the one that actually adds support and appropriate warnings to Wrangler for the CLOUDFLARE_ENV variable

@petebacondarwin petebacondarwin requested review from a team as code owners November 9, 2025 15:21
@changeset-bot
Copy link

changeset-bot bot commented Nov 9, 2025

🦋 Changeset detected

Latest commit: f482fc4

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

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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 9, 2025

create-cloudflare

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

@cloudflare/kv-asset-handler

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

miniflare

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

@cloudflare/pages-shared

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

@cloudflare/unenv-preset

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

@cloudflare/vite-plugin

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

@cloudflare/vitest-pool-workers

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

@cloudflare/workers-editor-shared

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

@cloudflare/workers-utils

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

wrangler

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

commit: f482fc4

devin-ai-integration bot added a commit to cloudflare/cloudflare-docs that referenced this pull request Nov 9, 2025
- Add CLOUDFLARE_ENV to system environment variables documentation
- Update environments documentation to mention CLOUDFLARE_ENV usage
- Add changelog entry for the new feature

Related to cloudflare/workers-sdk#11228

Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com>
@petebacondarwin petebacondarwin force-pushed the pbd/support-CLOUDFLARE_ENV-in-wrangler branch 3 times, most recently from 04c0b77 to b7c64fc Compare November 10, 2025 09:31
@ascorbic
Copy link
Contributor

I think these test failures are legit, as they are related to enviornments

@petebacondarwin petebacondarwin marked this pull request as draft November 10, 2025 12:41
@petebacondarwin
Copy link
Contributor Author

Yeah sorry @ascorbic - I am working on it. I meant to set this back to "draft".

@petebacondarwin petebacondarwin force-pushed the pbd/support-CLOUDFLARE_ENV-in-wrangler branch 5 times, most recently from f7ca3ef to dfce7e7 Compare November 10, 2025 20:09
@petebacondarwin petebacondarwin marked this pull request as ready for review November 11, 2025 09:24
setLogLevel(envLogLevel as LoggerLevel);
}
// Also set the CLI package log level to match
setLogLevel(logger.loggerLevel);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The loggerLevel property is actually a getter that computes the level taking into account the current ALS.

Copy link
Contributor

@jamesopstad jamesopstad left a comment

Choose a reason for hiding this comment

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

As discussed in chat, if CLOUDFLARE_ENV is also read via dotenv in Wrangler then I am less convinced this is a good idea. Imagine the following example:

.env

CLOUDFLARE_ENV=development

.env.production

CLOUDFLARE_ENV=production

The build is run using vite build --mode production, which reads .env.production. Running wrangler deploy would then read .env and so the selected CLOUDFLARE_ENV value would not match the build.

@petebacondarwin
Copy link
Contributor Author

As discussed in chat, if CLOUDFLARE_ENV is also read via dotenv in Wrangler then I am less convinced this is a good idea. Imagine the following example:

.env

CLOUDFLARE_ENV=development

.env.production

CLOUDFLARE_ENV=production

The build is run using vite build --mode production, which reads .env.production. Running wrangler deploy would then read .env and so the selected CLOUDFLARE_ENV value would not match the build.

Dropping our offline conversation in here for transparency...

Looking at our docs, we do not recommend that the user puts CLOUDFLARE_ENV in the .env file.

https://developers.cloudflare.com/workers/vite-plugin/reference/cloudflare-environments/#combining-cloudflare-environments-and-vite-modes

Instead we only give examples of putting it in .env.production or .env.staging.

If there is not a CLOUDFLARE_ENV in the .env file then everything will work fine, since as far as Wrangler is concerned there is no CLOUDFLARE_ENV var set and so it will just deploy the redirected config as-is.

We could update the docs to explicitly call out not putting CLOUDFLARE_ENV in the .env file.

@petebacondarwin petebacondarwin force-pushed the pbd/support-CLOUDFLARE_ENV-in-wrangler branch from 7b99508 to 44af2c2 Compare November 11, 2025 15:56
@petebacondarwin petebacondarwin force-pushed the pbd/support-CLOUDFLARE_ENV-in-wrangler branch 2 times, most recently from fa8151a to b272db3 Compare November 14, 2025 09:26
@petebacondarwin petebacondarwin marked this pull request as draft November 14, 2025 10:21
@petebacondarwin petebacondarwin force-pushed the pbd/support-CLOUDFLARE_ENV-in-wrangler branch 2 times, most recently from 345ab79 to 194775d Compare November 24, 2025 12:16
@petebacondarwin petebacondarwin marked this pull request as ready for review November 24, 2025 13:08
petebacondarwin and others added 11 commits November 24, 2025 13:24
The startup-profiling test was leaking the logger level between tests,
so that got fixed in here too.
…e environment

This change enables users to select the environment for commands such as `CLOUDFLARE_ENV=prod wrangler versions upload`.
The `--env` command line argument takes precedence.

The `CLOUDFLARE_ENV` enviroment variable is mostly used with the `@cloudflare/vite-plugin` to select the environment for building
the Worker to be deployed. This build also generates a "redirected deploy config" that is flattened to only contain the active environment.
To avoid accidentally deploying a version that is built for one environment to a different environment, there is an additional
check to ensure that if the user specifies an environment in Wrangler it matches the original selected environment from the build.
@petebacondarwin petebacondarwin force-pushed the pbd/support-CLOUDFLARE_ENV-in-wrangler branch from 194775d to 9014b9c Compare November 24, 2025 13:24
/You have specified the environment ".*?", but are using a redirected configuration/
]);
} catch (e) {
error = e as string;
Copy link
Member

Choose a reason for hiding this comment

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

isn't e already an any?

Suggested change
error = e as string;
error = e;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right! For some reason I always assume that the thing being caught is types as unknown, not any.


let error = "";
try {
const { getOutput } = await runWranglerDev(basePath, [
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const { getOutput } = await runWranglerDev(basePath, [
await runWranglerDev(basePath, [

getOutput is not being used anywhere, is it?

@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Nov 24, 2025
@petebacondarwin petebacondarwin merged commit 43903a3 into main Nov 24, 2025
36 of 37 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Nov 24, 2025
@petebacondarwin petebacondarwin deleted the pbd/support-CLOUDFLARE_ENV-in-wrangler branch November 24, 2025 17:37
MattieTK pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Nov 28, 2025
* Document CLOUDFLARE_ENV environment variable for Wrangler

- Add CLOUDFLARE_ENV to system environment variables documentation
- Update environments documentation to mention CLOUDFLARE_ENV usage
- Add changelog entry for the new feature

Related to cloudflare/workers-sdk#11228

Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com>

* Update changelog to combine build and deploy commands with &

Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com>

* Add link to generated wrangler configuration docs in changelog

Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
elithrar pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Dec 9, 2025
* Document CLOUDFLARE_ENV environment variable for Wrangler

- Add CLOUDFLARE_ENV to system environment variables documentation
- Update environments documentation to mention CLOUDFLARE_ENV usage
- Add changelog entry for the new feature

Related to cloudflare/workers-sdk#11228

Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com>

* Update changelog to combine build and deploy commands with &

Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com>

* Add link to generated wrangler configuration docs in changelog

Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@PetarMetodiev
Copy link

As discussed in chat, if CLOUDFLARE_ENV is also read via dotenv in Wrangler then I am less convinced this is a good idea. Imagine the following example:
.env

CLOUDFLARE_ENV=development

.env.production

CLOUDFLARE_ENV=production

The build is run using vite build --mode production, which reads .env.production. Running wrangler deploy would then read .env and so the selected CLOUDFLARE_ENV value would not match the build.

Dropping our offline conversation in here for transparency...

Looking at our docs, we do not recommend that the user puts CLOUDFLARE_ENV in the .env file.

https://developers.cloudflare.com/workers/vite-plugin/reference/cloudflare-environments/#combining-cloudflare-environments-and-vite-modes

Instead we only give examples of putting it in .env.production or .env.staging.

If there is not a CLOUDFLARE_ENV in the .env file then everything will work fine, since as far as Wrangler is concerned there is no CLOUDFLARE_ENV var set and so it will just deploy the redirected config as-is.

We could update the docs to explicitly call out not putting CLOUDFLARE_ENV in the .env file.

Folks, sorry to resurrect a merged PR, but this cost me a couple days of debugging and I think it is worth addressing because it is an easy footgun.

I have a project with multiple dotenv files (.env, .env.production, .env.staging, etc.) and for consistency each one defines CLOUDFLARE_ENV=..., including the default .env (mine has CLOUDFLARE_ENV=dev).

What happens in my setup:

  • Build: I run vite build --mode production. Vite loads .env.production, so the build is produced with CLOUDFLARE_ENV=production and generates the redirected Wrangler config for that environment.
  • Deploy: Later I run wrangler deploy (without --env). Wrangler loads .env via dotenv, sees CLOUDFLARE_ENV=dev, and then selects the active environment as dev. This is effectively the same as running wrangler deploy --env dev, even though I did not pass --env.

This results in a confusing warning like:

▲ [WARNING] Processing build/server/wrangler.json configuration:

- No environment found in configuration with name "dev".
  Before using `--env=dev` there should be an equivalent environment section in the configuration.

  Consider adding an environment configuration section to the wrangler.json file:
  [env.dev]That message sent me down the wrong path. My issue was not "I forgot to add `[env.dev]`". My issue was "Wrangler picked up `CLOUDFLARE_ENV=dev` from `.env` via dotenv and treated it as an environment selection, while my build used a different mode and environment".

I see the docs recommend setting CLOUDFLARE_ENV in mode-specific files like .env.production or .env.staging, but they do not explicitly warn against defining it in the default .env, which is a common pattern when people try to keep env files uniform: Combining Cloudflare environments and Vite modes.

Could you please consider one of the following?

  • Docs: explicitly call out "Do not set CLOUDFLARE_ENV in .env; only set it in .env.<mode> files" (or otherwise clarify that Wrangler reads dotenv and will treat CLOUDFLARE_ENV as environment selection).
  • Error/warning UX: change the warning to mention CLOUDFLARE_ENV and dotenv explicitly (for example: "CLOUDFLARE_ENV=dev was loaded from .env. This makes Wrangler behave like --env dev. If you built with --mode production, move CLOUDFLARE_ENV to .env.production or unset it for deploy.")
  • Behavior (if appropriate): reconsider reading CLOUDFLARE_ENV from dotenv by default, or adjust precedence, so wrangler deploy does not unexpectedly behave like --env was passed.

Happy to open a new issue if that is preferred.

Thanks!

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

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

wrangler upload doesn't recognize CLOUDFLARE_ENV

5 participants