Skip to content

fix(compiler): leave optional-chain arguments unread in inferred dependencies - #1813

Merged
trueadm merged 1 commit into
mainfrom
fix/optional-chain-skipped-deps
Oct 6, 2026
Merged

trueadm merged 1 commit into
mainfrom
fix/optional-chain-skipped-deps

Conversation

@trueadm

@trueadm trueadm commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Dependency arrays no longer read values that an optional chain skips. This covers inferred hook dependencies (collectDependencies) and compiler-memoized use() arguments and SSR prop creations (collectDepPaths).
  • With run and options both undefined, run?.(options.label) ?? 'idle' and value?.[options.key] now render idle instead of throwing. An options.label getter is no longer called before the authored code would call it.

Fixes #1799

Why

An optional chain evaluates the arguments and computed keys after its optional link only once the receiver is non-nullish. Both collectors walked them as unconditional reads. On main, the issue's example compiles to these dependency arrays:

Site main This PR
SSR prop memo run?.(options.label) ?? 'idle' [run, options.label] [run, __methodDep(options, "label", true)]
useMemo(() => value?.[options.key]) [value, options.key] [value, __methodDep(options, "key", true)]
useMemo(() => value?.list.at(options.index)) [value?.list, options.index] [value?.list, __methodDep(options, "index", true)]

Each main array throws Cannot read properties of undefined (reading 'label') when options is undefined, whether during SSR or in a client or plain-module hook.

Changes

  • hook-deps.js: a new followsOptionalLink(node) checks whether a call or member comes after an optional link. It walks the callee/object spine through TSNonNullExpression and stops at a parenthesized chain. collectDependencies walks those arguments and computed keys under the existing guard, so skipped one-level reads become the guarded descriptor probe from fix: preserve guarded property reads in inferred hook dependencies #1123. Receiver reads stay precise. The existing promotion still turns a guarded probe back into a plain dependency when the same path is also read unconditionally.
  • compile.js, collectDepPaths:
    • Skipped arguments and keys are counted separately from deferred ones. One-level member reads in skipped positions emit __methodDep(root, name, true), through a collision-safe private alias (requireRuntimeForContext now allocates __methodDep like the other private helpers).
    • Ambient globals in skipped positions contribute no dependency, the same rule deferred reads follow.
    • Reads inside callbacks keep their existing root?.prop form.
    • Any other read of the same path replaces an earlier probe, so a getter read both inside run?.(…) and after ?? keeps its value dependency.
  • docs/differences-from-react.md: the guarded-read paragraph now names optional-chain arguments and keys, and notes that use() and SSR prop creations follow the same rule.

Validation

  • pnpm format:check: not run repo-wide. pnpm format:files:check passes on every changed file.
  • pnpm typecheck: not run repo-wide. CI's typecheck excludes packages/octane/tests. pnpm typecheck:files on the three changed test files reports only implicit-any bindings in the .tsrx fixture, which its existing components on main also report.
  • pnpm test: not run in full; CI runs it.
  • Targeted tests in the octane project: 12 files, 2,075 tests pass. They are guarded-hook-dependencies, hydration/guarded-hook-dependencies, creation-dependencies, auto-hook-deps-behavior, signal-local-selectors, ssr-prop-flow-promises, ssr-parallel-use, and the compiler suites auto-hook-deps, plain-hook-memo-fidelity, strong-mode, strong-state, and inline-hook-memo-codegen. After rebasing onto 4b46332e26, the 3 owning files (664 tests) pass again.
  • Pre-fix failures: with the source change reverted, all 8 new hook and hydration tests fail with the issue's TypeError, and 20 of the 24 new creation cases fail. The 4 that pass are .tsx client use() arguments, which are not memoized.
  • Mutation checks:
    • Emitting options?.label instead of the probe fails the throwing-getter SSR test (Error: skipped optional-chain read).
    • Removing probe promotion fails the getter-precision test (expected 'first' to be 'second').
  • Corpus: 12,588 compiles cover every tracked .tsrx in client and server, dev and prod. Compared with main, only the new fixture's 4 outputs changed. Compile time was dominated by machine load, so this PR makes no timing claim. The added work is a short walk down the chain, and only for calls with arguments and computed members.

New coverage:

  • Client tests use undefined and null receivers, a throwing options.label getter, transitions to and from a receiver, and a field change on the same options object. They run against plugin-compiled, dev and prod compiled, and plain-hook builds.
  • SSR tests cover prop memos and hooks in dev and prod. Plugin and production clients adopt the server output and then follow the argument.
  • The creation-dependency matrix gains use()-argument and prop cases across client and server, .tsrx and .tsx, and dev and prod.

