Astro Info
Astro v6.0.8
Vite v7.3.1
Node v24.7.0
System macOS (arm64)
Package Manager pnpm
Output server
Adapter @astrojs/cloudflare (v13.1.3)
Integrations @storyblok/astro (v8.1.0)
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
When using wrangler environment-specific config (env.staging in wrangler.jsonc) and building with astro build --env staging, the generated dist/server/wrangler.json always contains the top-level config values instead of the environment-specific ones. This means account_id, route, name, assets, and other env-overridden fields are incorrect in the build output.
Running wrangler deploy --env staging after the build then either deploys to the wrong account or fails due to mismatched config.
Root Cause
The @cloudflare/vite-plugin determines which wrangler environment to use via the CLOUDFLARE_ENV environment variable, loaded through vite.loadEnv():
// @cloudflare/vite-plugin/dist/index.mjs
const prefixedEnv = vite.loadEnv(viteEnv.mode, root, ["CLOUDFLARE_", ...]);
const cloudflareEnv = prefixedEnv.CLOUDFLARE_ENV;
@astrojs/cloudflare never sets or forwards CLOUDFLARE_ENV. Astro's --env flag is not propagated to the Cloudflare vite plugin, so unstable_readConfig is called with env: undefined, resolving to the top-level config.
Reproduction
wrangler.jsonc:
Build command:
astro build --env staging
Result in dist/server/wrangler.json:
{
"account_id": "production-account-id",
"name": "my-app",
"route": { "pattern": "production.example.com", "custom_domain": true }
}
Expected in dist/server/wrangler.json:
{
"account_id": "staging-account-id",
"name": "my-app-staging",
"targetEnvironment": "staging",
"route": { "pattern": "staging.example.com", "custom_domain": true }
}
Workaround
Manually set CLOUDFLARE_ENV as an environment variable before building:
CLOUDFLARE_ENV=staging astro build --env staging
This correctly resolves the wrangler environment config and produces the expected output.
What's the expected result?
@astrojs/cloudflare should forward Astro's --env value to CLOUDFLARE_ENV (or pass it directly to the @cloudflare/vite-plugin config) so that wrangler.unstable_readConfig resolves the correct environment.
Link to Minimal Reproducible Example
https://github.com/F0rce/astro-cloudflare-env-bug
Participation
Astro Info
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
When using wrangler environment-specific config (
env.staginginwrangler.jsonc) and building withastro build --env staging, the generateddist/server/wrangler.jsonalways contains the top-level config values instead of the environment-specific ones. This meansaccount_id,route,name,assets, and other env-overridden fields are incorrect in the build output.Running
wrangler deploy --env stagingafter the build then either deploys to the wrong account or fails due to mismatched config.Root Cause
The
@cloudflare/vite-plugindetermines which wrangler environment to use via theCLOUDFLARE_ENVenvironment variable, loaded throughvite.loadEnv():@astrojs/cloudflarenever sets or forwardsCLOUDFLARE_ENV. Astro's--envflag is not propagated to the Cloudflare vite plugin, sounstable_readConfigis called withenv: undefined, resolving to the top-level config.Reproduction
wrangler.jsonc:{ "account_id": "production-account-id", "name": "my-app", "env": { "staging": { "account_id": "staging-account-id", "name": "my-app-staging", "route": { "pattern": "staging.example.com", "custom_domain": true } } } }Build command:
Result in
dist/server/wrangler.json:{ "account_id": "production-account-id", "name": "my-app", "route": { "pattern": "production.example.com", "custom_domain": true } }Expected in
dist/server/wrangler.json:{ "account_id": "staging-account-id", "name": "my-app-staging", "targetEnvironment": "staging", "route": { "pattern": "staging.example.com", "custom_domain": true } }Workaround
Manually set
CLOUDFLARE_ENVas an environment variable before building:This correctly resolves the wrangler environment config and produces the expected output.
What's the expected result?
@astrojs/cloudflareshould forward Astro's--envvalue toCLOUDFLARE_ENV(or pass it directly to the@cloudflare/vite-pluginconfig) so thatwrangler.unstable_readConfigresolves the correct environment.Link to Minimal Reproducible Example
https://github.com/F0rce/astro-cloudflare-env-bug
Participation