Skip to content

fix(compiler): declare the host ref/spread binding in every element position - #758

Merged
trueadm merged 1 commit into
octanejs:mainfrom
dudusotero:fix/virtual-tsx-ref-spread-declaration
Aug 15, 2026
Merged

trueadm merged 1 commit into
octanejs:mainfrom
dudusotero:fix/virtual-tsx-ref-spread-declaration

Conversation

@dudusotero

@dudusotero dudusotero commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A host element carrying ref plus a JSX spread lowers, in the type-only path, to a generated let X = __normalize_spread_props_for_ref_attr(bag) with the attributes rewritten to read X. The declaration rides on the element's metadata for a later pass to hoist, and only two consumers hoist it: the render-block statement builder and the native-directive path. An element anywhere else kept the references and lost the declaration, so editors and tsrx-tsc reported TS2304 on source that compiles and runs correctly.

Such an element also reached both lowering sites — the walker's JSXOpeningElement visitor and transform_element_attributes_dispatch — and the lowering is not idempotent: the second pass read the composed ref={[authored, X.ref]} array as an authored ref and composed it again, emitting a second binding and a ref nested one level deeper than the runtime ever produces. merge_duplicate_refs rebuilds the merged ref without the synthetic_ref marker, so the attribute list could no longer answer "already lowered".

Beyond the positions in the issue, the same defect covers an attribute value, an array-literal element, an element with two spreads, <text {...bag} ref={bag.ref} />, and plain JS nested inside a native @{ … } template — the issue notes the native directive path works, but only the directive does.

Closes #737.

Why

ref plus a spread on a host element is ordinary authoring. Nothing in docs/differences-from-react.md or .rulesync/rules/tsrx-authoring.md restricts where such an element may appear, and the authoring rule documents ref as a plain prop with no positional constraint. The runtime compiles every position correctly, so the divergence was confined to the type-only print reached through octane/compiler/volar.

Rewriting the three affected visx components to hoist the element into a return would hide a compiler defect behind app changes and leave the position broken for every consumer.

Changes

  • patches/@[email protected] — mark an opening element whose host ref/spread has been lowered and have the attribute dispatch honour that marker; wrap residual setup declarations at the walker's JSXElement path in the same wrap_jsx_setup_declarations IIFE the native-directive path already emits. Both changes are scoped to transform_context.typeOnly, so runtime emit for the other platforms sharing this transform is unchanged.
  • pnpm-workspace.yaml — patch rationale, following the existing convention in that block including the "remove once upstream ships it" note.
  • packages/octane/tests/compiler/volar.test.ts — the position matrix, one test asserting no TS2304 from a real ts program over the generated TSX, and one asserting the composed ref is a flat array.
  • scripts/check-virtual-tsx-declarations.mjs + virtual-tsx:check + a CI step — scans every .tsrx in the repo for a generated name referenced without a declaration. Packages typechecked through tsgo cannot catch this class, because tsgo does not read .tsrx at all.
  • The regenerated lockfileSha256 in 73 audit/react-parity.json files and the evals manifest follows from the new patch hash. That line is their only change.

Validation

Both new tests were confirmed to fail without the fix: 25 TS2304 diagnostics and a nested ref array respectively. The guard names the three visx files and exits 1 with the fix inert.

  • pnpm format:check — via pnpm format:files:check on the changed files
  • pnpm typecheck — not run in full. pnpm typecheck:files reports 4 pre-existing errors in volar.test.ts (TS7016 on the octane/compiler/volar import, three TS7006); the identical set is present on the stashed HEAD version, so this diff adds none.
  • pnpm test — run twice; fails both times, confined to packages/octane/tests/browser/native-change/native-change.test.ts (12 tests, page.waitForFunction timeouts rooted in a Vite 504 (Outdated Optimize Dep)). The same file fails on clean origin/main with this patch absent, so it is pre-existing and not caused by this change. Failure count varied 14 / 12 / 1 across identical runs, and clearing the Vite optimizer caches did not change it. Everything else passed: 2192 files, 21590 tests.
  • targeted: vitest --project octane (441 files, 7118 tests), volar.test.ts (17), virtual-tsx:check (2139 files), tsrx-tsc -p packages/visx (TS2304 7 → 0), visx compile:check (257 components, client + server) and types:check, ci:workflow:test (108), changeset:check

No changeset: packages/octane/src is untouched, and a pnpm patch never ships in the tarball, so consumer-visible package behavior is unchanged.

