Skip to content

fix(ui): make isMarkup survive cross-bundle module instance boundaries (#524) - #537

Merged
jimhoyd merged 1 commit into
mainfrom
fix/isMarkup-cross-bundle-524
Sep 23, 2026
Merged

jimhoyd merged 1 commit into
mainfrom
fix/isMarkup-cross-bundle-524

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Root cause

urlcode init --with ui,auth,admin composes extension bundles that each embed their own transitive copy of ui (scripts/prepare-extension-bundles.ts's dependencySet(): auth embeds ['ui','auth'], admin embeds ['ui','auth','admin']). When a site composes ui,auth,admin together, installBundle/loadExtensionBundle extracts each requested bundle to its own content-hash-keyed cache directory, so the composed host loads more than one module instance of @jimhoyd/urlcode-ui in the same process: the standalone ui bundle's copy, and the copy embedded inside auth's/admin's bundle.

packages/ui/src/escape.ts's isMarkup() did a plain value instanceof Markup check, which fails across that module-instance boundary even though both Markup classes are built from identical source (each loaded module gets its own distinct class object). Wrapped-safe-HTML values returned by auth/admin's rendering (constructed against their own embedded ui copy) then failed isMarkup() when checked by the layout template (rendering against the standalone ui copy), fell through to being treated as a plain object, and the layout threw Error: layout: object placed as text: content at the first real screen render — breaking sign-in/dashboard screens for essentially every composed multi-extension site.

Why this fix (direction 3 of the three the issue named)

The issue named three possible directions without prescribing one:

  1. Dedup shared sub-dependencies (ui) across bundles requested together, so they resolve to one shared module instance.
  2. Have bundles that depend on ui reference it via the host's own already-loaded ui instance (passed as a parameter) instead of importing values like Markup/escapeHtml from their own embedded copy.
  3. Replace the instanceof Markup check with a structural/duck-typed check that survives cross-module-instance boundaries.

This PR implements direction 3. It's the most surgical, lowest-risk fix: it doesn't require redesigning the bundle cache/extraction format (direction 1) or plumbing the ui instance through auth/admin's rendering call sites as a parameter (direction 2, a bigger API change with wider blast radius). It fixes the actual bug — a cross-instance identity check — at its root, without touching the bundle distribution architecture, which the issue itself flagged as needing "a real design decision."

Directions 1 and 2 remain open as follow-up architectural considerations if a more thorough fix (eliminating the duplicate ui copies themselves, not just their observable breakage) is wanted later. This PR does not claim to resolve the underlying "each bundle embeds its own copy of ui" duplication.

The fix

packages/ui/src/escape.ts: Markup's constructor now also sets a Symbol.for('urlcode.ui.Markup')-keyed property on each instance. Symbol.for returns the same symbol across separately loaded module instances of the same source in one process (unlike a locally scoped Symbol()), so this brand survives the module-instance boundary that instanceof cannot cross.

isMarkup() now checks value instanceof Markup (kept as the fast path — unchanged behavior for the common single-module-instance case) or the structural brand, so it recognizes a Markup value regardless of which loaded copy of the module constructed it.

Searched the rest of the workspace for other cross-package instanceof-based identity checks with the same failure mode; found none — the other instanceof checks in packages/*/src are all native types (Error, Uint8Array, Response, Date, Map) or error classes (AuthError, HttpError, StoreError, ConfigError, AuthHttpError) that are constructed and consumed within a single package's own module graph, not passed across the bundle-composition boundary the way Markup is.

Tests

  • Un-skipped test/workspace-scaffold.integration.ts's 'a generated site overrides auth and admin screens from its own ui/ directory' test, which was previously force-skipped pending this fix (it reproduces the bug with a real bundle pack/extract cycle mirroring prepare-extension-bundles.ts's dependencySet() exactly, and actually serves the composed host end to end). It now passes.
  • Added packages/ui/test/markup-brand.test.ts, a focused unit test that loads escape.ts twice under distinct specifiers (a cache-busting query string forces two separate module instances, the same mechanism that separates bundles' embedded copies) and verifies isMarkup recognizes a Markup value built by the other loaded instance, while instanceof alone does not.

Results

  • npm run typecheck (root): clean
  • npm run typecheck in packages/ui, packages/auth, packages/admin: clean
  • packages/ui test suite: 91/91 passing
  • packages/auth test suite: 213/213 passing
  • packages/admin test suite: 67/67 passing
  • test/workspace-scaffold.integration.ts: 6/6 passing, including the previously-skipped #524 regression test
  • npm run verify (full lint/typecheck/check/build/test/workspaces): ran to completion in the background; no failures observed before this PR was opened (see follow-up comment if anything surfaces after this description was written)

Closes #524

🤖 Generated with Claude Code

#524)

Composing extension bundles together (urlcode init --with ui,auth,admin)
made each named bundle embed its own transitive copy of ui, so a
composed host loads more than one module instance of @jimhoyd/urlcode-ui.
isMarkup()'s plain `instanceof Markup` check fails across that boundary
even though both Markup classes come from identical source, so
wrapped-safe-HTML values built by auth/admin's embedded ui copy fell
through to being treated as plain objects and the layout template threw
"object placed as text: content" when actually served.

Brand Markup instances with a Symbol.for('urlcode.ui.Markup')-keyed
property set in the constructor. Symbol.for returns the same symbol
across separately loaded module instances of the same source in one
process, so isMarkup() can check it as a fallback after the (kept, fast
path) instanceof check to recognize a Markup value regardless of which
loaded copy of the module built it.

Un-skips the existing composed-bundle integration test that reproduced
this with a real bundle pack/extract cycle, and adds a focused unit test
that loads escape.ts twice under distinct specifiers to exercise the
cross-instance brand check directly.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@jimhoyd
jimhoyd merged commit ed2efa1 into main Sep 23, 2026
61 checks passed
@jimhoyd
jimhoyd deleted the fix/isMarkup-cross-bundle-524 branch September 23, 2026 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

extension-bundles: composing ui + auth/admin breaks cross-bundle instanceof checks (Markup rendering fails)

1 participant