Skip to content

fix(compiler): leave hook methods called during module init unslotted - #1572

Merged
trueadm merged 4 commits into
mainfrom
fix/module-init-hook-methods
Oct 2, 2026
Merged

trueadm merged 4 commits into
mainfrom
fix/module-init-hook-methods

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The plain .ts/.js hook pass wraps a hook-method call (obj.useX()) in withSlot(_h$N, () => obj.useX()), but it declares the _h$N constants after the module body so authored line numbers survive. A hook method evaluated while the module initializes therefore threw:

export const initial = store.useValue();
// -> export const initial = _$withSlot(_h$0, () => store.useValue());
// ReferenceError: Cannot access '_h$0' before initialization

This applies #1546's rule to hook methods: a call made during module initialization runs outside every render, so it needs no boundary.

  • hook-deps.js buildScopes stamps _octaneModuleInitCall on a hook-method call that withinFunction(scope) says is not inside a function. This happens in the plain pass only (onlyImported); the full .tsrx compiler declares its slots before the body and never had the bug.
  • hookMethodName (hook-methods.js) returns null for those calls, so the surgical emitter, the inlineHookMemo emitter, and optional-chain lowering all leave them authored.
  • Static fields, static blocks, computed keys, decorators and extends of a module-level class also run at module init, so they lose the boundary too.
  • An instance field (or accessor) initializer runs with each construction, possibly during a render. buildScopes now walks its value in a new 'initializer' scope, and withinFunction counts that scope, so the call keeps its boundary as a constructor body's would. That also covers fix(compiler): give each call to a module-local custom hook its own slots #1546's identifier custom-hook calls: on main, first = useCell('first') in an instance field got no boundary, so two such fields shared one state cell.

Validation

  • targeted tests, octane + octane-prod: transitive-hook-ownership, plain-hook-local-custom-hooks, plain-hook-import-alias, external-hook-slot, member-hook-boundary, compiler/plain-hook-memo-codegen, compiler/auto-hook-deps (444 passed), re-run on the final merge (74 passed)
  • pnpm format:files:check and pnpm typecheck:files on the changed files
  • pnpm sync leaves the tree clean
  • pnpm format:check / pnpm typecheck / pnpm test repo-wide: left to CI. A local full packages/octane/tests run under load average ~80 failed only on 5s–37s timeouts. The 16 affected files re-ran clean except one flaky native-condition-demand case, which passes 3/3 both with and without this change.

New regressions in packages/octane/tests/transitive-hook-ownership.test.ts, all using loadPlainHookFixtureSource:

  • evaluates hook methods called while the module initializes: client/server × surgical/inline, and it asserts the inline emitter was actually selected. The module evaluates, and each export equals the method's result: top level, optional chain (present and null), static field, static block, computed key, with this preserved.
    • Before the fix it failed in all 8 runs with the TDZ ReferenceError.
    • Re-enabling the boundary for each construct separately (static field, static block, computed key, optional chain) turns it red again.
  • keeps a boundary for cells.useCell() / useCell() in instance field initializers: renders a component through SSR and the client, then updates it. Dropping the 'initializer' scope makes it read firstfirst instead of firstsecond.

Risk / follow-ups

  • A module-scope IIFE ((() => store.useValue())()) still gets a boundary and hits the TDZ, for identifier and method calls alike. A separate session handles that and will stack on this branch.
  • This is compile-time only, with no runtime or bundle change; module-init calls just lose their slot. The extra compile cost is a few property checks per use* method call plus one scope object per instance field with an initializer. No benchmark was run.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Medium Risk
Compile-time hook slotting changes affect plain-TS modules and class field initializers; behavior is covered by new tests but edge cases like module-scope IIFEs remain unaddressed.

Overview
Fixes a temporal dead zone crash in the plain .ts/.js hook compiler when store.useX()-style hook methods run during module initialization. Those calls are no longer wrapped in withSlot(_h$N, …) because slot constants are emitted after the module body.

Scope analysis now tags module-init hook-method sites with _octaneModuleInitCall, and hookMethodName skips them so codegen leaves the call as authored. The same rule applies to top-level exports, optional chains, and module-level class static fields, static blocks, and computed keys.

Instance field / accessor initializers are walked in a dedicated initializer scope (counted like a function body), so hooks there still get per-construction slot boundaries—fixing shared state when two fields each call a local custom hook.

