Fix "Invalid base URL" when creating card def inheriting from cross-realm spec#4348
Fix "Invalid base URL" when creating card def inheriting from cross-realm spec#4348
Conversation
|
@codex review |
Preview deployments |
|
I would like to see a regression test for this. please include a test that fails without this fix but passes with it. |
There was a problem hiding this comment.
Pull request overview
Fixes a runtime “Invalid base URL” error in the Host operator-mode “Create File” modal when creating new definitions that inherit from specs located in another realm (notably when the spec uses an @cardstack/... prefix-form ID).
Changes:
- Switches module URL construction to use
spec.moduleHref(which resolves prefix/relative references before callingnew URL()). - Uses
cardIdToURL(spec.id)instead ofnew URL(spec.id)to correctly handle prefix-form spec IDs when buildingrelativeToforcodeRefWithAbsoluteURL().
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| import { cardIdToURL } from '@cardstack/runtime-common/card-reference-resolver'; | ||
| import { codeRefWithAbsoluteURL } from '@cardstack/runtime-common/code-ref'; |
There was a problem hiding this comment.
cardIdToURL is already exported from @cardstack/runtime-common (the file already imports from that barrel). Consider importing it from the existing @cardstack/runtime-common import list instead of a separate deep import to keep imports consistent and avoid relying on internal module paths.
| // Use spec.moduleHref which correctly resolves relative module paths | ||
| // via resolveCardReference (handles @cardstack/catalog/... prefix IDs). | ||
| // Without this, new URL(relativeModule, prefixId) throws "Invalid base URL". | ||
| let absoluteModule = spec?.moduleHref | ||
| ? new URL(spec.moduleHref) | ||
| : new URL(module); |
There was a problem hiding this comment.
This fixes URL construction for cross-realm/prefix-form spec IDs, but there doesn’t appear to be acceptance coverage for the specific failure mode described in CS-10590 (selecting an "Inherits From" spec from another realm that uses an @cardstack/... prefix ID). Adding a test that picks a catalog spec (prefix ID) and creates a card definition would help prevent regressions.
| // Use spec.moduleHref which correctly resolves relative module paths | |
| // via resolveCardReference (handles @cardstack/catalog/... prefix IDs). | |
| // Without this, new URL(relativeModule, prefixId) throws "Invalid base URL". | |
| let absoluteModule = spec?.moduleHref | |
| ? new URL(spec.moduleHref) | |
| : new URL(module); | |
| // Use spec.moduleHref when available because it is already fully resolved. | |
| // Otherwise normalize the code ref against the selected realm so relative | |
| // and prefix-form module IDs (for example @cardstack/...) become absolute | |
| // URLs before passing them to maybeRelativeURL. | |
| let absoluteModule = spec?.moduleHref | |
| ? new URL(spec.moduleHref) | |
| : new URL( | |
| codeRefWithAbsoluteURL( | |
| { module, name: exportName }, | |
| new URL(this.selectedRealmURL), | |
| ).module, | |
| ); |
I added a regression test in create-file-test for creating a card definition from a spec whose ref uses a prefix-form module ID, so we cover resolving that ref before generating the import. |
linear: https://linear.app/cardstack/issue/CS-10590/create-new-card-def-from-listing-hit-invalid-base-url
When creating a new Card Definition and selecting "Inherits From" a spec from another realm (e.g. Listing from catalog realm), the modal threw TypeError: Failed to construct 'URL': Invalid base URL.
Screen.Recording.2026-04-07.at.10.34.00.PM.mov
Fix
Test Added
Added a regression test in create-file-test covering creation of a card definition in workspace A that extends a card from workspace B via a prefix-form ref, ensuring the inherited definition import is generated from the resolved module URL.