Skip to content

Astro Env behaves not like expected in dev and cloudflare #14036

@alexanderniebuhr

Description

@alexanderniebuhr

Astro Info

Astro                    v5.10.2
Node                     v22.16.0
System                   macOS (arm64)
Package Manager          pnpm
Output                   static
Adapter                  @astrojs/cloudflare
Integrations             @astrojs/starlight

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

https://discord.com/channels/830184174198718474/1390078410503749634

Using vars defined in .dev.vars
[mf:info] ✨ Parsed 3 valid redirect rules.
platformProxy.env inside astro:server:setup {
  GITHUB_CLIENT_ID: 'Ov23lig7iTHNZD7CWaHS',
  BETTER_AUTH_SECRET: 'SECRET-REDACTED',
  GITHUB_CLIENT_SECRET: 'SECRET-REDACTED',
  DB: ProxyStub { name: 'D1Database', poisoned: false },
  ASSETS: ProxyStub { name: 'Fetcher', poisoned: false }
}
process.env after loop in setProcessEnv {
  NODE_ENV: 'development',
  BETTER_AUTH_SECRET: 'SECRET-REDACTED',
  GITHUB_CLIENT_SECRET: 'SECRET-REDACTED'
}
09:24:05 [types] Generated 1ms
09:24:06 [content] Syncing content
09:24:06 [content] Synced content
[EnvInvalidVariables] The following environment variables defined in `env.schema` are invalid:

- BETTER_AUTH_SECRET is missing
- GITHUB_CLIENT_SECRET is missing

  Error reference:
    https://docs.astro.build/en/reference/errors/env-invalid-variables/
  Location:
    /Users/alexanderniebuhr/Developer/Projects/astrolicious/astro-tips/node_modules/.pnpm/astro@5.10.2_@types+node@22.16.0_rollup@4.44.1_typescript@5.8.3_yaml@2.8.0/node_modules/astro/dist/env/vite-plugin-env.js:101:11
  Stack trace:
    at validatePublicVariables (file:///Users/alexanderniebuhr/Developer/Projects/astrolicious/astro-tips/node_modules/.pnpm/astro@5.10.2_@types+node@22.16.0_rollup@4.44.1_typescript@5.8.3_yaml@2.8.0/node_modules/astro/dist/env/vite-plugin-env.js:101:11)
    at PluginContext.buildStart (file:///Users/alexanderniebuhr/Developer/Projects/astrolicious/astro-tips/node_modules/.pnpm/astro@5.10.2_@types+node@22.16.0_rollup@4.44.1_typescript@5.8.3_yaml@2.8.0/node_modules/astro/dist/env/vite-plugin-env.js:44:7)
    at EnvironmentPluginContainer.buildStart (file:///Users/alexanderniebuhr/Developer/Projects/astrolicious/astro-tips/node_modules/.pnpm/vite@6.3.5_@types+node@22.16.0_yaml@2.8.0/node_modules/vite/dist/node/chunks/dep-DBxKXgDP.js:42171:12)
    at initServer (file:///Users/alexanderniebuhr/Developer/Projects/astrolicious/astro-tips/node_modules/.pnpm/vite@6.3.5_@types+node@22.16.0_yaml@2.8.0/node_modules/vite/dist/node/chunks/dep-DBxKXgDP.js:38717:6)
    at file:///Users/alexanderniebuhr/Developer/Projects/astrolicious/astro-tips/node_modules/.pnpm/vite@6.3.5_@types+node@22.16.0_yaml@2.8.0/node_modules/vite/dist/node/chunks/dep-DBxKXgDP.js:25034:16
 ELIFECYCLE  Command failed with exit code 1.

So they are accessed and also populated to process.env using setProcessEnv, but they are still missing in the validation step 🤔
loadedEnv param of validatePublicVariables doesn't have them. It is the process.env object before they got populated 🤔
This envLoader.get() returns the wrong process.env

And this call happens after the Cloudflare custom one, so overriding everything the Cloudflare one did maybe?
I know not minimal repo, but still good enough I hope astrolicious/astro-tips.dev#388

What's the expected result?

setProcessEnv https://github.com/withastro/astro/blob/main/packages/integrations/cloudflare/src/index.ts#L298 should be passed through the whole lifecycle and not get overriden.

Link to Minimal Reproducible Example

astrolicious/astro-tips.dev#388

Participation

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

Metadata

Metadata

Labels

- P3: minor bugAn edge case that only affects very specific usage (priority)feat: envrelated to the way astro handles `.env` or `import.meta.env` or `process.env` (scope)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions