Repository navigation
fix(compiler): leave hook methods called during module init unslotted - #1572
Merged
Merged
Conversation
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]>
trueadm
marked this pull request as ready for review
October 2, 2026 15:05
3 of 4 tasks
Contributor
Benchmark report🟢 No size increases and no timing regressions outside noise. Compares js-framework: 1k/10k row operations (ms, lower is better)All 20 timed operations (20 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
Compares the CI run for
All 26 timed jobs
|
2 of 5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The plain
.ts/.jshook pass wraps a hook-method call (obj.useX()) inwithSlot(_h$N, () => obj.useX()), but it declares the_h$Nconstants after the module body so authored line numbers survive. A hook method evaluated while the module initializes therefore threw: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.jsbuildScopesstamps_octaneModuleInitCallon a hook-method call thatwithinFunction(scope)says is not inside a function. This happens in the plain pass only (onlyImported); the full.tsrxcompiler declares its slots before the body and never had the bug.hookMethodName(hook-methods.js) returns null for those calls, so the surgical emitter, theinlineHookMemoemitter, and optional-chain lowering all leave them authored.extendsof a module-level class also run at module init, so they lose the boundary too.accessor) initializer runs with each construction, possibly during a render.buildScopesnow walks its value in a new'initializer'scope, andwithinFunctioncounts 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
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:checkandpnpm typecheck:fileson the changed filespnpm syncleaves the tree cleanpnpm format:check/pnpm typecheck/pnpm testrepo-wide: left to CI. A local fullpackages/octane/testsrun under load average ~80 failed only on 5s–37s timeouts. The 16 affected files re-ran clean except one flakynative-condition-demandcase, which passes 3/3 both with and without this change.New regressions in
packages/octane/tests/transitive-hook-ownership.test.ts, all usingloadPlainHookFixtureSource:thispreserved.ReferenceError.cells.useCell()/useCell()in instance field initializers: renders a component through SSR and the client, then updates it. Dropping the'initializer'scope makes it readfirstfirstinstead offirstsecond.Risk / follow-ups
(() => 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.use*method call plus one scope object per instance field with an initializer. No benchmark was run.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith 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/.jshook compiler whenstore.useX()-style hook methods run during module initialization. Those calls are no longer wrapped inwithSlot(_h$N, …)because slot constants are emitted after the module body.Scope analysis now tags module-init hook-method sites with
_octaneModuleInitCall, andhookMethodNameskips 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
initializerscope (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.