Skip to content

fix(compiler): run a hook on every render when its omitted dependencies read a later binding - #1657

Merged
trueadm merged 7 commits into
mainfrom
fix/hook-deps-later-binding
Oct 3, 2026
Merged

trueadm merged 7 commits into
mainfrom
fix/hook-deps-later-binding

Conversation

@trueadm

@trueadm trueadm commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Builds on #1650's initializationEnds helper. #1650 has merged and been merged into this branch, so the diff is now only this fix.

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:

export function App(props) @{
  useEffect(() => { console.log(prefix); }); // emitted as [prefix], read too early
  const prefix = props.prefix;
  <p>{'x'}</p>
}
  • A later let, const or class throws ReferenceError: Cannot access 'prefix' before initialization at render, on the client and during SSR.
  • A later var is still undefined at the call, so the inferred list never changes and the hook goes stale.
  • const f = useCallback(() => f()) reads f inside its own initializer and throws the same way.

useCallback and useMemo behave 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 writing null yourself.

  • hook-deps.js: a new lateDependencyFinder reuses fix(signals): re-run a local declaration that captures a later or reassigned variable #1650's initializationEnds, which records where each declarator or class ends, including the last var declarator. It compares only bindings declared in the hook's own function. A module let declared below a component is initialized long before the component renders, so it keeps its inferred list.
  • All four places that print an inferred list now emit null: the full compiler's rebuild (which also skips the native inferred-memo flag), its helper-import scan, the plain-TS surgical text pass in slot-hooks.js, and plain-hook-memo.js.
  • markStableMemoDependencies (added on main by perf(compiler): shrink plain-module hook boundaries and inline memo lowering #1634) skips a null list. Without that, compiling a plain-TS module with such a hook threw result.dependencies is not iterable; the plain-TS fixture caught it after the merge.
  • Strong mode already rejects an authored null list (OCTANE_STRONG_UNTRACKED_EFFECT) and bans useMemo/useCallback. Running the hook every render without saying so would contradict that, so analyzeStrongHookPolicies reports OCTANE_STRONG_UNTRACKED_EFFECT at the late read and asks for the declaration to come before the hook. It uses the same finder as inference.
  • Docs: a new row in the differences-from-react.md inference table and an updated OCTANE_STRONG_UNTRACKED_EFFECT row in strong-compiler-checks.md. Changeset: octane patch.

Validation

  • pnpm format:check (not run repo-wide; pnpm format:files:check passes on every changed file)
  • pnpm typecheck (not run; the change is compiler JS and test files, and tests sit outside pnpm typecheck. pnpm typecheck:files on the changed tests shows only the fixture's existing implicit-any errors and an existing TS7016)
  • pnpm test (not run locally, per the shared-machine load rule; left to CI)
  • targeted tests:
    • auto-hook-deps-behavior.test.ts in octane and octane-prod: 44/44 pass. The new cases mount useEffect, useCallback and useMemo with a later const, a self-referencing useCallback, a later var (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' for var), and both SSR cases failed with ReferenceError.
    • 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.ts and signal-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.
    • Equivalence: an omitted array compiles byte-identically to an authored null across .tsrx/.tsx, client/server and dev/prod/HMR (12 combinations, also with nativeReads). The one exception is source-hash binding ids, which differ because the source gains , null.
    • Corpus: main's compiler vs this one over 19,180 compiles (every tracked .tsrx/.tsx file × 3 modes, plus 553 plain-TS hook modules × 4 modes). The only outputs that changed are the new fixtures.
    • Compile time over the .tsrx corpus: median 10,732 ms vs 10,722 ms per pass on main, within noise. Runtime output for existing code is unchanged.

Risk / follow-ups

  • A let reassigned 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.
  • The check compares source order only inside the hook's own function. A custom hook defined inside a component and called before a declaration in the component can still throw. This case was already broken on main.

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
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/class TDZ, stale var, or a self-referencing useCallback), emitted code could throw at render or never re-run when that value changed.

Inference now uses lateDependencyFinder (with initializationEnds) to detect same-function bindings initialized after the call. In that case inferred dependencies become null, so the hook runs every render—matching React for an omitted array. All emission paths (applyHookDependencies, slot-hooks, plain-hook-memo) synthesize null instead of a broken array; null lists are skipped for stable-memo marking and native inferred memo.

Strong mode reports OCTANE_STRONG_UNTRACKED_EFFECT when 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.

trueadm and others added 3 commits October 3, 2026 10:19
…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]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 3, 2026
@github-actions
github-actions Bot marked this pull request as draft October 3, 2026 09:59
@trueadm
trueadm marked this pull request as ready for review October 3, 2026 10:01
@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No size increases and no timing regressions outside noise.

Compares 950ef0b0b with d1fa64f63 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 3.34 3.43 +2.7% ±11% ⚪ within noise
octane-tsrx replace 6.91 6.75 −2.3% ±15% ⚪ within noise
octane-tsrx add 3.29 3.45 +4.9% ±19% ⚪ within noise
octane-tsrx update 0.33 0.34 +3.0% ±28% ⚪ within noise
octane-tsrx select 0.14 0.20 +42.9% ±90% ⚪ within noise
octane-tsrx swap 0.35 0.37 +5.7% ±50% ⚪ within noise
octane-tsrx remove 0.38 0.42 +10.5% ±51% ⚪ within noise
octane-tsrx runlots 27.67 29.86 +7.9% ±24% ⚪ within noise
octane-tsrx select_lots 0.20 0.23 +15.0% ±64% ⚪ within noise
octane-tsrx clear 44.25 43.26 −2.2% ±8% ⚪ within noise
octane-jsx run 3.54 3.61 +2.0% ±8% ⚪ within noise
octane-jsx replace 7.13 7.76 +8.8% ±40% ⚪ within noise
octane-jsx add 3.14 3.25 +3.5% ±10% ⚪ within noise
octane-jsx update 0.53 0.61 +15.1% ±28% ⚪ within noise
octane-jsx select 0.34 0.41 +20.6% ±41% ⚪ within noise
octane-jsx swap 0.57 0.57 +0.0% ±25% ⚪ within noise
octane-jsx remove 0.55 0.59 +7.3% ±31% ⚪ within noise
octane-jsx runlots 27.36 27.51 +0.5% ±5% ⚪ within noise
octane-jsx select_lots 2.53 2.53 +0.0% ±23% ⚪ within noise
octane-jsx clear 46.32 45.62 −1.5% ±14% ⚪ within noise

Application bundle size (bytes)

No changes across 45 measured values.

Public-import reachability bundles (bytes)

No changes across 105 measured values.

trueadm and others added 3 commits October 3, 2026 11:26
…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]>
@trueadm
trueadm merged commit 105e0f0 into main Oct 3, 2026
30 checks passed
@trueadm
trueadm deleted the fix/hook-deps-later-binding branch October 3, 2026 10:54
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