Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Nested V2 Addons #1327

Merged
merged 2 commits into from
Mar 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 35 additions & 29 deletions packages/addon-shim/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,22 @@ export function addonV1Shim(directory: string, options: ShimOptions = {}) {
return tree;
}

let autoImportInstance: EAI2Instance | undefined;

return {
name: pkg.name,
included(this: AddonInstance, ...args: unknown[]) {
included(
this: AddonInstance & {
registerV2Addon(name: string, dir: string): void;
},
...args: unknown[]
) {
let parentOptions;
let parentName: string;
if (isDeepAddonInstance(this)) {
parentOptions = this.parent.options;
parentName = this.parent.name;
} else {
parentOptions = this.app.options;
parentName = this.parent.name();
}

// if we're being used by a v1 package, that package needs ember-auto-import 2
if ((this.parent.pkg['ember-addon']?.version ?? 1) < 2) {
let autoImport = locateAutoImport(this.parent.addons);
if (!autoImport.present) {
throw new Error(
`${parentName} needs to depend on ember-auto-import in order to use ${this.name}`
);
}

if (!autoImport.satisfiesV2) {
throw new Error(
`${parentName} has ember-auto-import ${autoImport.version} which is not new enough to use ${this.name}. It needs to upgrade to >=2.0`
);
}
autoImportInstance = autoImport.instance;
autoImportInstance.registerV2Addon(this.name, directory);
} else {
// if we're being used by a v2 addon, it also has this shim and will
// forward our registration onward to ember-auto-import
(this.parent as EAI2Instance).registerV2Addon(this.name, directory);
}
this.registerV2Addon(this.name, directory);

if (options.disabled) {
disabled = options.disabled(parentOptions);
Expand Down Expand Up @@ -158,8 +138,34 @@ export function addonV1Shim(directory: string, options: ShimOptions = {}) {
return isInside(directory, appInstance.project.root);
},

registerV2Addon(name: string, root: string): void {
autoImportInstance!.registerV2Addon(name, root);
registerV2Addon(this: AddonInstance, name: string, root: string): void {
let parentName: string;
if (isDeepAddonInstance(this)) {
parentName = this.parent.name;
} else {
parentName = this.parent.name();
}

// if we're being used by a v1 package, that package needs ember-auto-import 2
if ((this.parent.pkg['ember-addon']?.version ?? 1) < 2) {
let autoImport = locateAutoImport(this.parent.addons);
if (!autoImport.present) {
throw new Error(
`${parentName} needs to depend on ember-auto-import in order to use ${this.name}`
);
}

if (!autoImport.satisfiesV2) {
throw new Error(
`${parentName} has ember-auto-import ${autoImport.version} which is not new enough to use ${this.name}. It needs to upgrade to >=2.0`
);
}
autoImport.instance.registerV2Addon(name, root);
} else {
// if we're being used by a v2 addon, it also has this shim and will
// forward our registration onward to ember-auto-import
(this.parent as EAI2Instance).registerV2Addon(name, root);
Copy link
Collaborator

Choose a reason for hiding this comment

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

How do we know it's safe to call this on the parent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's the biggest question mark I have, quote from the issue above:

Why I'm saying this is only a suggested fix: I'm not sure, if this piece of code is in the intention of the comment or if that means a different implementation (given I have basically no real knowledge about embroider).

which basically targets this line.

But following the analysis I put in the issue, there is either a autoImportInstance or this.parent with registerV2Addon available.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Potentially, you can safeguard this. But I'd say, it would be better to throw an error over silently do nothing and not registering the addon at all, without a dev knowing about it ?!?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's safe once refactored inside the if (this.parent.pkg['ember-addon']?.version ?? 1) < 2) check.

}
},
};
}
Expand Down