Skip to content

Commit 7be4dde

Browse files
crisbetoAndrewKushnir
authored andcommitted
fix(core): throw better errors for potential circular references
Currently circular references in user code manifest themselves with an error like `Cannot read properties of undefined (reading 'ɵcmp')`. This is a bit cryptic so these changes add an assertion mentioning circular references. Relates to angular#65917.
1 parent 128aef0 commit 7be4dde

11 files changed

Lines changed: 34 additions & 8 deletions

File tree

goldens/public-api/core/errors.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export const enum RuntimeErrorCode {
3434
// (undocumented)
3535
CYCLIC_DI_DEPENDENCY = -200,
3636
// (undocumented)
37+
DEF_TYPE_UNDEFINED = 919,
38+
// (undocumented)
3739
DEFER_IN_HMR_MODE = -751,
3840
// (undocumented)
3941
DEFER_LOADING_FAILED = -750,

packages/core/src/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export const enum RuntimeErrorCode {
134134
MISSING_DIRECTIVE_DEFINITION = 916,
135135
NO_COMPONENT_FACTORY_FOUND = 917,
136136
EXTERNAL_RESOURCE_LOADING_FAILED = 918,
137+
DEF_TYPE_UNDEFINED = 919,
137138

138139
// Signal integration errors
139140
REQUIRED_INPUT_NO_VALUE = -950,

packages/core/src/render3/def_getters.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {NG_COMP_DEF, NG_DIR_DEF, NG_MOD_DEF, NG_PIPE_DEF} from './fields';
1414
import type {ComponentDef, DirectiveDef, PipeDef} from './interfaces/definition';
1515

1616
export function getNgModuleDef<T>(type: any): NgModuleDef<T> | null {
17+
assertTypeDefined(type, '@NgModule');
1718
return type[NG_MOD_DEF] || null;
1819
}
1920

@@ -36,6 +37,7 @@ export function getNgModuleDefOrThrow<T>(type: any): NgModuleDef<T> | never {
3637
*/
3738

3839
export function getComponentDef<T>(type: any): ComponentDef<T> | null {
40+
assertTypeDefined(type, '@Component');
3941
return type[NG_COMP_DEF] || null;
4042
}
4143

@@ -52,13 +54,26 @@ export function getDirectiveDefOrThrow<T>(type: any): DirectiveDef<T> | never {
5254
}
5355

5456
export function getDirectiveDef<T>(type: any): DirectiveDef<T> | null {
57+
assertTypeDefined(type, '@Directive');
5558
return type[NG_DIR_DEF] || null;
5659
}
5760

5861
export function getPipeDef<T>(type: any): PipeDef<T> | null {
62+
assertTypeDefined(type, '@Pipe');
5963
return type[NG_PIPE_DEF] || null;
6064
}
6165

66+
function assertTypeDefined(type: any, symbolType: string): void {
67+
if (type == null) {
68+
throw new RuntimeError(
69+
RuntimeErrorCode.DEF_TYPE_UNDEFINED,
70+
(typeof ngDevMode === 'undefined' || ngDevMode) &&
71+
`Cannot read ${symbolType} metadata. This can indicate a runtime ` +
72+
`circular dependency in your app that needs to be resolved.`,
73+
);
74+
}
75+
}
76+
6277
/**
6378
* Checks whether a given Component, Directive or Pipe is marked as standalone.
6479
* This will return false if passed anything other than a Component, Directive, or Pipe class

packages/core/test/bundling/animations-standalone/bundle.golden_symbols.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@
339339
"areAnimationSupported",
340340
"arrRemove",
341341
"assertNotDestroyed",
342+
"assertTypeDefined",
342343
"attachPatchData",
343344
"balancePreviousStylesIntoKeyframes",
344345
"balanceProperties",
@@ -858,4 +859,4 @@
858859
],
859860
"lazy": []
860861
}
861-
}
862+
}

packages/core/test/bundling/create_component/bundle.golden_symbols.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@
265265
"areAnimationSupported",
266266
"arrRemove",
267267
"assertNotDestroyed",
268+
"assertTypeDefined",
268269
"attachPatchData",
269270
"baseElement",
270271
"bind",
@@ -689,4 +690,4 @@
689690
],
690691
"lazy": []
691692
}
692-
}
693+
}

packages/core/test/bundling/defer/bundle.golden_symbols.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@
310310
"areAnimationSupported",
311311
"arrRemove",
312312
"assertNotDestroyed",
313+
"assertTypeDefined",
313314
"attachPatchData",
314315
"bind",
315316
"bindingUpdated",
@@ -744,4 +745,4 @@
744745
"writeToDirectiveInput"
745746
]
746747
}
747-
}
748+
}

packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@
371371
"assertControlPresent",
372372
"assertNotDestroyed",
373373
"assertPlatform",
374+
"assertTypeDefined",
374375
"attachPatchData",
375376
"baseElement",
376377
"bind",
@@ -1018,4 +1019,4 @@
10181019
],
10191020
"lazy": []
10201021
}
1021-
}
1022+
}

packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@
375375
"assertControlPresent",
376376
"assertNotDestroyed",
377377
"assertPlatform",
378+
"assertTypeDefined",
378379
"attachPatchData",
379380
"baseElement",
380381
"bind",
@@ -1018,4 +1019,4 @@
10181019
],
10191020
"lazy": []
10201021
}
1021-
}
1022+
}

packages/core/test/bundling/hydration/bundle.golden_symbols.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@
305305
"areAnimationSupported",
306306
"arrRemove",
307307
"assertNotDestroyed",
308+
"assertTypeDefined",
308309
"attachPatchData",
309310
"baseElement",
310311
"bind",
@@ -797,4 +798,4 @@
797798
],
798799
"lazy": []
799800
}
800-
}
801+
}

packages/core/test/bundling/router/bundle.golden_symbols.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@
414414
"arrRemove",
415415
"arrayEquals",
416416
"assertNotDestroyed",
417+
"assertTypeDefined",
417418
"attachPatchData",
418419
"baseElement",
419420
"bind",
@@ -1147,4 +1148,4 @@
11471148
],
11481149
"lazy": []
11491150
}
1150-
}
1151+
}

0 commit comments

Comments
 (0)