Skip to content

fix(universal): link Lynx main-thread output by importing only used helpers - #1391

Merged
trueadm merged 1 commit into
mainfrom
fix/lynx-main-thread-helper-imports
Sep 29, 2026
Merged

trueadm merged 1 commit into
mainfrom
fix/lynx-main-thread-helper-imports

Conversation

@trueadm

@trueadm trueadm commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Closes #1315.

Lynx main-thread compiler output failed to link with esbuild: No matching export in "packages/lynx/src/main-renderer.ts" for import "universalBlock". This happened even for a plain export function App() @{ <view id="simple" /> }. It is a regression from #1313, and the fix has two halves.

  • Compiler (octane): universal output imported all 13 region helpers from renderer.module whether it used them or not. Each emission site now records the helper it references (referenceHelper), and the import list comes from one table (UNIVERSAL_RENDERER_HELPER_IMPORTS) filtered to what the output uses. A plain component now imports defineUniversalComponent, universalPlan, and universalValue only. The three helpers that were already allocated lazily (firstScreenEvent, and the Three-only universalHostComponentLeafPlan and registerThreeIntrinsic) now go through the same path. A module that references no helper gets no renderer import at all, instead of a bare side-effect import.
  • Runtime (@octanejs/lynx): main-renderer.ts now exports a real universalBlock, so setup-bearing @{ … } child blocks render on the main thread instead of only compiling. The background claims one child owner range for each block. On the one-shot first screen that is exactly a taken @if arm, one range at the block's position, so universalBlock(body) returns universalIf(true, body). Host IDs, topology, and listener metadata match the background batch, and the main-thread runtime needs no new branch in materialize.

The new export guard found a second instance of the same bug: useFormState has been in the universal compiler's octane import allowlist since #943, but no universal runtime exported it. Both octane/universal and the Lynx renderers failed to link it. universal-core (which covers octane/universal and octane/universal/native) and the Lynx main renderer now alias it to useActionState, as the DOM octane entry already does.

Validation

  • pnpm format:files:check (changed files); repo-wide pnpm format:check not run
  • Typecheck: tsgo -p packages/octane/tsconfig.json, packages/octane/typetests, packages/lynx/tsconfig.json, tsconfig.testing.json, and packages/lynx/typetests. pnpm typecheck:files passes for the new test file. It reports existing DOM-lib/jsdom errors in main-renderer.test.ts and an unresolved .tsrx fixture import in compatibility-exports.test.ts, on lines this PR doesn't touch. Both packages exclude tests/ from their tsconfig. Full pnpm typecheck not run.
  • pnpm sync: no generated changes
  • pnpm test (full): not run locally. Ran every relevant Vitest project instead:
    • octane, octane-prod, lynx, three, ink, opentui, rspeedy-plugin, vite-plugin, rspack-plugin: 1,296 files, 25,431 tests passing before rebase
    • after rebasing onto 55c2c018c: the lynx project plus the octane/octane-prod universal, compiler-capability, compatibility-exports, and actions suites: 82 files, 1,389 tests passing
  • New regression tests, each confirmed red against a deliberately broken implementation:
    • packages/lynx/tests/compiled-runtime-link.test.ts
      • Export guard: for every universal renderer config @octanejs/lynx exports (background and main-thread), each runtime module exports every name the compiler can import from it: the helper table, rendererRegion, routed octane runtime imports, and thread-function imports to threadFunctionsModule. Helpers that only the Three lowering or main-thread callback erasure emit are gated by renderer. Red when universalBlock or useFormState is missing.
      • esbuild link repros from the issue, a plain <view /> and a setup-bearing child block, bundled against this checkout's runtime modules and rendered.
      • Compiled parity: a block that owns useState and a bindtap handler. The main-thread first-screen batch must equal the background batch. Red if a main-thread block drops its range.
    • universal-compiler-capabilities.test.ts: every import from the renderer module is referenced by the compiled module, for main-thread Lynx, background Lynx, and an object renderer, in dev and prod. Red on the pre-fix compiler.
    • main-renderer.test.ts: universalBlock added to the descriptor-level main-vs-background control-flow parity case.
    • compatibility-exports.test.ts: octane/universal and octane/universal/native export useFormState === useActionState.
  • lynx-bundle-size, baseline de2685e13 vs this change, same command. Raw bytes are unchanged for preview and IFR main and background (for example, IFR main 203,800 → 203,800 B). gzip moves −3 to +1 B, and Brotli moves −224 to +291 B. That churn is deterministic minifier renaming at identical raw length, caused by the tree-shaken useFormState alias. Without the alias, the background program is byte-identical to baseline. An earlier revision that added a $$kind === UNIVERSAL_BLOCK branch to materialize cost +109 B raw / +54 B gzip on every main-thread bundle; routing through universalIf removed that.

Risk / follow-ups

  • The lynx-bundle-size suite's budget gate already fails on origin/main before this change (preview main gzip 83,657 > 82,070). This PR doesn't move that number.
  • Compile-time timing was too noisy on this machine to measure (0.9–2.6 s for the same run). I chose per-site marking over a post-emission AST scan so the compiler does no extra work.
  • Renderer-boundary regions (lowerUniversalRendererRegionAst) still import their generated hook-runtime aliases unconditionally. Every universal runtime, including the Lynx main renderer, exports those names (the guard checks this), so they link.

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 universal compiler import emission and Lynx first-screen rendering parity; scope is bounded by new link and batch-equality tests but affects all universal Lynx builds.

