Skip to content

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

Closed

Description

@jimhoyd

What I observed

Composing multiple extension bundles together via urlcode init --with ui,auth,admin --bundle-release ... and then actually serving the generated host (not just generating it) throws at the first screen render that needs auth/admin content wrapped as safe HTML:

Error: layout: object placed as text: content
    at renderNodes (.../node_modules/@jimhoyd/urlcode-ui/dist/template.js:214:27)
    ...
    at Object.wrap (.../node_modules/@jimhoyd/urlcode-ui/dist/host/extension.js:154:36)
    at screenResponse (.../node_modules/@jimhoyd/urlcode-auth/dist/auth-ui.js:29:22)
    at httpFailure (.../node_modules/@jimhoyd/urlcode-auth/dist/auth-ui.js:143:76)

Note the two different node_modules/@jimhoyd/urlcode-ui/... paths in the stack — they're under different extracted bundle cache directories (different SHA-256-named folders under .urlcode/extension-bundles/).

Root cause

scripts/prepare-extension-bundles.ts's dependencySet() makes each named bundle embed its own copy of ui:

function dependencySet(bundle:BundleName):BundleName[]{return bundle==='ui'?['ui']:bundle==='auth'?['ui','auth']:bundle==='admin'?['ui','auth','admin']:bundle==='forms'?['ui','forms']:['store'];}

So extension-bundles@v…'s auth asset contains its own node_modules/@jimhoyd/urlcode-ui/..., separate from the standalone ui asset. When a site composes ui,auth,admin together, installBundle/loadExtensionBundle extracts each requested bundle to its own cache directory keyed by content hash, so the generated host ends up with two independently-loaded module instances of @jimhoyd/urlcode-ui: one from the standalone ui bundle (used to build the actual ui host instance passed around), and one embedded inside auth's bundle (used by auth's own compiled code, e.g. packages/auth/src/auth-ui.ts importing Markup from @jimhoyd/urlcode-ui).

packages/ui/src/escape.ts's isMarkup() does a plain instanceof check:

export function isMarkup(value) { return value instanceof Markup; }

A Markup instance created by auth's embedded ui copy is not instanceof the standalone ui bundle's Markup class — they're different classes from Node's perspective, despite being built from identical source. template.js's renderer falls through to treating the wrapped-safe-HTML value as a plain object instead of unwrapping it, producing "object placed as text: content".

How I found this

While converting test/workspace-scaffold.integration.ts to bundle-mode fixtures (following #503/#511-#523's removal of npm-based --with distribution), I built a local packer that faithfully reproduces prepare-extension-bundles.ts's real dependencySet/entryFor conventions (verified byte-for-byte against the actual script) rather than inventing my own. The "a generated site overrides auth and admin screens from its own ui/ directory" test — which is the only test in the suite that actually serves a composed multi-bundle host rather than just checking generated file content — reproduces this reliably. I confirmed the conventions match production exactly, so this isn't a fixture artifact; the real published [email protected] release would hit the same failure serving a composed ui+auth or ui+auth+admin site.

Impact

Every composed multi-extension bundle site (--with ui,auth, --with ui,auth,admin, etc. via --bundle-release) is likely broken at the first screen that renders Markup-wrapped content originating from a non-ui extension — which is the primary, advertised use case for auth/admin (sign-in screens, dashboards). This has apparently never been caught because no existing test previously exercised live rendering of a multi-bundle composed host (the closest coverage, test/workspace-scaffold.integration.ts's companion-scaffold test, explicitly skips live serving via t.diagnostic(...), and single-extension bundle tests don't hit cross-bundle instance duplication at all).

Possible directions (not prescribing one)

  • Have installBundle/the extracted-cache layout deduplicate shared sub-dependencies (e.g. ui) across bundles requested together in the same --with, so they resolve to one shared module instance instead of one per bundle.
  • Or have bundles that depend on ui reference it via the host's own already-loaded ui instance (passed as a parameter, as already happens for the runtime ui object) rather than importing static values like Markup/escapeHtml from their own embedded copy.
  • Or replace the instanceof Markup check with a structural/duck-typed check (e.g. a marker property) that survives cross-module-instance boundaries.

I did not attempt a fix — this needs a real design decision about how the bundle format should share cross-extension dependencies, not a quick patch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions