Skip to content

Fix "Invalid base URL" when creating card def inheriting from cross-realm spec#4348

Open
lucaslyl wants to merge 8 commits intomainfrom
CS-10590/create-new-card-def-from-listing-hit-invalid-base-url
Open

Fix "Invalid base URL" when creating card def inheriting from cross-realm spec#4348
lucaslyl wants to merge 8 commits intomainfrom
CS-10590/create-new-card-def-from-listing-hit-invalid-base-url

Conversation

@lucaslyl
Copy link
Copy Markdown
Contributor

@lucaslyl lucaslyl commented Apr 7, 2026

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

  1. Resolve spec.moduleHref through codeRefWithAbsoluteURL() before constructing the module URL, so prefix-form refs are normalized to real URLs consistently.
  2. Use cardIdToURL() for spec IDs so prefix-form card IDs are resolved correctly when deriving the base URL.

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.

@lucaslyl
Copy link
Copy Markdown
Contributor Author

lucaslyl commented Apr 7, 2026

@codex review

@lucaslyl lucaslyl self-assigned this Apr 7, 2026
@cardstack cardstack deleted a comment from chatgpt-codex-connector bot Apr 7, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 7, 2026

Preview deployments

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 7, 2026

Host Test Results

2 186 tests  +1   2 171 ✅ +1   2h 21m 56s ⏱️ -9s
    1 suites ±0      15 💤 ±0 
    1 files   ±0       0 ❌ ±0 

Results for commit 9a9c725. ± Comparison against base commit 2e77b8f.

♻️ This comment has been updated with latest results.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 7, 2026

Realm Server Test Results

  1 files  ±0    1 suites  ±0   13m 14s ⏱️ -24s
836 tests ±0  836 ✅ ±0  0 💤 ±0  0 ❌ ±0 
907 runs  ±0  907 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit 9a9c725. ± Comparison against base commit 2e77b8f.

♻️ This comment has been updated with latest results.

@habdelra habdelra requested a review from Copilot April 7, 2026 22:23
@habdelra
Copy link
Copy Markdown
Contributor

habdelra commented Apr 7, 2026

I would like to see a regression test for this. please include a test that fails without this fix but passes with it.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 calling new URL()).
  • Uses cardIdToURL(spec.id) instead of new URL(spec.id) to correctly handle prefix-form spec IDs when building relativeTo for codeRefWithAbsoluteURL().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 50 to 51
import { cardIdToURL } from '@cardstack/runtime-common/card-reference-resolver';
import { codeRefWithAbsoluteURL } from '@cardstack/runtime-common/code-ref';
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +830 to +835
// 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);
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
// 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,
);

Copilot uses AI. Check for mistakes.
@lucaslyl
Copy link
Copy Markdown
Contributor Author

lucaslyl commented Apr 8, 2026

I would like to see a regression test for this. please include a test that fails without this fix but passes with it.

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.

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.

3 participants