Reviewed by Cursor Bugbot for commit c1db09c. Bugbot is set up for automated code reviews on this repo. Configure here.

trueadm and others added 4 commits October 2, 2026 15:15
The plain .ts/.js hook pass wrapped every `obj.useX()` call in
`withSlot(_h$N, () => obj.useX())`, but declares its slot constants after
the module body to keep authored line numbers. A hook method evaluated
while the module initializes, e.g. `export const initial =
store.useValue()`, therefore threw `ReferenceError: Cannot access '_h$0'
before initialization`.

Such a call runs outside every render and needs no boundary. The scope
analysis now stamps `_octaneModuleInitCall` on a hook-method call with no
enclosing function (plain pass only; the full compiler declares its slots
before the body) and `hookMethodName` skips it, so the surgical and inline
emitters and optional-chain lowering all leave it authored. Static fields,
static blocks, computed keys and decorators of a module-level class run at
module init too. An instance field initializer runs with each
construction, possibly during render, so it gets an `initializer` scope
that keeps its boundary, as a constructor body does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…methods

# Conflicts:
#	packages/octane/src/compiler/hook-deps.js
The merge with #1546 shares the instance-field `initializer` scope with
its withinFunction, so a module-declared custom hook called from an
instance field also keeps its own slot.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 2, 2026
@github-actions
github-actions Bot marked this pull request as draft October 2, 2026 15:04
@trueadm
trueadm marked this pull request as ready for review October 2, 2026 15:05
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No size increases and no timing regressions outside noise.

Compares 3994e1ea0 with 3819f09f2 on the same runner. Timing verdicts require a change beyond the combined margin of error (at least 5%) and one 0.1ms timer tick, with the fastest sample also moving beyond that margin. Workflow run

js-framework: 1k/10k row operations (ms, lower is better)

All 20 timed operations (20 within noise)
target operation base head Δ noise
octane-tsrx run 4.69 7.82 +66.7% ±73% ⚪ within noise
octane-tsrx replace 11.46 11.65 +1.7% ±80% ⚪ within noise
octane-tsrx add 4.22 6.39 +51.4% ±55% ⚪ within noise
octane-tsrx update 0.93 0.88 −5.4% ±49% ⚪ within noise
octane-tsrx select 0.30 0.31 +3.3% ±59% ⚪ within noise
octane-tsrx swap 0.71 0.66 −7.0% ±46% ⚪ within noise
octane-tsrx remove 0.45 0.52 +15.6% ±65% ⚪ within noise
octane-tsrx runlots 46.60 52.45 +12.6% ±95% ⚪ within noise
octane-tsrx select_lots 0.24 0.28 +16.7% ±57% ⚪ within noise
octane-tsrx clear 50.12 42.78 −14.6% ±59% ⚪ within noise
octane-jsx run 10.83 9.16 −15.4% ±85% ⚪ within noise
octane-jsx replace 11.56 16.69 +44.4% ±72% ⚪ within noise
octane-jsx add 6.41 7.66 +19.5% ±103% ⚪ within noise
octane-jsx update 1.15 1.27 +10.4% ±61% ⚪ within noise
octane-jsx select 0.70 0.60 −14.3% ±76% ⚪ within noise
octane-jsx swap 1.17 1.06 −9.4% ±59% ⚪ within noise
octane-jsx remove 0.99 0.95 −4.0% ±64% ⚪ within noise
octane-jsx runlots 39.97 41.01 +2.6% ±37% ⚪ within noise
octane-jsx select_lots 4.11 3.30 −19.7% ±70% ⚪ within noise
octane-jsx clear 44.50 54.88 +23.3% ±56% ⚪ within noise

Application bundle size (bytes)

No changes across 45 measured values.

Public-import reachability bundles (bytes)

No changes across 105 measured values.

CI job durations

  • 🟡 6 job(s) slower than every recent main run: React parity shard (4/4), React parity shard (2/4), test shard (Node 24, 4/4), heavy integration (package builds, eval corpus, runtime loaders, Astro), example apps (2/3), example apps (3/3)

Compares the CI run for c1db09ce2 (run) with the last 5 successful main push runs. A job is only slower or faster outside the whole main range, by at least 10% and 15s. Job time excludes runner setup.

