Skip to content

Commit 2b6da93

Browse files
crisbetoalxhub
authored andcommitted
fix(core): incorrectly throwing error for self-referencing component (#50559)
Components are implied to be self-referencing, but if they explicitly set themselves in the `imports` array, they would throw an error because we weren't filtering them out. Fixes #50525. PR Close #50559
1 parent ef35ccd commit 2b6da93

2 files changed

Lines changed: 65 additions & 3 deletions

File tree

packages/core/src/render3/jit/directive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ function getStandaloneDefFunctions(type: Type<any>, imports: Type<any>[]): {
245245
// Standalone components are always able to self-reference, so include the component's own
246246
// definition in its `directiveDefs`.
247247
cachedDirectiveDefs = [getComponentDef(type)!];
248-
const seen = new Set<Type<unknown>>();
248+
const seen = new Set<Type<unknown>>([type]);
249249

250250
for (const rawDep of imports) {
251251
ngDevMode && verifyStandaloneImport(rawDep, type);

packages/core/test/acceptance/component_spec.ts

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {DOCUMENT} from '@angular/common';
10-
import {ApplicationRef, Component, ComponentRef, createComponent, createEnvironmentInjector, Directive, ElementRef, EmbeddedViewRef, EnvironmentInjector, inject, Injectable, InjectionToken, Injector, Input, NgModule, OnDestroy, reflectComponentType, Renderer2, Type, ViewChild, ViewContainerRef, ViewEncapsulation, ɵsetDocument} from '@angular/core';
9+
import {DOCUMENT, NgIf} from '@angular/common';
10+
import {ApplicationRef, Component, ComponentRef, createComponent, createEnvironmentInjector, Directive, ElementRef, EmbeddedViewRef, EnvironmentInjector, forwardRef, inject, Injectable, InjectionToken, Injector, Input, NgModule, OnDestroy, reflectComponentType, Renderer2, Type, ViewChild, ViewContainerRef, ViewEncapsulation, ɵsetDocument} from '@angular/core';
1111
import {stringifyForError} from '@angular/core/src/render3/util/stringify_utils';
1212
import {TestBed} from '@angular/core/testing';
1313
import {expect} from '@angular/platform-browser/testing/src/matchers';
@@ -400,6 +400,68 @@ describe('component', () => {
400400
.toThrowError(
401401
/NG0300: Multiple components match node with tagname comp: CompA and CompB/);
402402
});
403+
404+
it('should not throw if a standalone component imports itself', () => {
405+
@Component({
406+
selector: 'comp',
407+
template: '<comp *ngIf="recurse"/>hello',
408+
standalone: true,
409+
imports: [Comp, NgIf]
410+
})
411+
class Comp {
412+
@Input() recurse = false;
413+
}
414+
415+
@Component({
416+
template: '<comp [recurse]="true"/>',
417+
standalone: true,
418+
imports: [Comp],
419+
})
420+
class App {
421+
}
422+
423+
let textContent = '';
424+
425+
expect(() => {
426+
const fixture = TestBed.createComponent(App);
427+
fixture.detectChanges();
428+
textContent = fixture.nativeElement.textContent.trim();
429+
}).not.toThrow();
430+
431+
// Ensure that the component actually rendered.
432+
expect(textContent).toBe('hellohello');
433+
});
434+
435+
it('should not throw if a standalone component imports itself using a forwardRef', () => {
436+
@Component({
437+
selector: 'comp',
438+
template: '<comp *ngIf="recurse"/>hello',
439+
standalone: true,
440+
imports: [forwardRef(() => Comp), NgIf]
441+
})
442+
class Comp {
443+
@Input() recurse = false;
444+
}
445+
446+
@Component({
447+
template: '<comp [recurse]="true"/>',
448+
standalone: true,
449+
imports: [Comp],
450+
})
451+
class App {
452+
}
453+
454+
let textContent = '';
455+
456+
expect(() => {
457+
const fixture = TestBed.createComponent(App);
458+
fixture.detectChanges();
459+
textContent = fixture.nativeElement.textContent.trim();
460+
}).not.toThrow();
461+
462+
// Ensure that the component actually rendered.
463+
expect(textContent).toBe('hellohello');
464+
});
403465
});
404466

405467
it('should use a new ngcontent attribute for child elements created w/ Renderer2', () => {

0 commit comments

Comments
 (0)