Turbopack: resolve globalThis.process and global.process in the analyzer (#102240)
### What?
Turbopack's static analyzer now treats `globalThis` and `global` as
well-known objects, and resolves `globalThis.process` / `global.process`
to the same well-known `process` object as the bare `process` global,
when it is used:
- `globalThis.process.env.NODE_ENV` / `global.process.env.NODE_ENV` are
inlined like `process.env.NODE_ENV` (same for other `process` defines
such as `process.turbopack`, and for `in` / truthiness checks on env
vars).
- `globalThis.process.env.FOO` is tracked as a runtime env var
read/existence check, like `process.env.FOO`.
- Aliases work too, e.g. `const p = globalThis.process; p.env.NODE_ENV`.
### Why?
Isomorphic code frequently reaches `process` via `globalThis` (or
`global` in Node.js code). Previously `globalThis` resolved to an
unknown free variable, so such accesses missed compile-time inlining and
env var tracking that the bare form gets.
### How?
- Two new well-known object kinds, one for `globalThis` and one for
`global`. They keep their own definable names, so existing behavior that
depends on those names is unaffected: the `global` → runtime global
rewrite, `typeof global` inlining, and user defines keyed on
`globalThis.X` / `global.X`.
- `globalThis.process` is only resolved when it is *used*: as the object
of a member access or member call (`globalThis.process.env`,
`globalThis.process.cwd()`) or as a callee. Using it would throw if it
didn't exist, so assuming it's the global is safe there. The bare
`globalThis.process` value is left as is, because well-known values are
always truthy and `globalThis.X` is the standard way to feature-detect a
global that might not exist: `if (globalThis.process) { … } else { … }`
must keep its fallback branch.
- Because the used `globalThis.process` resolves to the existing
`process` well-known object, define inlining and env var tracking work
through the existing definable-name paths without any special casing.
- The mapping of bare globals (`process`, `URL`, `Worker`, …) moved into
a shared helper used by both the module analysis visitor and the
analyzer test visitor, replacing two duplicated mappings.
- Other members (`globalThis.Worker`, `globalThis.URL`,
`globalThis.Buffer`, …) are not resolved yet; there's a TODO to extend
the "resolve on use" handling to them. Unknown members stay plain member
expressions, so they remain definable.
### Verification
- New tests:
- Execution test cases in `turbopack-tests` `basic/comptime`: `NODE_ENV`
and `process.turbopack` through `globalThis`, `global` and an alias
variable, plus feature-detection cases (`globalThis.Worker ? … : …`,
`global.SharedWorker ? … : …`, `if (globalThis.process)`) that check
fallback branches are kept. The feature-detection case fails with an
implementation that resolves `globalThis.X` eagerly.
- Execution test asserting that for every well-known global X
(`process`, `Object`, `Buffer`, `navigator`, `URL`, `Worker`,
`SharedWorker`), `if (globalThis.X)`, `globalThis.X ? … : …`, `typeof X`
and `typeof globalThis.X` stay runtime checks: X is temporarily replaced
on the global object with `undefined` and with a function, and the
results must follow. It also fails with the eager implementation.
- Env var tracking fixture `env-var-info/global-this` (reads incl. via
alias, existence checks, inlined vars excluded, shadowed local
`globalThis` excluded).
- Existing analyzer graph snapshots updated: bare `global` /
`globalThis` now explain as well-known objects instead of unknown free
vars.
- `cargo test -p turbopack-ecmascript`, `cargo test -p turbopack-tests
--test execution`, `cargo test -p turbopack-tests --test snapshot`
(existing tests, no snapshot outputs changed).
- `cargo clippy -p turbopack-ecmascript -p turbopack-tests --all-targets
-- -D warnings -A deprecated`, `cargo fmt --all -- --check`.
- Not run: Next.js e2e tests with a rebuilt native binding.
<!-- NEXT_JS_LLM -->
<!-- fleet d95c47b8-5ddb-4813-86fa-94d53ba49412 -->
---------
Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>