Risk / follow-ups

  • Getter precision trade-off. A skipped read now follows the policy fix: preserve guarded property reads in inferred hook dependencies #1123 set for conditional reads. An own data property tracks its value. An accessor tracks its receiver without invoking the getter. So format?.(user.name) with a getter-backed name on a stable user object no longer refreshes when only that getter's value changes. Before this PR it was precise, but it threw whenever user was missing.
  • Plain-module use() arguments are not covered. These are .ts/.js hook modules, handled by collectParallelUseDependencies in slot-hooks.js. That collector has no skip or defer policy at all: optional chains, callback bodies and unreplayable conditionals are all read eagerly. It's flagged as a separate follow-up rather than widened into this PR.
  • Runtime code is unchanged. Generated output changes only for optional chains with arguments or computed keys that read a member.

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-emitted dependency arrays for optional calls and computed members; behavior is well-tested but trades getter precision on skipped paths for correctness when receivers are absent.

Overview
Inferred hook dependencies, use() memo arguments, and SSR prop creations no longer evaluate arguments or computed keys that optional chaining would skip at render time. Patterns like run?.(options.label) and value?.[options.key] stop throwing when the receiver or options is missing, and no longer invoke getters on skipped paths during dependency evaluation.

The compiler adds followsOptionalLink and routes those skipped member reads through guarded __methodDep(..., true) probes (same policy as conditional reads). collectDepPaths can still promote a probe to a plain dependency when the same path is read unconditionally elsewhere (e.g. after ??). Documentation now states that optional-chain skipping applies to inferred deps and compiler-memoized creation sites.

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

…ndencies

`run?.(options.label)` and `value?.[options.key]` evaluate their argument or
computed key only once the receiver exists. Inferred hook dependency lists
(`collectDependencies`) and compiler-memoized `use()` and SSR prop creations
(`collectDepPaths`) read them unconditionally, so a missing receiver with an
undefined `options` threw from the dependency array, and an `options.label`
getter ran before the authored code would.

Both collectors now walk arguments and computed keys after an optional link
under a guard. Skipped one-level member reads use the existing guarded
`__methodDep(root, name, true)` descriptor probe; receiver reads stay precise,
and a non-skipped read of the same path keeps its plain member dependency.

Fixes #1799

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 6, 2026
@trueadm
trueadm marked this pull request as ready for review October 6, 2026 11:55
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No budget breaches, no added work, and no timing changes beyond ±3%.

Compares 4b46332e2, the merge commit's first parent with d9af2eb75 on the same runner. A byte value over its committed budget or any increase in a js-framework work counter fails this check. Wall time is a report: base and head samples alternate in one browser, and an operation is called slower or faster only when the 95% confidence interval of its ratio excludes ±3%. Workflow run

js-framework: 1k/10k row operations

All 20 timed operations (20 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 2.708 2.717 +0.0% ±0.0% … +3.8% ⚪ within noise
octane-tsrx replace 5.650 6.883 +5.3% −1.6% … +25.2% ⚪ within noise
octane-tsrx add 2.542 2.708 ±0.0% −3.9% … +6.1% ⚪ within noise
octane-tsrx update 0.132 0.130 −2.7% −5.0% … +4.3% ⚪ within noise
octane-tsrx select 0.005 0.005 +0.3% −1.9% … +2.0% ⚪ within noise
octane-tsrx swap 0.037 0.038 +2.7% +0.8% … +4.8% ⚪ within noise
octane-tsrx remove 0.024 0.025 +6.4% −3.7% … +12.8% ⚪ within noise
octane-tsrx runlots 26.192 26.042 −2.1% −5.4% … +1.8% ⚪ within noise
octane-tsrx select_lots 0.007 0.007 −1.0% −4.3% … +1.9% ⚪ within noise
octane-tsrx clear 52.833 52.017 +1.8% −7.3% … +7.9% ⚪ within noise
octane-jsx run 3.650 3.650 +0.0% −2.6% … +5.4% ⚪ within noise
octane-jsx replace 7.375 7.092 +2.8% −5.1% … +7.3% ⚪ within noise
octane-jsx add 3.508 3.350 −1.5% −5.9% … ±0.0% ⚪ within noise
octane-jsx update 0.300 0.301 +0.7% −2.9% … +5.2% ⚪ within noise
octane-jsx select 0.131 0.133 +2.3% +0.8% … +2.9% ⚪ within noise
octane-jsx swap 0.246 0.240 +1.1% −0.2% … +2.1% ⚪ within noise
octane-jsx remove 0.186 0.187 +0.6% −0.0% … +0.9% ⚪ within noise
octane-jsx runlots 31.108 29.967 −2.5% −6.7% … +0.3% ⚪ within noise
octane-jsx select_lots 1.633 1.647 +1.2% −1.7% … +3.7% ⚪ within noise
octane-jsx clear 53.383 54.958 −1.1% −4.3% … +4.2% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

No changes across 60 measured values.

Public-import reachability bundles (bytes)

No changes across 105 measured values.

@trueadm
trueadm merged commit 087cd77 into main Oct 6, 2026
50 of 52 checks passed
@trueadm
trueadm deleted the fix/optional-chain-skipped-deps branch October 6, 2026 12:14
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.

Optional-chain arguments are evaluated eagerly in inferred hook and SSR prop dependencies

1 participant