next.js
cee16b5f - Turbopack: treat process.env.hasOwnProperty(key) as an env var existence check (#102284)

Commit
1 day ago
Turbopack: treat process.env.hasOwnProperty(key) as an env var existence check (#102284) ### What? Turbopack's env var tracking (`EnvVarInfo.runtime`) now treats these as existence checks of `FOO`, like `'FOO' in process.env`: - `process.env.hasOwnProperty('FOO')` and `process.env.propertyIsEnumerable('FOO')` (the same for env vars) - `Object.hasOwn(process.env, 'FOO')`, also extracted (`const { hasOwn } = Object`) - `Object.prototype.hasOwnProperty.call(process.env, 'FOO')`, `process.env.hasOwnProperty.call(process.env, 'FOO')`, and a destructured method called with `.call` Since this works on linked values rather than syntax, it also covers `import { env } from 'node:process'`, aliases (`const e = process.env`), computed method names (`process.env['hasOwnProperty']`) and keys that resolve to a constant (`const key = 'FOO'`). Before this, these calls weren't understood: `FOO` wasn't listed at all. Only `hasOwnProperty` itself was recorded as a runtime env var read (from the `process.env.hasOwnProperty` member expression). ### Why? Code checks for an env var with `process.env.hasOwnProperty(name)` and its equivalents, so its result depends on whether `FOO` is set. Consumers of `EnvVarInfo.runtime` should see that dependency, consistently for all of these forms. ### How? - New well-known values: `WellKnownObjectKind::ObjectPrototype` (`Object.prototype`) and the functions `HasOwnProperty`, `PropertyIsEnumerable` (`Object.prototype` methods, also inherited by `process.env`), `ObjectHasOwn` and `FunctionCall` (`Function.prototype.call`, only for these methods). - What `hasOwnProperty` checks depends on its receiver (`this`). So `handle_call` now gets the receiver of a member call (`None` for a plain call) and passes it on to `handle_well_known_function_call`. `handle_call` already handles a callee with alternatives, so no extra alternatives handling is needed: - `obj.hasOwnProperty(key)`: the receiver `obj` must link to `process.env`. - `method.call(thisArg, ...args)`: `FunctionCall` calls its receiver `method` with `thisArg` as `this`, so it calls `handle_call` again with the linked `method`, receiver `thisArg` and the remaining arguments. `(0, method).call(...)` works the same way. This recursive call is treated like a call in an alternative, so it doesn't generate code. - A call without a receiver isn't an env var check: a detached `hasOwnProperty(key)` or `const call = Object.prototype.hasOwnProperty.call; call(process.env, key)` both throw at runtime. One exception is `(0, process.env.hasOwnProperty)(key)`, which also throws: the effects don't distinguish it from a member call, so the key is still listed, an unnecessary but harmless dependency. - `Object.hasOwn(obj, key)` doesn't depend on a receiver. - `process.env.hasOwnProperty` / `propertyIsEnumerable` resolve to alternatives of the function and an unknown value, because an env var with that name would shadow the method. `Object.hasOwn` is also an alternative with an unknown value, because it's missing in older runtimes. So conditions like `if (process.env.hasOwnProperty)`, `typeof process.env.hasOwnProperty` and `Object.hasOwn || fallback` aren't constant-folded. `Object.prototype.hasOwnProperty` / `propertyIsEnumerable` are assumed to exist (ES3). - The member read of `hasOwnProperty` / `propertyIsEnumerable` on `process.env` is still recorded as a potential env var read. - Unlike `'FOO' in process.env`, the call isn't replaced when `FOO` is a compile-time define, because it still runs against the runtime `process.env`. - Keys that don't link to a constant string are left as before: not tracked. #102093 builds on this and marks them as reading any env var. ### Verification - New `env-var-info/static-has-own-property` fixture with all the forms above (including `(0, process.env.hasOwnProperty).call(process.env, key)`), plus aliased `process.env` and `env` imported from `node:process`. It also has negatives that must not be listed: `hasOwnProperty` on an `env` import from another module, the same calls on an unrelated object, `process.env.hasOwnProperty.call(other, key)`, a detached `hasOwnProperty(key)` and a detached `.call`. Before this change (on `7239c80`), a temporary fixture showed that none of `Object.hasOwn`, `.call` or `propertyIsEnumerable` were listed, while the detached `hasOwnProperty(key)` was. - Existing cases in `static-existence` are unchanged. All three analysis modes give the same snapshots. - The `react-dom-production` analyzer graph snapshot changes because its `Object.prototype.hasOwnProperty.call(...)` calls now resolve to the well-known functions. The conditions using them stay unknown. - Turbopack execution test `basic/process-env-has-own-property`. It runs the bundled code in Node against the real `process.env`, using a synthetic key and an env var named `hasOwnProperty`. Their previous state is saved and restored afterwards. It covers: - the constant-key forms above, returning `false` and then `true` once the var is set (also to `''`) - a key the analyzer can't resolve (`JSON.parse(JSON.stringify(KEY))`) - an env var named `hasOwnProperty` set to `''`: `typeof` is `'string'`, and the conditional and `!` aren't folded at build time - `Object.hasOwn` deleted at runtime: `Object.hasOwn ? 'native' : 'fallback'` is `'fallback'` Two checks of the fold tests: - **`Object.hasOwn`:** with it mapped to the plain well-known function (no unknown alternative), the test failed (`Expected: "fallback"`, `Received: "native"`). - **Shadowing alternative:** temporarily removing it made the fold check fail (`Expected: "falsy"`, `Received: "truthy"`). This was checked on the first version of the test. `cargo test -p turbopack-tests --test execution basic__`: 43 passed. - A shadowed `process` (function parameter) and an unrelated `env` import aren't recorded. - `cargo test -p turbopack-ecmascript`: 638 passed. - `cargo clippy -p turbopack-ecmascript --all-targets`, `cargo fmt --check`, `prettier --check` on the fixtures <!-- NEXT_JS_LLM --> <!-- fleet 84a68acb-8a0a-4cf5-89c8-4dd63b2a53a6 --> --------- Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Author
Parents
Loading