Commit ad8507d
fix(hir): keep the null-guard for an optional method call on a
`process.env?.[k]?.method()` (env read accessed inline) silently dropped the
per-receiver null-guard on the `?.` before the method, so the method was
invoked on the `undefined` an unset variable reads as. The result was the
STRING "undefined" (from stringifying the missing value) instead of a
short-circuit to `undefined`.
Cause: `process.env[k]` lowers to `IndexGet { object: ProcessEnv, .. }`, and
the `a?.b?.method()` lowering in `arm_optchain` only re-adds the receiver
null-guard when the receiver is `opt_call_receiver_repeatable`. That predicate
did not list `ProcessEnv` / env reads, so the receiver was deemed unsafe to
evaluate twice and the guard was skipped — leaving `process.env[k].method()`
to dereference the unguarded `undefined`. (Hoisting the env read into a local
first — `const e = process.env; e?.[k]?.method()` — worked, because a
`LocalGet` receiver is repeatable and kept the guard.)
Env reads are pure, side-effect-free, and stable within an expression, so they
are safe to evaluate more than once (guard + call). Add `ProcessEnv`,
`EnvGet`, and `EnvGetDynamic` (repeatable iff its key is) to
`opt_call_receiver_repeatable`.
This shape is ubiquitous: SDKs read config via `readEnv(k)?.trim()`, and a
common HTTP-client base-URL default is `process.env.BASE_URL?.trim() ?? "…"`,
which silently became the string "undefined" and produced
`new URL("undefined/…")` failures.
Adds an e2e test covering computed/static keys, a set var flowing through, and
a raw missing read being JS `undefined` (not the string).
Co-authored-by: Ralph Küpper <ralph@skelpo.com>process.env read (#6058)1 parent 5fe1303 commit ad8507d
2 files changed
Lines changed: 103 additions & 0 deletions
File tree
- crates
- perry-hir/src/lower/lower_expr
- perry/tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
270 | 270 | | |
271 | 271 | | |
272 | 272 | | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
273 | 283 | | |
274 | 284 | | |
275 | 285 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 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 | + | |
0 commit comments