Repository navigation
Description
Which @angular/* package(s) are the source of the bug?
core
Is this a regression?
Yes
Description
When updating from Angular 18 to 19 I get a "Circular dependency in DI detected" for ChangeDetectionScheduler which I did not get in Angular 18.
The scenario is a bit complex and uses CDK, but I managed to make a simpler reproduction in StackBlitz. The issue related to using effect in the bootstrapping code. Angular 18 used micro tasks and that made it work. In the StackBlitz in DirectionalityCdkOverride it is possible to get it to run by replacing effect with ɵmicrotaskEffect. This however seems like a hack and I suppose that ɵmicrotaskEffect will not be available in the future.
The scenario
We have our own service for keeping language information including text direction LTR/RTL. We therefore override CDK's Directionality with our own version. We also get the users preference when initializing Angular.
The problem/question
It does not seem like our application has circular dependencies as the error message suggests. Also the message refers to ChangeDetectionScheduler which is a class out of our reach. So maybe the error message is not really applicable/informational for this scenario?
Also is this scenario "illegal" or is it a complex scenario that effect does not handle well?
Thanks
Please provide a link to a minimal reproduction of the bug
Please provide the exception or error you saw
Error: NG0200: Circular dependency in DI detected for ChangeDetectionScheduler. Find more at https://angular.dev/errors/NG0200
Please provide the environment you discovered this bug in (run ng version)
Angular CLI: 19.0.2
Node: 20.18.0
Package Manager: npm 10.8.2
OS: win32 x64
Angular: 19.0.1
... animations, cdk, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1900.2
@angular-devkit/build-angular 19.0.2
@angular-devkit/core 19.0.2
@angular-devkit/schematics 19.0.2
@angular/cli 19.0.2
@schematics/angular 19.0.2
rxjs 7.8.1
typescript 5.6.3
zone.js 0.15.0
Anything else?
To see the error in StackBlitz, open the browsers DevTools console.
Activity
JeanMeche commentedon Nov 29, 2024
The issue currently boils down to
Having an effect during app initialization throws a circular DI error.
alxhub commentedon Nov 30, 2024
@JeanMeche i'm not seeing that behavior - when I reduce the repro to
provideAppInitializer(() => { effect(() => {}); });the effect is created and runs properly.From what I can tell, the root cause here is that
ApplicationRefinjectsINTERNAL_APPLICATION_ERROR_HANDLEReagerly on creation. The service in the repro (DirectionalityCdkOverride) ends up getting instantiated as part of the user-providedErrorHandler. This creates the cycle ofChangeDetectionScheduler->ApplicationRef->InternalErrorHandler->ErrorHandler-> (user code) ->ChangeDetectionScheduler.Really, user code shouldn't be instantiated as part of
ApplicationRef's creation - we should make theApplicationRef->InternalErrorHandlerinjection lazy. We could also try to break the cycle atChangeDetectionSchedulerImpl->ApplicationRef, making it possible for CDS to be instantiated beforeApplicationRef.JeanMeche commentedon Nov 30, 2024
Right, I didn't disable the right code on the repro.
#58984, should fix that issue by instantiating
InternalErrorHandlerlazily.mpo-dev commentedon Dec 6, 2024
Any chance this issue can be fixed in the next Angular update? This is currently blocking our update to Angular 19 because of some dependency providers not working due to this circular dependency error.
benelliott commentedon Jan 13, 2025
@mpo-dev It should be possible to work around it by injecting
Injectorinto the offending class (such as a customErrorHandler) and then obtaining the previously-injected dependencies from theInjectora bit later on.For example,
TheTomasJ commentedon Jan 28, 2025
In my case the workaround was to remove the "toObservable" function call from @angular/core/rxjs-interop.
I just removed the signal based logic to be rxjs based.
felixgeissler commentedon Feb 12, 2025
We had a similar issue which might be related to this as well. In our case we provide a custom
GlobalErrorHandlerwhere we depend on theHttpClientto report errors to an API. I'm not certain how managed to get theError: NG0200: Circular dependency in DI detected for ChangeDetectionSchedulererror.Utilizing the
Injectorto inject allGlobalErrorHandlerdependencies solved the issue for us.jmeinlschmidt commentedon Feb 27, 2025
The same exception is being thrown for us when using the
effectfunction in ourErrorHandlerafter upgrading to v19 from v18. Similar to the already resolved issue here #52680refactor(core): Instantiate the ErrorHandler lazily.
refactor(core): Instantiate the ErrorHandler lazily. (angular#58984)
angular-automatic-lock-bot commentedon May 3, 2025
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.