Skip to content

Fix astro:env tests in Cloudflare#14925

Merged
matthewp merged 3 commits intofeat/environment-apifrom
tests/env-astro-env
Dec 1, 2025
Merged

Fix astro:env tests in Cloudflare#14925
matthewp merged 3 commits intofeat/environment-apifrom
tests/env-astro-env

Conversation

@matthewp
Copy link
Copy Markdown
Contributor

Changes

  • This fixes astro:env tests and brings back .dev.vars support
  • Copy .dev.vars to the codegen directory if using a scoped wrangler.json
  • Copy .dev.vars to process.env so that astro:env can see it.

Testing

  • Brought back astro-env.test.js. Refactored to use the preview server for build testing.

Docs

N/A, bug fix

This fixes astro:env tests and brings back .dev.vars support
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Nov 28, 2025

⚠️ No Changeset found

Latest commit: 24de78b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

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

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label Nov 28, 2025
Comment on lines +197 to +202
// Copy .dev.vars to codegen dir if it exists
const devVarsPath = new URL('.dev.vars', config.root);
const devVarsCodegenPath = new URL('.dev.vars', codegenDir);
if (existsSync(devVarsPath)) {
copyFileSync(devVarsPath, devVarsCodegenPath);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think is the correct fix. If you run the fixture astro-dev-platform using astro dev, you'll be able to see this log

Using vars defined in .dev.vars

We don't log this, but cloudflare does. Which means that in dev, everything is correct. The issue is related to build/preview

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what you mean. The tests have build and ssr tests using the preview server, they work there too. Here's the code where we copy the code in the preview path: https://github.com/withastro/astro/pull/14925/files#diff-5f874afb8a78db44cdc5d0877cb87511119e6eeda232bca86911b0f3a41a825bR35-R40

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Then I don't understand the added code, if things already work

Copy link
Copy Markdown
Contributor Author

@matthewp matthewp Dec 1, 2025

Choose a reason for hiding this comment

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

I'm saying that after these changes, the tests which cover both dev and build work. They were failing without these changes.

What's happening in this line is that if the user doesn't have a wrangler.json/toml file we create the codegen and add our own. This line copies the .dev.vars to that directory, because the Cloudflare vite plugin looks up .dev.vars relative to where the config file is located.

You're saying that there's an issue with build/preview. What is the issue? Is there something the tests aren't covering?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What's happening in this line is that if the user doesn't have a wrangler.json/toml file we create the codegen and add our own. This line copies the .dev.vars to that directory, because the Cloudflare vite plugin looks up .dev.vars relative to where the config file is located.

Cheers. This wasn't clear from the PR description. Please, next time, highlight such quirks in the PR description so we avoid back-and-forth. 😃

Comment on lines +312 to +313
const data = readFileSync(devVarsPath, 'utf-8');
const parsed = parse(data);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This function can throw a runtime error, we should catch it and maybe:

  • handle it gracefully with a log
  • or throw and AstroError

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added an error log.

@matthewp matthewp merged commit 922daf0 into feat/environment-api Dec 1, 2025
22 checks passed
@matthewp matthewp deleted the tests/env-astro-env branch December 1, 2025 15:26
ematipico added a commit that referenced this pull request Dec 4, 2025
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Co-authored-by: Samuel Macleod <smacleod@cloudflare.com>
Co-authored-by: Alexander Niebuhr <alexander@nbhr.io>
Co-authored-by: ascorbic <213306+ascorbic@users.noreply.github.com>
Co-authored-by: matthewp <361671+matthewp@users.noreply.github.com>
Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
Co-authored-by: Matt Kane <m@mk.gg>
Co-authored-by: florian-lefebvre <69633530+florian-lefebvre@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Matthew Phillips <matthew@skypack.dev>
Co-authored-by:  Matthew Phillips <matthew@skypack.dev>
Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
Co-authored-by: Hayato Hasegawa <hase1225hayato@gmail.com>
fix: creation of routes list in build (#14325)
Fix unit tests for environment API (#14394)
fixture (#14599)
fix: organize imports and formatting in cloudflare handler (#14697)
fix: resolve environment-api build issues for client & CSS (#14770)
Fixes CSS when both static/client styles exist (#14790)
fix: flag `serverLike` incorrectly computed (#14792)
fix: redirects and i18n routing (#14797)
fix: logger level and static paths params (#14799)
Fix image tests crashing (#14803)
fix(environment): Error overlay not working (#14816)
fix: hashing of chunks (#14811)
Fix before-hydration script emission for environment-api (#14818)
fix: node leak into content collections (#14820)
fix: avoid `node:path` (#14827)
Fix API route tests (#14825)
Fix unit tests (#14832)
fix: server islands build (#14830)
Fix astro-component-bundling tests (#14840)
Fix astro-component-bundling tests (#14837)
Fix config alias by using resolve.alias config (#14828)
fix: container and routing tests (#14852)
fix: respect user vite config on sourcemaps (#14847)
fix: remove useless tests (#14854)
Fixes assetQueryParams and public folder tests (#14853)
fix: renderError fallback (#14855)
Fix server-islands data being used across tests (#14857)
fix: incorrect configuration in tests (#14858)
Fixes for tests in environment branch (#14865)
Fixes for e2e tests in environment branch (#14870)
fix(test): svelte async rendering (#14886)
fix(cloudflare/vercel/netlify): some tests pass and excluded others (#14892)
Fixes for node integrations tests (#14900)
Fix @astrojs/db tests (#14899)
fix(environment): Prevent Vite from interpreting injected sourcemap comments as actual sourcemaps (#14921)
Fix remaining cloudflare tests (#14912)
fix(vite): import mergeConfig from direct file to avoid dynamic import being processed by Vite (#14939)
Fix astro:env tests in Cloudflare (#14925)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: integration Related to any renderer integration (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants