Repository navigation
fix(compiler): give each call to a module-local custom hook its own slots - #1546
Merged
Merged
Conversation
…lots The plain `.ts`/`.js` hook pass wrapped only imported custom-hook calls in `withSlot`. A hook calling another hook declared in the same module got no boundary, so two calls shared their base-hook cells on the client: `useState`, `useRef`, `useId` and memos collided. The server kept them apart by occurrence index, so SSR rendered `a,b`, the client `a,a`, and hydration replaced the server output. `hook-deps.js` now annotates calls to any hook-named binding the module declares, matching `.tsrx`'s by-name rule while keeping lexical provenance. That covers functions, values, parameters, and `const` aliases of them. Imports, unbound globals, optional calls, `useContext`, and differently named helpers are unchanged. Both emitters already consume the annotation. Two guards come with it. Calls evaluated while the module initializes stay unwrapped: the trailing slot constants are not initialized yet, which already threw a ReferenceError for imported hooks. The inline memo tier also treats a slotted custom-hook call as hook-shaped, so a factory containing one keeps the runtime `useMemo`. This fixes collisions in existing bindings, including mantine-hooks `useFloatingWindow` (every callback ref resolved to `onDragEnd`) and redux-toolkit `buildHooks`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
trueadm
marked this pull request as ready for review
October 2, 2026 08:27
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
|
trueadm
added a commit
that referenced
this pull request
Oct 2, 2026
…nals #1546 gave module-local custom-hook calls a withSlot boundary, but the plain pass still walks only modules that import from `octane`. A `hooks.ts` that imports `query$` from `octane/signals` and composes its own hooks (`useUsers$` calling `useUser$` twice) therefore got no boundary, and both calls shared one cell. A runtime signals import makes the module Octane's, so its imported or declared `use*` hooks are now walked too. A module that only declares signals is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
trueadm
added a commit
that referenced
this pull request
Oct 2, 2026
…1553) * fix(octane): give each custom-hook call its own signal declarations A `signal$`, `derived$` or `query$` declared inside a custom hook was keyed only by its component instance and declaration site, so two calls of one hook in one component aliased a single cell: `useUser$(a)`/`useUser$(b)` rendered `X|X` and ran one loader. Instance declarations now fold in the compiler-assigned call sites of the enclosing custom-hook calls. Runtime hook-slot numbers follow module evaluation order and differ between server and browser, while declaration keys are wire identity for seeds, streamed results and early values, so each signal-aware custom-hook call gets a position-hashed `h:` site registered on its slot Symbol (tsrx, plain-module surgical and inline-memo paths). Sites are emitted only for DOM targets, in modules with a runtime signals import or for `$` hooks. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(compiler): key aliased `$` hook calls the same way in every module kind A `.tsrx` module checked only the local callee for a trailing `$`, while plain modules checked only the imported name. `import { useUser$ as useUser }` in a component module without a signals import therefore still shared one cell across calls. All three emitters now register a call site when either the local or the imported hook name ends in `$`; the `.tsrx` path reads the exported name from the authored imports that its server and client compiles share. Co-Authored-By: Claude Opus 5.5 <[email protected]> * test(octane): re-select only the changed call of a repeated query$ hook Pins the combination with #1550: each custom-hook call owns its own query, so a new captured argument re-selects that call's query and leaves the other call's result in place. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(compiler): bound hook calls in plain modules that import only signals #1546 gave module-local custom-hook calls a withSlot boundary, but the plain pass still walks only modules that import from `octane`. A `hooks.ts` that imports `query$` from `octane/signals` and composes its own hooks (`useUsers$` calling `useUser$` twice) therefore got no boundary, and both calls shared one cell. A runtime signals import makes the module Octane's, so its imported or declared `use*` hooks are now walked too. A module that only declares signals is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
This was referenced Oct 2, 2026
trueadm
added a commit
that referenced
this pull request
Oct 2, 2026
…#1572) * fix(compiler): leave hook methods called during module init unslotted 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]> * chore(changeset): note instance-field boundaries for local custom hooks 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]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
trueadm
added a commit
that referenced
this pull request
Oct 2, 2026
…FEs (#1574) * fix(compiler): leave hook methods called during module init unslotted 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]> * chore(changeset): note instance-field boundaries for local custom hooks 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]> * fix(compiler): treat functions invoked in place at module init as module init A module-scope IIFE runs during module initialization, but its body is a function scope, so its hook-method and custom-hook calls still got a withSlot(_h$N, ...) boundary and the module threw a TDZ ReferenceError on load. A call whose callee is an arrow or function expression (directly, through TypeScript or paren wrappers, or via .call/.apply) now marks that function's scope invokedInPlace, and withinFunction walks through it. An IIFE inside a component or hook still has that function above it and keeps its boundary. Generator bodies run on .next(), possibly during a render, so they do not count. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
4 of 7 tasks
trueadm
added a commit
that referenced
this pull request
Oct 3, 2026
…owering (#1634) Production builds of plain .ts/.js hook modules grew with #1546 (a withSlot boundary per local custom-hook call) and #1577 (the inline memo tier admitted ~99% of modules). This trims both without changing behavior: - hook-deps.js omits a module-declared hook's call boundary when the hook reads no slot (it calls nothing but useContext and other such hooks), or when it is the hook's only use, made directly by another local hook that is used only by name, outside any cycle of such calls. Signal-reading modules and `$` hooks keep every boundary. - memoSlot receives the hook name only for an authored slot, which a manual hook may leave undefined; a compiler-appended slot always resolves. - A plain-module memo dependency that is a never-reassigned local binding is read in place instead of through a temporary. Imports stay copied: they are live bindings the factory can change. - The inline tier's admission and print move into createPlainProgramPrinter, and a corpus test reprints every Octane module in packages/*/src through it, comparing TypeScript syntax trees and requiring TypeScript and esbuild to parse the output, so an esrap gap fails CI instead of shipping to production only. Co-authored-by: Claude Opus 5.5 <[email protected]>
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
In a plain
.ts/.jsmodule, a custom hook that calls another hook declared in the same module now gives each call its own state, as a.tsrxmodule does.Previously
usePair('a', 'b')returneda,aon the client. The plain-module hook pass (slot-hooks.js, andplain-hook-memo.jsfor inline production memos) wrapped only imported custom-hook calls inwithSlot. Both local calls therefore resolved theiruseState,useRef,useIdand memo cells to one slot. The server's occurrence-indexed hook records kept them apart (a,b), so SSR→hydrate disagreed and hydration replaced the server output with mismatch diagnostics.hook-deps.jsnow annotates a call to any hook-named binding the module declares (_octaneCustomHookCall). Both emitters already consume that annotation. This follows.tsrx's by-name rule while keeping the plain pass's lexical provenance:function useX,const useX = …, hook-named parameter,constalias of oneuseContext, type-only importTwo guards come with the change:
use*()call evaluated while the module loads read them before initialization. That threw aReferenceErrorfor imported hooks before this change, and would now have hit local ones too.withSlot(slot, hook, …)no longer looks like a hook call by name.isHookShapedCallnow honors the copied annotation, so auseMemofactory that calls a custom hook keeps the runtimeuseMemo, as the inline tier's policy requires.Existing collisions this fixes
18 of 1,868 plain package modules change output (
.tsrxoutput is identical for all 1,268 package sources). At least two had live collisions:@octanejs/mantine-hooksuseFloatingWindowcalls a localuseRefValuefour times, so every callback ref resolved toonDragEnd. A drag reportedend, end, end, andenabled: falsewas ignored. A new test in that package covers it.@octanejs/redux-toolkitbuildHookscalls a localuseShallowStableValuethree times in one hook.Validation
pnpm format:files:checkon every changed filepnpm format:check(repo-wide, not run)pnpm typecheck(full chain not run). Ran the parts that cover this diff:tsgo -p packages/octane/tsconfig.jsonand both mantine-hooks configs are clean.pnpm typecheck:filesreports nothing new on changed lines.pnpm test(not run as one invocation). Ranoctane+octane-prodin full, 36,061 tests. Every failure caused by this change was fixed. The remaining failures inbehavior-root,hmr,hydration-ssr-diagnosticsandvolarare timeouts under machine load (load average 45–79). Their compiled output is byte-identical tomain(no.tsrxtest fixture changes output);hmrandhydration-ssr-diagnosticspassed when run alone.plain-hook-local-custom-hooks.test.ts: every binding kind called twice, dev and prod, surgical and inline emitters, SSR→hydrate (same nodes, no diagnostics, live updates), and module-initialization calls on client and servercompiler/plain-hook-memo-codegen.test.tsenabledtestmain. Breaking each part of the fix on its own (local rule, alias rule, module-init guard, hook-shape check) fails its intended test.mainand by this branch, a local 4-hook helper called once per render (pure overhead, no collision), 20,000 renders × 12 alternating rounds. The extra boundary costs +35 to +51 ns per render (2.6–3.9% of a render that does nothing else). That is the same boundary imported and.tsrxcustom hooks already pay. The runtime is unchanged, so thehooks-runtimeratios are unaffected.Updated existing tests
Five tests pinned the old "local calls are never wrapped" behavior:
conditional-effect-helpers.tsandactivity-dom-insertion-helpers.tsrelied on two local calls sharing one slot to exercise repeated enqueues through one effective slot. They now reach that state with a plain-module loop, which repeats one call site, so that runtime coverage is kept.external-hook-slot,auto-hook-depsandplain-hook-import-aliasdrop their "not wrapped" assertions. Their real contracts are still asserted: the module's bytes are restored once slots are stripped,use()arguments are not memoized around hooks, plain-module wrapper calls get no inferred dependency arrays (asdocs/differences-from-react.mddocuments), and shadowed or type-only imports gain no hook identity.Risk / follow-ups
withSlotboundary, including calls that never collided..tsrx. Restricting this to declared functions would be a one-line change.export const v = store.useValue()) still hits the same TDZ throughhook-methods.js. It predates this change and is filed as a separate task.signal$orquery$inside a local hook called twice) still alias onmain, becausei:keys don't include custom-hook call sites. Separate in-progress work adds those sites to the_octaneCustomHookCallbranches. With this change, local calls take those branches. Applied together locally, a locally composedsignal$hook keeps its cells per call through mount and hydration (a,b,c,d). Without this change it rendersa,a,c,c.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.Note
Medium Risk
Touches the plain-module hook transform used broadly (18 modules change output); behavior fix is correct but adds a withSlot boundary on every local custom-hook call, with compiler/runtime hydration alignment as the main concern.
Overview
Plain
.ts/.jsmodules now give each call to a module-local custom hook its own hook slots, matching.tsrxbehavior. The compiler annotates calls to hook-named bindings declared in the same module (functions,consthooks, parameters, aliases) with_octaneCustomHookCall, and the surgicalslot-hooks/plain-hook-memopasses wrap those sites inwithSlot—previously only imported custom hooks got that boundary, so repeated local calls shareduseState,useRef,useId, and memo state on the client while SSR kept them separate, causing hydration mismatches.Guards: hook calls at module initialization (outside any function) are not rewritten, avoiding
ReferenceErrorfrom slot constants defined after the module body.isHookShapedCalltreats annotated custom-hook boundaries as hook-shaped souseMemofactories that call local hooks still follow inline memo policy.Tests: new
plain-hook-local-custom-hooks.test.ts(independence, SSR→hydrate, module-scope calls);mantine-hooksdrag/enabledcoverage foruseFloatingWindow’s localuseRefValue; several compiler tests updated to expectwithSlotaround local hooks while still asserting wrappers don’t get inferred dependency arrays.Reviewed by Cursor Bugbot for commit 64707dd. Bugbot is set up for automated code reviews on this repo. Configure here.