Commit 17cb26b
Caleb Barnes
feat: pass mutated config environment variables to functions during local development (#7325)
* feat: pass mutated config environment variables to functions during local development
## Summary
Fixes environment variables from build event handlers not being passed to functions during local development. Static config environment variables from `netlify.toml` were 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 to `config.build.environment`, but these **mutated** environment variables were not available to functions in `process.env` during local development.
**What was working:**
- Static environment variables from `netlify.toml` → `cachedConfig.env` → global `process.env` → functions ✅
- Build event handler mutations → `config.build.environment` ✅
- Process env override behavior ✅
**What was broken:**
- Mutated config environment variables → functions ❌
## Root Cause
Static config environment variables work because they're included in `cachedConfig.env` and injected into the global `process.env` via `injectEnvVariables()`. However, mutated config environment variables from build event handlers are only stored in `config.build.environment` and were never being passed to functions.
The `NetlifyFunction.invoke()` method was passing an empty `environment` object to the runtime, so functions only had access to the global `process.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:
- Extract environment variables from `config.build.environment` in `NetlifyFunction.invoke()`
- Pass them directly to the function runtime via the `environment` parameter
- The runtime injects them into the function's specific `process.env`
- Maintain existing precedence: process env variables override config env variables
## Key Changes
### Core Implementation
- **`src/lib/functions/netlify-function.ts`**: Extract environment variables from mutated config and pass to runtime
- **`src/lib/functions/runtimes/js/index.ts`**: Update V1 function path to accept and use environment variables
### Testing
- **`tests/integration/commands/dev/functions.test.ts`**: Significantly expanded test coverage:
- **NEW**: Static config environment variables for V1 functions
- **NEW**: Build event handler environment variables for V1 functions
- **NEW**: Static config environment variables for V2 functions *(first V2 function env var tests)*
- **NEW**: Build event handler environment variables for V2 functions *(first V2 function env var tests)*
- **NEW**: Environment variable precedence for both V1 and V2 functions *(first build event handler mutation tests)*
**Note**: While static config environment variables were already tested in `dev.config.test.ts` for 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:
- It doesn't pollute the global process environment
- It only affects the specific function being invoked
- It maintains proper precedence handling
- It works for both V1 and V2 function runtimes
## Use Case
This enables plugins to add environment variables via build event handlers:
```javascript
// Plugin build event handler
export const onPreDev = ({ netlifyConfig }) => {
netlifyConfig.build.environment.MY_PLUGIN_VAR = 'value'
}
// Function can now access the mutated config env var during local dev
export const handler = async (event, context) => {
console.log(process.env.MY_PLUGIN_VAR) // 'value' ✅ (was undefined ❌)
return { statusCode: 200, body: 'OK' }
}
```
## 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)1 parent 813a2e2 commit 17cb26b
File tree
3 files changed
+241
-2
lines changed- src/lib/functions
- runtimes/js
- tests/integration/commands/dev
3 files changed
+241
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
252 | 252 | | |
253 | 253 | | |
254 | 254 | | |
255 | | - | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
256 | 272 | | |
257 | 273 | | |
258 | 274 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
73 | 79 | | |
74 | 80 | | |
75 | 81 | | |
| |||
114 | 120 | | |
115 | 121 | | |
116 | 122 | | |
| 123 | + | |
117 | 124 | | |
118 | 125 | | |
119 | 126 | | |
120 | 127 | | |
121 | 128 | | |
| 129 | + | |
122 | 130 | | |
123 | 131 | | |
124 | 132 | | |
| |||
134 | 142 | | |
135 | 143 | | |
136 | 144 | | |
| 145 | + | |
| 146 | + | |
137 | 147 | | |
138 | 148 | | |
139 | 149 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
0 commit comments