feat: pass mutated config environment variables to functions during local development #7325
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.
Summary
Fixes environment variables from build event handlers not being passed to functions during local development. Static config environment variables from
netlify.tomlwere already working via global process environment injection, but variables added by plugins via build event handlers were not.Problem
Build event handlers (like
onPreDev) could successfully mutate the config to add environment variables toconfig.build.environment, but these mutated environment variables were not available to functions inprocess.envduring local development.What was working:
netlify.toml→cachedConfig.env→ globalprocess.env→ functions ✅config.build.environment✅What was broken:
Root Cause
Static config environment variables work because they're included in
cachedConfig.envand injected into the globalprocess.envviainjectEnvVariables(). However, mutated config environment variables from build event handlers are only stored inconfig.build.environmentand were never being passed to functions.The
NetlifyFunction.invoke()method was passing an emptyenvironmentobject to the runtime, so functions only had access to the globalprocess.env(which contained static config vars) but not the mutated config environment variables.Solution
Instead of injecting mutated config into the global
process.env(which could affect other parts of the system), we:config.build.environmentinNetlifyFunction.invoke()environmentparameterprocess.envKey Changes
Core Implementation
src/lib/functions/netlify-function.ts: Extract environment variables from mutated config and pass to runtimesrc/lib/functions/runtimes/js/index.ts: Update V1 function path to accept and use environment variablesTesting
tests/integration/commands/dev/functions.test.ts: Significantly expanded test coverage:Note: While static config environment variables were already tested in
dev.config.test.tsfor V1 functions, this PR adds the first comprehensive test coverage for V2 functions and build event handler mutations.Technical Details
This approach is better than global environment injection because:
Use Case
This enables plugins to add environment variables via build event handlers:
Test Coverage
✅ V1 Functions - Static config and build event handler environment variables
✅ V2 Functions - Static config and build event handler environment variables
✅ Environment variable precedence - Process env overrides config env for both function types
✅ Backwards compatibility - All existing functionality preserved (16 existing tests still pass)
Backwards Compatibility
✅ No breaking changes - all existing functionality preserved
✅ Static config environment variables continue to work via global process.env
✅ Process environment variables continue to take precedence
✅ Functions without config environment variables work unchanged
For us to review and ship your PR efficiently, please perform the following steps:
passes our tests.
A picture of a cute animal (not mandatory, but encouraged)