job main median main range this PR Δ
wall clock (first job start to last job end) 13m 00s 11m 55s–14m 59s 21m 35s +8m 35s (+66%) 🟡 slower
total job time 140m 58s 134m 18s–157m 45s 154m 31s +13m 33s (+10%) ⚪ within noise
React parity shard (4/4) 10m 53s 10m 08s–13m 14s 13m 24s +2m 31s (+23%) 🟡 slower
React parity shard (2/4) 10m 08s 10m 04s–11m 23s 12m 07s +1m 59s (+20%) 🟡 slower
test shard (Node 24, 4/4) 10m 26s 10m 21s–11m 11s 11m 46s +1m 20s (+13%) 🟡 slower
heavy integration (package builds, eval corpus, runtime loaders, Astro) 5m 23s 5m 21s–5m 53s 6m 14s +51s (+16%) 🟡 slower
example apps (2/3) 3m 44s 3m 42s–3m 55s 4m 34s +50s (+22%) 🟡 slower
example apps (3/3) 3m 30s 3m 22s–3m 49s 3m 53s +23s (+11%) 🟡 slower
All 26 timed jobs
job main median main range this PR Δ
React parity shard (4/4) 10m 53s 10m 08s–13m 14s 13m 24s +2m 31s (+23%) 🟡 slower
React parity shard (2/4) 10m 08s 10m 04s–11m 23s 12m 07s +1m 59s (+20%) 🟡 slower
test shard (Node 24, 2/4) 11m 55s 10m 38s–13m 38s 13m 19s +1m 24s (+12%) ⚪ within noise
test shard (Node 24, 4/4) 10m 26s 10m 21s–11m 11s 11m 46s +1m 20s (+13%) 🟡 slower
heavy integration (browser - chromium) 11m 40s 11m 03s–14m 20s 12m 53s +1m 13s (+10%) ⚪ within noise
heavy integration (package builds, eval corpus, runtime loaders, Astro) 5m 23s 5m 21s–5m 53s 6m 14s +51s (+16%) 🟡 slower
example apps (2/3) 3m 44s 3m 42s–3m 55s 4m 34s +50s (+22%) 🟡 slower
validate publishable packages 3m 58s 3m 51s–4m 54s 4m 41s +43s (+18%) ⚪ within noise
test shard (Node 24, 1/4) 11m 40s 11m 19s–12m 22s 12m 19s +39s (+6%) ⚪ within noise
React parity shard (1/4) 11m 18s 10m 23s–11m 27s 11m 54s +36s (+5%) ⚪ within noise
lint checks 8m 51s 7m 43s–12m 03s 9m 21s +30s (+6%) ⚪ within noise
test shard (Node 24, 3/4) 9m 33s 9m 29s–10m 46s 10m 02s +29s (+5%) ⚪ within noise
website integration suite 6m 06s 5m 43s–6m 49s 6m 30s +24s (+7%) ⚪ within noise
example apps (3/3) 3m 30s 3m 22s–3m 49s 3m 53s +23s (+11%) 🟡 slower
example apps (1/3) 3m 13s 3m 04s–3m 31s 3m 27s +14s (+7%) ⚪ within noise
React parity shard (3/4) 10m 25s 10m 07s–11m 31s 10m 38s +13s (+2%) ⚪ within noise
Three compatibility 2m 43s 2m 42s–3m 16s 2m 52s +9s (+6%) ⚪ within noise
Lynx compatibility 1m 32s 1m 29s–1m 36s 1m 40s +8s (+9%) ⚪ within noise
classify changeset release 13s 12s–15s 15s +2s (+15%) ⚪ within noise
examples 3s 3s–4s 4s +1s (+33%) ⚪ within noise
typecheck 3s 3s–4s 4s +1s (+33%) ⚪ within noise
CI provenance 3s 3s–4s 4s +1s (+33%) ⚪ within noise
typecheck checks 2m 13s 1m 56s–2m 40s 2m 13s ±0s (±0%) ⚪ within noise
test (24) 3s 2s–5s 3s ±0s (±0%) ⚪ within noise
lint 4s 3s–4s 3s −1s (−25%) ⚪ within noise
React parity checks 12s 10s–14s 11s −1s (−8%) ⚪ within noise

@trueadm
trueadm merged commit fdf54fe into main Oct 2, 2026
57 of 86 checks passed
@trueadm
trueadm deleted the fix/module-init-hook-methods branch October 2, 2026 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant