Add fine-grained environment variable injection control #547
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Astro's Netlify adapter disables all environment variables by default, which prevents platform vars (
NETLIFY_LOCAL,CONTEXT,SITE_ID) from being available in local dev. This breaks platform features likepurgeCache()that require these vars.Changes
Added
environmentVariables.injectUserEnvoption (default:true)false: injects only platform env vars from 'general' and 'internal' sourcestrue: injects all env vars including user-defined ones from account, UI, config files, and addonsUpdated
injectEnvVariables()functionisPlatformEnvironmentVariable()helper to identify platform vars by sourceinjectUserEnvisfalseTests and documentation
@netlify/devand@netlify/vite-pluginpackagesUsage
For integrations that manage their own env vars but need platform functionality:
This allows Astro's adapter to enable platform vars without surprising users with injected env vars from their Netlify site.
Original prompt
Issue Title: Add fine-grained env var injection to Vite plugin and Astro adapter
Issue Description: Astro’s Netlify adapter disables the Vite plugin’s environment variables feature by default, which prevents basic platform vars (e.g.,
NETLIFY_LOCAL,CONTEXT,SITE_ID) from being available in local dev. This led to differences vs.netlify dev(e.g.,purgeCache()failing without a site ID when using the Vite plugin).Proposal:
features.environmentVariables.injectUserEnv: boolean, so platform env vars can be enabled without injecting user-defined env.References:
purgeCache()behavior and env var defaults: thread context@netlify/config: https://github.com/netlify/build/blob/3bf580227e8efae895429a3c3871fcd6f7185a74/packages/config/src/env/main.ts#L111Fixes https://linear.app/netlify/issue/FRB-2020/add-fine-grained-env-var-injection-to-vite-plugin-and-astro-adapter
Comment by User db2967f5-c49c-428b-ae14-6c61855027c1:
netlify/primitives
Comment by User c8ba5c18-8b2b-41c2-8484-9ca4ad793475:
📋 I wasn't able to determine which GitHub repository to work in.
I think it's one of these, but can you tell me which one is right?
Comment by User c8ba5c18-8b2b-41c2-8484-9ca4ad793475:
📋 I wasn't able to determine which GitHub repository to work in.
I think it's one of these, but can you tell me which one is right?
Comment by User :
This thread is for an agent session with githubcopilot.
Comment by User :
This thread is for an agent session with githubcopilot.
Comment by User 8e14cbd8-ca68-4b55-956a-3ba8677986e0:
Seems like they preferred to release after ensuring the prompt is good, rather than releasing garbage quickly 😉
Comment by User 8e14cbd8-ca68-4b55-956a-3ba8677986e0:
Nice!
Comment by User db2967f5-c49c-428b-ae14-6c61855027c1:
Wow I'm impressed. That Linear issue description is perfect.
Comment by User :
Created issue FRB-2020
Comment by User db2967f5-c49c-428b-ae14-6c61855027c1:
This comment thread is synced to a corresponding thread in Slack. All replies are displayed in both locations.
Comment by User 8e14cbd8-ca68-4b55-956a-3ba8677986e0:
Meanwhile, merged updates https://github.com/netlify-templates/astro-platform-starter
Comment by User db2967f5-c49c-428b-ae14-6c61855027c1:
let me try this new fancy thing...
@linear create an issue to add the more fine-grained env var option I mentioned above to the vite plugin + update the astro netlify adapter to use it
Comment by User 8e14cbd8-ca68-4b55-956a-3ba8677986e0:
But seems like the scope of not enabling it is a bit wider than we thought, or we'd have taken care of the "platform" env vars then
Comment by User 8e14cbd8-ca68-4b55-956a-3ba8677986e0:
Yeah, I recall that we talked to @matthew Phillips about that
Comment by User db2967f5-c49c-428b-ae14-6c61855027c1:
trying to remember what astro was even trying to solve by disable env vars... found the PR that did it: withastro/astro#14078. :confused-travolta:
Seems like it was just about the surprise factor / potentially breaking change:
Comment by User 8e14cbd8-ca68-4b55-956a-3ba8677986e0:
anyway this solves the purgeCache() failure, but I was curious if I would get AI Gateway env vars as well, and the answer's still no 😕
Comment by User 8e14cbd8-ca68-4b55-956a-3ba8677986e0:
I agree, we should bring that up since our own platform functionality is impaired
Comment by User db2967f5-c49c-428b-ae14-6c61855027c1:
btw arguably the
@astrojs/netlifydocs are lying here (they say the env vars feature is enabled, then lower on the page it says disabled): [https://docs.astro.build/en/guides/integrations-guide/netlify/#local-development-features](https://docs.astro.build/en/guides/integrations-guide/netlify/#local-development...💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.