fix(elements): resolve environment injector for custom elements - #70454
fix(elements): resolve environment injector for custom elements#70454surajy93 wants to merge 1 commit into
Conversation
7150d13 to
ce35cce
Compare
ce35cce to
4e73a95
Compare
JeanMeche
left a comment
There was a problem hiding this comment.
AGENT: Thanks for this fix. I've left an inline suggestion because the current implementation accidentally introduces a dependency injection regression for custom elements created with a NodeInjector. Please review the suggested change.
| const environmentInjector = | ||
| this.injector instanceof EnvironmentInjector | ||
| ? this.injector | ||
| : this.injector.get(EnvironmentInjector); |
There was a problem hiding this comment.
AGENT: I looked into this, and changing childInjector's parent from this.injector to environmentInjector causes a major regression in dependency injection. If this.injector is a NodeInjector, any custom element created with it will completely lose access to the NodeInjector's provider hierarchy because it's bypassed in favor of the EnvironmentInjector.
The core issue of the DevTools infinite recursion is actually fixed perfectly by your changes in injector_discovery_utils.ts, which now gracefully skips NodeInjectors when walking up the parent chain!
Because of that fix, we can revert childInjector's parent back to this.injector while still correctly setting environmentInjector: environmentInjector in the createComponent options. This satisfies DevTools and preserves DI resolution from the NodeInjector.
| : this.injector.get(EnvironmentInjector); | |
| const childInjector = Injector.create({providers: [], parent: this.injector}); |
There was a problem hiding this comment.
Makes total sense! I've updated childInjector's parent back to this.injector to preserve the NodeInjector provider hierarchy while keeping the resolved environmentInjector passed to createComponent. Amended and pushed.
Resolve the correct EnvironmentInjector from the provided injector in ComponentNgElementStrategy instead of casting a NodeInjector directly. This prevents a malformed injector hierarchy which causes infinite loop recursion when Angular DevTools resolves the injector resolution path. Also, add a defensive check in getModuleInjectorOfNodeInjector to ensure we don't cycle when a NodeInjector is configured as the parent of an R3Injector. Fixes angular#70452
4e73a95 to
c08039a
Compare
Resolve the correct EnvironmentInjector from the provided injector in ComponentNgElementStrategy instead of casting a NodeInjector directly. This prevents a malformed injector hierarchy which causes infinite loop recursion when Angular DevTools resolves the injector resolution path.
Also, add a defensive check in getModuleInjectorOfNodeInjector to ensure we don't cycle when a NodeInjector is configured as the parent of an R3Injector.
Fixes #70452