Repository navigation
fix(compiler): run a hook on every render when its omitted dependencies read a later binding - #1657
Merged
Merged
Conversation
…ssigned variable #1635 compares the render values a local derived$ or query$ captures where the declaration runs. A var declared after the declaration is still undefined there, and a let or var assigned again holds an earlier value than the one the lazy closure reads. Both compared equal across renders, so the declaration kept its first render's closure after the variable changed. Capture analysis now records every variable declaration, using a repeated var's last one, and does not list a callback that captures a reassigned binding. Such declarations reevaluate on every render as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A declaration inside the initializer of a variable its closure reads, such
as `const api = { count$: derived$(() => api.items.length), items }`, had
`[api.items]` listed. The list is read where the declaration runs, while
`api` is still in its temporal dead zone, so rendering threw. Capture
analysis now compares each binding's initialization end, the end of its
last declarator or class, so such a declaration keeps no list.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…es read a later binding An omitted dependency array is inferred from the callback's captures and read where the hook is called. A capture that the component initializes after the call was read too early: a later let, const or class threw a temporal-dead-zone ReferenceError at render, a later var was still undefined so the hook never re-ran, and `const f = useCallback(() => f())` read f inside its own initializer. React runs a hook with an omitted array on every render, so inference now emits null for such a hook. lateDependencyFinder compares only bindings of the hook's own function, using #1650's initializationEnds, so a module let declared below a component keeps its list. All four places that print an inferred list emit null. Strong mode rejects an authored null list, so it reports OCTANE_STRONG_UNTRACKED_EFFECT at the late read instead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
trueadm
marked this pull request as ready for review
October 3, 2026 10:01
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. |
…inding # Conflicts: # packages/octane/src/compiler/hook-deps.js
…endencies markStableMemoDependencies, added on main by #1634, walks every inferred dependency list on the plain-module path. A hook whose omitted list reads a later binding now infers null, so the walk threw "result.dependencies is not iterable" while compiling such a module. A null list has no dependencies to mark. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 3, 2026
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
Bug (on
main). When a hook's dependency array is omitted, the compiler infers it from the callback's captures and reads it where the hook is called. If a capture is a binding the component initializes after that call, the read happens too early:let,constorclassthrowsReferenceError: Cannot access 'prefix' before initializationat render, on the client and during SSR.varis stillundefinedat the call, so the inferred list never changes and the hook goes stale.const f = useCallback(() => f())readsfinside its own initializer and throws the same way.useCallbackanduseMemobehave the same way. Plain React accepts this source, because an omitted array means "run every render".Fix. When any inferred dependency would be read before its binding is initialized, inference emits
null, so the hook runs on every render. That is React's behavior for an omitted array. The output is byte-identical to writingnullyourself.hook-deps.js: a newlateDependencyFinderreuses fix(signals): re-run a local declaration that captures a later or reassigned variable #1650'sinitializationEnds, which records where each declarator or class ends, including the lastvardeclarator. It compares only bindings declared in the hook's own function. A moduleletdeclared below a component is initialized long before the component renders, so it keeps its inferred list.null: the full compiler's rebuild (which also skips the native inferred-memo flag), its helper-import scan, the plain-TS surgical text pass inslot-hooks.js, andplain-hook-memo.js.markStableMemoDependencies(added onmainby perf(compiler): shrink plain-module hook boundaries and inline memo lowering #1634) skips anulllist. Without that, compiling a plain-TS module with such a hook threwresult.dependencies is not iterable; the plain-TS fixture caught it after the merge.nulllist (OCTANE_STRONG_UNTRACKED_EFFECT) and bansuseMemo/useCallback. Running the hook every render without saying so would contradict that, soanalyzeStrongHookPoliciesreportsOCTANE_STRONG_UNTRACKED_EFFECTat the late read and asks for the declaration to come before the hook. It uses the same finder as inference.differences-from-react.mdinference table and an updatedOCTANE_STRONG_UNTRACKED_EFFECTrow instrong-compiler-checks.md. Changeset:octanepatch.Validation
pnpm format:check(not run repo-wide;pnpm format:files:checkpasses on every changed file)pnpm typecheck(not run; the change is compiler JS and test files, and tests sit outsidepnpm typecheck.pnpm typecheck:fileson the changed tests shows only the fixture's existing implicit-any errors and an existingTS7016)pnpm test(not run locally, per the shared-machine load rule; left to CI)auto-hook-deps-behavior.test.tsinoctaneandoctane-prod: 44/44 pass. The new cases mountuseEffect,useCallbackanduseMemowith a laterconst, a self-referencinguseCallback, a latervar(effect and callback), a plain-TS custom hook, and server-render the fixtures in dev and prod. Before the fix, all 7 mount cases failed in both projects (ReferenceError, or a stale'a'forvar), and both SSR cases failed withReferenceError.compiler/strong-hook-policy.test.ts: 5 new cases fail before the fix and pass after. Each also checks that putting the declaration first compiles.compiler/auto-hook-deps.test.tsandsignal-local-selectors.test.ts(fix(signals): re-run a local declaration that captures a later or reassigned variable #1650's tests on the shared helper) pass. All 162 tests across the four files pass.nullacross.tsrx/.tsx, client/server and dev/prod/HMR (12 combinations, also withnativeReads). The one exception is source-hash binding ids, which differ because the source gains, null..tsrx/.tsxfile × 3 modes, plus 553 plain-TS hook modules × 4 modes). The only outputs that changed are the new fixtures..tsrxcorpus: median 10,732 ms vs 10,722 ms per pass on main, within noise. Runtime output for existing code is unchanged.Risk / follow-ups
letreassigned after the hook call still goes stale:let label = props.a; useEffect(…); label = props.b;infers[label]from the earlier value. This is a separate defect and is tracked as its own task.main.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 core hook dependency inference and codegen across client/server and Strong mode; behavior change is intentional and aligned with React, but any code that relied on the previous broken inference could run effects more often.
Overview
Fixes a compiler bug where omitted hook dependency arrays were inferred from callback captures but evaluated at the hook call site. If the callback closed over a binding declared after the hook (
const/let/classTDZ, stalevar, or a self-referencinguseCallback), emitted code could throw at render or never re-run when that value changed.Inference now uses
lateDependencyFinder(withinitializationEnds) to detect same-function bindings initialized after the call. In that case inferred dependencies becomenull, so the hook runs every render—matching React for an omitted array. All emission paths (applyHookDependencies,slot-hooks,plain-hook-memo) synthesizenullinstead of a broken array;nulllists are skipped for stable-memo marking and native inferred memo.Strong mode reports
OCTANE_STRONG_UNTRACKED_EFFECTwhen an omitted list would imply every-render behavior because of a late read, and docs/changeset describe the rule. Tests cover effects, memos, callbacks,var, external hooks, SSR, and Strong rejection.Reviewed by Cursor Bugbot for commit 95b9183. Bugbot is set up for automated code reviews on this repo. Configure here.