edge-functions: Deno.env should contain the same environment variables as in production
Describe the bug
Running edge functions locally with netlify dev, Deno.env contains all environment variables inherited from the CLI's shell. This is different from production, where it will only contain env vars specified in the Netlify UI, and two Deno-specific ones. This should be replicated in local dev.
One possible solution is to work with downstream @netlify/edge-bundler, and pass the --allow-env=... flag to only allow access to the environment variables that'd also exist in production:
https://github.com/netlify/edge-bundler/blob/0d2c6af674d30f0322bf376ad66acb0d5b5b0124/src/server/server.ts#L113
Steps to reproduce
Write an edge function, log Deno.env.toObject(). It should contain just placeholders for DENO_REGION and DENO_DEPLOYMENT_ID (probably not even that), but instead it contains all env vars inherited from the CLI's shell.
Configuration
No response
Environment
not relevant
cc @eduardoboucas
It should contain just placeholders for
DENO_REGIONandDENO_DEPLOYMENT_ID(probably not even that), but instead it contains all env vars inherited from the CLI's shell.
It also needs to contain the environment variables loaded from the API, which are merged into process.env. This is also the case with serverless functions.
One possible solution is to work with downstream
@netlify/edge-bundler, and pass the--allow-env=...flag to only allow access to the environment variables that'd also exist in production:
This does not work, because first of all we would need to remove --allow-all, because it overrides --allow-env. But the real problem is that DENO will throw once you try to access a not allowed environment variable. So doing Deno.env.toObject() throws PermissionDenied: Requires env access to "DENO_DEPLOYMENT_ID", run again with the --allow-env flag
I guess we have to explicitly set the environment variables.
wow that's weird behaviour 🤔