Overview
Fixes Lynx main-thread bundles failing to link when esbuild rejects imports for renderer helpers the compiled module never calls.

Compiler: Universal output now records each emitted renderer helper via referenceHelper and builds import lists from UNIVERSAL_RENDERER_HELPER_IMPORTS filtered by usedHelpers, so a minimal component only pulls in helpers like defineUniversalComponent, universalPlan, and universalValue instead of the full helper surface.

Lynx main thread: @octanejs/lynx/main-renderer adds universalBlock (implemented as universalIf(true, body)) so setup-bearing @{ … } blocks produce the same first-screen range topology as the background renderer, plus useFormState as an alias of useActionState.

Universal runtime: octane/universal and octane/universal/native export the same useFormState alias so universal modules that import it from octane can link.

Regression coverage includes esbuild link tests for plain and block components, compiler tests that every renderer import is referenced, and main-vs-background batch parity for blocks with hooks and events.

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

…elpers

Universal compiler output imported every region helper from its renderer
module, used or not. The Lynx main thread's standalone runtime did not export
universalBlock, so esbuild rejected even a plain `<view />` component.

- The compiler records each helper reference as it emits it and imports only
  those, so a module never names a helper its output does not call.
- @octanejs/lynx/main-renderer exports universalBlock. On the one-shot first
  screen a block renders like a taken @if arm, one range at its position, so
  host IDs match the background.
- octane/universal(/native) and the Lynx main renderer export useFormState.
  The universal compiler already accepted that import, but no runtime
  provided it.

Closes #1315

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 Sep 29, 2026
@trueadm
trueadm marked this pull request as ready for review September 29, 2026 12:29
@trueadm
trueadm merged commit 40f4827 into main Sep 29, 2026
50 checks passed
@trueadm
trueadm deleted the fix/lynx-main-thread-helper-imports branch September 29, 2026 12:46
trueadm added a commit that referenced this pull request Oct 2, 2026
… fixtures in PR CI (#1583)

* fix(compiler): keep cached-array regions on signal-capable child holes

Since #1069, a renderable `{expr}` hole in a DOM template lowers to the
signal-capable `bindSignalChild` binding whenever the module can carry a
signal handle, which is any identifier hole. That branch returned before the
only-child path that wraps a compiler-owned calculated descriptor array in its
`compilerCacheArray` auto-memo region, so an unchanged array inside a
component-children body reached `childSlot` for the list and each item on
every parent render. The hook-memo provider-output guard
`inline_memo_snapshot_slots` rose from 0 to 256 per 128 updates.

Wrap the signal-capable binding in the same region. Only an unchanged plain
data array can hit; a signal handle or any other value fails
`compilerCacheArray` and rebinds every render, and the existing identity miss
rebuilds a previously cached array after the hole held something else.

The new value-position case switches one calculated child between cached rows
and a signal handle. It fails in the production compile when the region's
identity miss is dropped, and passes on the uncached path too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hg86UdUyzRwFTLWhpAMdFE

* test(bench): report hook-memo scenarios net of the root update driver

Eight hook-memo ratio guards breached on main (eligible hit functions 704,
eligible hit arrays 3,872, and so on) while the inline memo tier's own counts
were unchanged. Per-site attribution put every excess event in the runtime's
same-component `root.render()` path: the per-update request/retry closures
and the root render transaction's undo log, OffscreenCapture arrays and
committed-transaction queue, 2 functions and 10 arrays per render in both
compiler variants. Bisecting `run.mjs` output found 5f7a457 (#833, root
render transactions) to be the first breaching commit; it did not touch this
benchmark, and its documented root-transaction cost is unrelated to hooks.

Add a hook-free Driver control that runs the identical phase sequence, report
every scenario net of the driver's same-phase counts (failing the run if a
scenario falls below it), and keep raw counters in the JSON metadata. Two new
guards pin the driver's per-update cost at its current 2 functions and
10 arrays so the root update path cannot grow silently.

The corrected runner reports identical net counts on both sides of #833, and
against 814a3c1 (before closure-free memo lowering) it still breaches the
same seven memo guards the original measurement recorded.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hg86UdUyzRwFTLWhpAMdFE

* ci: check the Lynx bench fixture contracts on every pull request

The weekly Bench workflow runs the Lynx table-stage and list fixture
contracts before its ratio guards. Those tests compile copies of
packages/lynx/src, and Lynx changes broke them twice (09-21: an unresolved
`octane/internal/context` import; 09-28: a removed `universalBlock` export,
fixed on main by #1391). PR CI never ran them, so each break surfaced only
in the next weekly run and kept every ratio guard from executing.

Add both suites to `ci:workflow:test`, which the lint job runs on every
ready pull request, and assert the two lists stay aligned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hg86UdUyzRwFTLWhpAMdFE

* fix(compiler): keep raw-HTML hosts off the cached signal child region

A host that may receive dangerouslySetInnerHTML, directly or through a
spread, must reach its child binding on every render: that call is where
children and raw HTML are kept mutually exclusive. The ordinary only-child
path already checks this before choosing the cached-array region, but the
region restored around bindSignalChild did not, so an unchanged cached array
let a spread add raw HTML beside still-mounted rows without the error.

Skip the region for such hosts, as the ordinary path does. The new spread
case fails in the production compile without the guard.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hg86UdUyzRwFTLWhpAMdFE

---------

Co-authored-by: Claude <[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.

Lynx main-thread compiler output fails to link because universalBlock is not exported

1 participant