Skip to content

fix(signals): re-run a local declaration that captures a later or reassigned variable - #1650

Merged
trueadm merged 2 commits into
mainfrom
fix/signal-captures-late-bindings
Oct 3, 2026
Merged

trueadm merged 2 commits into
mainfrom
fix/signal-captures-late-bindings

Conversation

@trueadm

@trueadm trueadm commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #1635, which lets a component-local derived$/query$ keep its accepted definition when the render values its closures capture are unchanged. The captured values are read where the declaration runs, but the closure runs later. Two kinds of capture can differ between those two points:

  • A var declared after the declaration. It is hoisted, so it is undefined at the declaration on every render. The captures always compared equal, so after the variable changed the declaration kept its first render's closure and value. A peer session found this while fixing the related .tsrx memo-region TDZ.
  • A declaration inside the initializer of a variable its closure reads, as in const api = { count$: derived$(() => api.items.length), items }. The emitted [api.items] is read while api is in its temporal dead zone, so rendering threw a ReferenceError.
  • A let or var assigned again after the declaration, as in let v = a; const d$ = derived$(() => v); v = b;. The captures held a while the closure reads b, so changing only b left the result stale.

#1635 already excluded a later let, const or class, which would throw in its temporal dead zone. This PR extends that guard to both cases above. A declaration that captures such a binding gets no captures list, so it reevaluates on every render, as it did before #1635.

Changes

  • hook-deps.js: lexicalDeclarationStarts becomes initializationEnds. It records where each binding finishes initializing: the end of its last declarator, now including var (a later var x = … assigns again), or the end of its class. A declaration inside its captured binding's own initializer therefore counts as late.
  • hook-deps.js: analyzeCallbackDependencies with evaluatedAt also reports null for a callback that captures a binding with reassigned set.
  • signal-local-selectors.test.ts: three plain-module hooks, useLaterVar$, useReassigned$ and useCount$, used by one component.
  • docs/signals.md: the sentence on unlisted declarations now names both cases.
  • Changeset.

Validation

  • pnpm format:check (scoped pnpm format:files:check on the changed files: clean)
  • pnpm typecheck (scoped pnpm typecheck:files on the changed files: clean)
  • pnpm test (not run locally; left to CI)
  • Targeted tests on this head, which contains main dc3e180236:
    • octane: signal-local-selectors, hydration/signal-local-selectors, compiler/signal-declarations, hmr.
    • octane-prod (prod and strong): the same files except compiler/signal-declarations.
    • octane-signals (dev), plus octane-signals-strong before the rebase onto current main: signals-hook-declarations, signals-local.
  • The new test fails on main: it shows >|a for the late var and reassignment cases, and throws ReferenceError: Cannot access 'api' before initialization for the self-initializer case. Removing each guard fails it too: >|b without var declarators, #|a without the reassigned check.
  • pnpm sync: no generated changes.

Risk / follow-ups

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
Changes compiler capture inference for all local signal declarations; the reassignment guard is conservative and may disable the fast path for locals assigned anywhere in scope.

Overview
Fixes stale component-local derived$ / query$ when capture-based memoization compared values at declaration time while the closure reads them later.

Compiler (hook-deps.js): analyzeCallbackDependencies no longer treats “declared after the call” as the only unsafe capture. It uses initializationEnds (end of the binding’s last initializer, including repeated var) and also invalidates the capture list when a dependency is reassigned anywhere in scope. Those declarations get null captures and reevaluate every render, matching pre-#1635 behavior and avoiding wrong reuse for later var, post-declaration assignment, and self-referential initializers (e.g. api.count$ inside api’s object literal).

Docs & tests: docs/signals.md documents the expanded cases; signal-local-selectors adds integration coverage for later var, reassigned let, and initializer-held declarations.

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

…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]>
@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:20
@trueadm
trueadm marked this pull request as ready for review October 3, 2026 09:20
@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 dc3e18023 with edb87e712 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.38 3.35 −0.9% ±9% ⚪ within noise
octane-tsrx replace 6.79 6.49 −4.4% ±14% ⚪ within noise
octane-tsrx add 3.20 3.23 +0.9% ±11% ⚪ within noise
octane-tsrx update 0.38 0.30 −21.1% ±56% ⚪ within noise
octane-tsrx select 0.14 0.13 −7.1% ±92% ⚪ within noise
octane-tsrx swap 0.35 0.30 −14.3% ±43% ⚪ within noise
octane-tsrx remove 0.29 0.31 +6.9% ±44% ⚪ within noise
octane-tsrx runlots 27.82 28.43 +2.2% ±10% ⚪ within noise
octane-tsrx select_lots 0.21 0.22 +4.8% ±36% ⚪ within noise
octane-tsrx clear 38.14 38.78 +1.7% ±7% ⚪ within noise
octane-jsx run 3.48 3.58 +2.9% ±13% ⚪ within noise
octane-jsx replace 6.56 6.57 +0.2% ±8% ⚪ within noise
octane-jsx add 3.07 3.08 +0.3% ±7% ⚪ within noise
octane-jsx update 0.54 0.56 +3.7% ±35% ⚪ within noise
octane-jsx select 0.31 0.37 +19.4% ±43% ⚪ within noise
octane-jsx swap 0.60 0.57 −5.0% ±28% ⚪ within noise
octane-jsx remove 0.52 0.52 −0.0% ±27% ⚪ within noise
octane-jsx runlots 27.88 27.74 −0.5% ±8% ⚪ within noise
octane-jsx select_lots 2.26 2.55 +12.8% ±23% ⚪ within noise
octane-jsx clear 39.10 40.28 +3.0% ±7% ⚪ within noise

Application bundle size (bytes)

No changes across 45 measured values.

Public-import reachability bundles (bytes)

No changes across 105 measured values.

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]>
@trueadm
trueadm merged commit e93bb26 into main Oct 3, 2026
53 of 57 checks passed
@trueadm
trueadm deleted the fix/signal-captures-late-bindings branch October 3, 2026 10:24
trueadm added a commit that referenced this pull request Oct 3, 2026
…es read a later binding (#1657)

* fix(signals): re-run a local declaration that captures a later or reassigned 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]>

* fix(signals): do not list a capture read inside its own initializer

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]>

* fix(compiler): run a hook on every render when its omitted dependencies 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]>

* fix(compiler): skip a null inferred list when marking stable memo dependencies

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]>
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