Repository navigation
fix(signals): re-run a local declaration that captures a later or reassigned variable - #1650
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]>
trueadm
marked this pull request as ready for review
October 3, 2026 09:20
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. |
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]>
Merged
2 of 5 tasks
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]>
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
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:vardeclared after the declaration. It is hoisted, so it isundefinedat 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.tsrxmemo-region TDZ.const api = { count$: derived$(() => api.items.length), items }. The emitted[api.items]is read whileapiis in its temporal dead zone, so rendering threw aReferenceError.letorvarassigned again after the declaration, as inlet v = a; const d$ = derived$(() => v); v = b;. The captures heldawhile the closure readsb, so changing onlybleft the result stale.#1635 already excluded a later
let,constorclass, 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:lexicalDeclarationStartsbecomesinitializationEnds. It records where each binding finishes initializing: the end of its last declarator, now includingvar(a latervar 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:analyzeCallbackDependencieswithevaluatedAtalso reportsnullfor a callback that captures a binding withreassignedset.signal-local-selectors.test.ts: three plain-module hooks,useLaterVar$,useReassigned$anduseCount$, used by one component.docs/signals.md: the sentence on unlisted declarations now names both cases.Validation
pnpm format:check(scopedpnpm format:files:checkon the changed files: clean)pnpm typecheck(scopedpnpm typecheck:fileson the changed files: clean)pnpm test(not run locally; left to CI)maindc3e180236: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), plusoctane-signals-strongbefore the rebase onto currentmain: signals-hook-declarations, signals-local.main: it shows>|afor the latevarand reassignment cases, and throwsReferenceError: Cannot access 'api' before initializationfor the self-initializer case. Removing each guard fails it too:>|bwithoutvardeclarators,#|awithout thereassignedcheck.pnpm sync: no generated changes.Risk / follow-ups
.tsrxauto-calculation TDZ fix is folded into fix(compiler): cache a const that only another cached calculation reads #1625. Main's auto-calculation cache has the same staleness for a capture reassigned after the call (let v = props.a; const reader = make(() => v); v = props.b;caches on[v]read before the reassignment). That case has been forwarded to the fix(compiler): cache a const that only another cached calculation reads #1625 session; this PR does not change that cache.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith 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):analyzeCallbackDependenciesno longer treats “declared after the call” as the only unsafe capture. It usesinitializationEnds(end of the binding’s last initializer, including repeatedvar) and also invalidates the capture list when a dependency isreassignedanywhere in scope. Those declarations getnullcaptures and reevaluate every render, matching pre-#1635 behavior and avoiding wrong reuse for latervar, post-declaration assignment, and self-referential initializers (e.g.api.count$insideapi’s object literal).Docs & tests:
docs/signals.mddocuments the expanded cases;signal-local-selectorsadds integration coverage for latervar, reassignedlet, and initializer-held declarations.Reviewed by Cursor Bugbot for commit bcaf287. Bugbot is set up for automated code reviews on this repo. Configure here.