Repository navigation
fix(ui): make isMarkup survive cross-bundle module instance boundaries (#524) - #537
Merged
Merged
Conversation
#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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
urlcode init --with ui,auth,admincomposes extension bundles that each embed their own transitive copy ofui(scripts/prepare-extension-bundles.ts'sdependencySet():authembeds['ui','auth'],adminembeds['ui','auth','admin']). When a site composesui,auth,admintogether,installBundle/loadExtensionBundleextracts each requested bundle to its own content-hash-keyed cache directory, so the composed host loads more than one module instance of@jimhoyd/urlcode-uiin the same process: the standaloneuibundle's copy, and the copy embedded insideauth's/admin's bundle.packages/ui/src/escape.ts'sisMarkup()did a plainvalue instanceof Markupcheck, which fails across that module-instance boundary even though bothMarkupclasses 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 embeddeduicopy) then failedisMarkup()when checked by the layout template (rendering against the standaloneuicopy), fell through to being treated as a plain object, and the layout threwError: layout: object placed as text: contentat 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:
ui) across bundles requested together, so they resolve to one shared module instance.uireference it via the host's own already-loadeduiinstance (passed as a parameter) instead of importing values likeMarkup/escapeHtmlfrom their own embedded copy.instanceof Markupcheck 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
uiinstance 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
uicopies themselves, not just their observable breakage) is wanted later. This PR does not claim to resolve the underlying "each bundle embeds its own copy ofui" duplication.The fix
packages/ui/src/escape.ts:Markup's constructor now also sets aSymbol.for('urlcode.ui.Markup')-keyed property on each instance.Symbol.forreturns the same symbol across separately loaded module instances of the same source in one process (unlike a locally scopedSymbol()), so this brand survives the module-instance boundary thatinstanceofcannot cross.isMarkup()now checksvalue instanceof Markup(kept as the fast path — unchanged behavior for the common single-module-instance case) or the structural brand, so it recognizes aMarkupvalue 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 otherinstanceofchecks inpackages/*/srcare 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 wayMarkupis.Tests
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 mirroringprepare-extension-bundles.ts'sdependencySet()exactly, and actually serves the composed host end to end). It now passes.packages/ui/test/markup-brand.test.ts, a focused unit test that loadsescape.tstwice under distinct specifiers (a cache-busting query string forces two separate module instances, the same mechanism that separates bundles' embedded copies) and verifiesisMarkuprecognizes aMarkupvalue built by the other loaded instance, whileinstanceofalone does not.Results
npm run typecheck(root): cleannpm run typecheckinpackages/ui,packages/auth,packages/admin: cleanpackages/uitest suite: 91/91 passingpackages/authtest suite: 213/213 passingpackages/admintest suite: 67/67 passingtest/workspace-scaffold.integration.ts: 6/6 passing, including the previously-skipped#524regression testnpm 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