Risk / follow-ups

  • The fix is local to this repo. A pnpm patch is not published, so octane's own consumers keep the broken virtual TSX until @tsrx/core ships the equivalent fix. The pnpm-workspace.yaml note records that.
  • The same defect breaks @tsrx/react's runtime emit, which is why the typeOnly scoping matters here and why this is worth fixing upstream. Its transform on the concise-arrow position produces <text {...A__spread_props2} ref={__mergeRefs(A__spread_props2.ref, __mergeRefs(p.r, A__spread_props1.ref))} />, where neither name is declared — a ReferenceError when that module runs, not just an editor diagnostic. Filing upstream is a maintainer call, not done here.
  • Cost on the IDE path: the wrapper now runs on every element in the type-only path, and it deep-walks the subtree to find declarations. Measured over all 2139 .tsrx files: 8.06 / 8.06 / 8.09s with it against 7.77 / 7.91 / 7.92s without — roughly 2%, and nothing on runtime compile.
  • A separate pre-existing defect in the same lowering, worth its own issue: the generated ref={[authored, X.ref]} reads .ref off the normalized bag unconditionally. When the spread bag's type declares no ref that is TS2339; when it declares ref as optional the array element is T | undefined and Octane.Ref<T> admits no undefined, so it is TS2322. Both fire in every position, including the ones that always worked. The repo's bindings spread wide React-style attribute bags and do not trip it; a narrow bag like { x?: number } trips it every time.
  • visx still has 31 tsrx-tsc errors (was 31 with 7 TS2304, now 31 with 0). Fixing this un-masked two the undeclared any had been hiding: Projection.tsrx:187 TS2345 and Text.tsrx:37 TS2551. None of it is gated — root pnpm typecheck sends visx through tsgo, which reads no .tsrx, so that package's type surface is unchecked in CI. Moving it to tsrx-tsc needs those errors resolved first and is deliberately out of scope here.

Provenance

  • An agent produced this diff (agent-authored)

Note

Medium Risk
Touches shared compiler transform via pnpm patch (type-only path); wrong scoping could affect IDE typings or double-lowering behavior, though runtime emit is explicitly unchanged.

Overview
Fixes virtual TSX diagnostics where host elements with ref plus a spread compile at runtime but editors/tsrx-tsc report TS2304 because the type-only lowering dropped the generated spread/ref binding outside render-block and native-directive hoisting paths.

The @tsrx/core pnpm patch (type-only scoped) marks elements after host ref/spread lowering, skips double-lowering in attribute dispatch, and wraps residual setup declarations in the same wrap_jsx_setup_declarations IIFE used elsewhere so bindings are declared in ternaries, concise arrows, callbacks, native templates, etc.

Adds scripts/check-virtual-tsx-declarations.mjs, pnpm virtual-tsx:check, and a CI step that scans all .tsrx virtual TSX for referenced-but-undeclared generated names. volar.test.ts gains a position matrix with TS2304 and single-level ref-array assertions. Lockfile hash updates in parity audit JSON and evals manifest follow the patch hash change.

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

…osition

A host element carrying `ref` plus a JSX spread lowers to a generated
`let X = __normalize_spread_props_for_ref_attr(bag)` that rides on the
element's metadata for a later pass to hoist. Only the render-block
statement builder and the native-directive path hoist it, so the same
element in plain-JS expression position — a ternary arm, a concise arrow
body, a declarator init, a callback body, an attribute value, an array
element, or plain JS nested in a native template — kept the references and
lost the declaration. Editors and tsrx-tsc then reported TS2304 on source
the runtime compiles and runs correctly.

Such an element also reached both lowering sites, and the lowering is not
idempotent: the second pass read the composed `ref={[authored, X.ref]}`
array as an authored ref and composed it again, emitting a second binding
and a ref nested one level deeper than the runtime ever produces.

Patch @tsrx/core to wrap residual declarations in the IIFE the
native-directive path already uses, and to mark an element whose host
ref/spread has been lowered so the dispatch does not lower it twice. Both
are scoped to the type-only path, so runtime emit for the other platforms
sharing the transform is unchanged.

Cover the position matrix in the volar suite, and add a repo-wide scan so
no .tsrx file can reference a generated name it never declares. Packages
typechecked through tsgo cannot catch this class: tsgo does not read .tsrx.

The regenerated lockfile hash in the parity audits and the evals manifest
follows from the new patch hash.
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Aug 14, 2026
@dudusotero
dudusotero marked this pull request as ready for review August 14, 2026 19:39
@trueadm
trueadm merged commit 5f733dd into octanejs:main Aug 15, 2026
49 checks passed
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.

Virtual TSX drops the let __spread_props declaration for a host element with ref plus a spread outside a direct return, producing TS2304

2 participants