Skip to content

@astrojs/cloudflare: astro build --env <name> does not pass wrangler environment to @cloudflare/vite-plugin #16031

Description

@F0rce

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:

{
  "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:

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

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P2: has workaroundAn edge case that only affects very specific usage, but has a trivial workaround (priority)pkg: cloudflareRelated to the Cloudflare adapter

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions