diff --git a/packages/base/links-to-editor.gts b/packages/base/links-to-editor.gts index a8287aa4d3..e0f6be6c27 100644 --- a/packages/base/links-to-editor.gts +++ b/packages/base/links-to-editor.gts @@ -24,6 +24,7 @@ import { RealmURLContextName, type ResolvedCodeRef, getNarrowestType, + Loader, } from '@cardstack/runtime-common'; import { AddButton, IconButton } from '@cardstack/boxel-ui/components'; import { IconMinusCircle } from '@cardstack/boxel-ui/icons'; @@ -146,7 +147,7 @@ export class LinksToEditor extends GlimmerComponent { private chooseCard = restartableTask(async () => { let type = identifyCard(this.args.field.card) ?? baseCardRef; - type = await getNarrowestType(this.args.subclassType, type); + type = await getNarrowestType(this.args.subclassType, type, myLoader()); let chosenCard: CardDef | undefined = await chooseCard( { filter: { type } }, { @@ -164,3 +165,14 @@ export class LinksToEditor extends GlimmerComponent { } }); } + +function myLoader(): Loader { + // we know this code is always loaded by an instance of our Loader, which sets + // import.meta.loader. + + // When type-checking realm-server, tsc sees this file and thinks + // it will be transpiled to CommonJS and so it complains about this line. But + // this file is always loaded through our loader and always has access to import.meta. + // @ts-ignore + return (import.meta as any).loader; +} diff --git a/packages/base/links-to-many-component.gts b/packages/base/links-to-many-component.gts index 87a8558655..097e5d3e7a 100644 --- a/packages/base/links-to-many-component.gts +++ b/packages/base/links-to-many-component.gts @@ -31,6 +31,7 @@ import { RealmURLContextName, type ResolvedCodeRef, getNarrowestType, + Loader, } from '@cardstack/runtime-common'; import { IconMinusCircle, IconX, FourLines } from '@cardstack/boxel-ui/icons'; import { eq } from '@cardstack/boxel-ui/helpers'; @@ -98,7 +99,7 @@ class LinksToManyEditor extends GlimmerComponent { selectedCards?.map((card: any) => ({ not: { eq: { id: card.id } } })) ?? []; let type = identifyCard(this.args.field.card) ?? baseCardRef; - type = await getNarrowestType(this.args.subclassType, type); + type = await getNarrowestType(this.args.subclassType, type, myLoader()); let filter = { every: [{ type }, ...selectedCardsQuery], }; @@ -488,3 +489,14 @@ export function getLinksToManyComponent({ }, }); } + +function myLoader(): Loader { + // we know this code is always loaded by an instance of our Loader, which sets + // import.meta.loader. + + // When type-checking realm-server, tsc sees this file and thinks + // it will be transpiled to CommonJS and so it complains about this line. But + // this file is always loaded through our loader and always has access to import.meta. + // @ts-ignore + return (import.meta as any).loader; +} diff --git a/packages/runtime-common/code-ref.ts b/packages/runtime-common/code-ref.ts index b7e264c11b..16d5abe41d 100644 --- a/packages/runtime-common/code-ref.ts +++ b/packages/runtime-common/code-ref.ts @@ -233,22 +233,11 @@ function refEquals(ref1: CodeRef, ref2: CodeRef): boolean { if (!isResolvedCodeRef(ref1) || !isResolvedCodeRef(ref2)) { return false; } - return ref1.name === ref1.module && ref1.module === ref2.module; + return ref1.name === ref2.name && ref1.module === ref2.module; } -function myLoader(): Loader { - // we know this code is always loaded by an instance of our Loader, which sets - // import.meta.loader. - - // When type-checking realm-server, tsc sees this file and thinks - // it will be transpiled to CommonJS and so it complains about this line. But - // this file is always loaded through our loader and always has access to import.meta. - // @ts-ignore - return (import.meta as any).loader; -} - -async function getAncestorRef(codeRef: CodeRef) { - let card = await loadCard(codeRef, { loader: myLoader() }); +async function getAncestorRef(codeRef: CodeRef, loader: Loader) { + let card = await loadCard(codeRef, { loader: loader }); let ancestor = getAncestor(card); return identifyCard(ancestor); } @@ -258,13 +247,14 @@ async function getAncestorRef(codeRef: CodeRef) { async function isInsideAncestorChain( codeRef: CodeRef, codeRefAncestor: CodeRef, + loader: Loader, ): Promise { if (refEquals(codeRef, codeRefAncestor)) { return true; } else { - let newAncestorRef = await getAncestorRef(codeRef); + let newAncestorRef = await getAncestorRef(codeRef, loader); if (newAncestorRef) { - return isInsideAncestorChain(newAncestorRef, codeRefAncestor); + return isInsideAncestorChain(newAncestorRef, codeRefAncestor, loader); } else { return undefined; } @@ -275,11 +265,12 @@ async function isInsideAncestorChain( export async function getNarrowestType( subclassType: CodeRef | undefined, type: CodeRef, + loader: Loader, ) { let narrowTypeExists: boolean = false; if (subclassType) { narrowTypeExists = - (await isInsideAncestorChain(subclassType, type)) ?? false; + (await isInsideAncestorChain(subclassType, type, loader)) ?? false; } let narrowestType = narrowTypeExists && subclassType ? subclassType : type; return narrowestType;