withastro / adapters

Home for Astro's core maintained adapters
47 stars 26 forks source link

Deploying with adapter 10.0 to CF does not load the env variables anymore #208

Closed irg1008 closed 3 months ago

irg1008 commented 3 months ago

Astro Info

Astro                    v4.5.12
Node                     v18.19.1
System                   Windows (x64)
Package Manager          pnpm
Output                   server
Adapter                  @astrojs/cloudflare
Integrations             @astrojs/tailwind
                         @astrojs/react
                         @astrojs/sitemap

Describe the Bug

When deploying to cloudflare with adapter 10.0 the env variables are not correctly loaded

What's the expected result?

To load the variables and not receive runtime errors from libraries or own code

Link to Minimal Reproducible Example

https://stackblitz.com/github/withastro/astro/tree/latest/examples/minimal?file=README.md

Participation

irg1008 commented 3 months ago

I see this now

process.env In the old version of the adapter we used to expose all the environment variables to process.env. This is no longer the case, as it was unsafe. If you need to use environment variables, you need to use either Astro.locals.runtime.env or context.locals.runtime.env. There is no way to access the environment variables directly from process.env or in the global scope. If you need to access the environment variables in global scope, you should refactor your code to pass the environment variables as arguments to your function or file. If you rely on any third library that uses process.env, please open an issue and we can investigate what the best way to handle this is.

I think this a is a HUGE breaking change. Every library now needs to change the process.env binding when used with astro?

alexanderniebuhr commented 3 months ago

Thanks for reporting. You are right this is a breaking change, but a needed one! The previous was unsafe and could lead to leaked environment variables. However you can still populate single values to 'process.env'.

Do you have an example where you need to use 'process.env' and can't use the other solution? Or show me a reproducible situation, so I can guide you how to use it in v10.

irg1008 commented 3 months ago

Yeah I am using xata ts sdk and even though I am manually passing the env variable using import.meta.env the library still throws an error stating that the env var does not exist.

Is this the right way to pass env variables outside astro? Should I populate process.env to check if the lib tries ti access it?

irg1008 commented 3 months ago

Okay I am not able to reproduce anymore. I really don't know what is I changed. I will close this for now until it raises again. Sorry for the inconvinience

alexanderniebuhr commented 3 months ago

No problem, please let us know any issues you encounter with v10. We want v10 to be much more reliable and stable than v9, but for that we need many users trying it and informing us about issues!