Skip to content

Angular 19: NG0200: Circular dependency in DI detected for ChangeDetectionScheduler #58970

Closed

Description

@bjarketrux

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

https://stackblitz.com/edit/stackblitz-starters-esaf7e?file=src%2Fdirectionality-cdk-override.service.ts

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

  1. added
    area: core
    on Nov 29, 2024
  2. modified the milestone: needsTriage on Nov 29, 2024
  3. JeanMeche commented on Nov 29, 2024

    @JeanMeche
    Member

    The issue currently boils down to

        provideAppInitializer(() => {
          effect(() => {}) // throws NG0200
        }),

    Having an effect during app initialization throws a circular DI error.

  4. added
    core: reactivity
    on Nov 29, 2024
  5. alxhub commented on Nov 30, 2024

    @alxhub
    Member

    @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 ApplicationRef injects INTERNAL_APPLICATION_ERROR_HANDLER eagerly on creation. The service in the repro (DirectionalityCdkOverride) ends up getting instantiated as part of the user-provided ErrorHandler. This creates the cycle of ChangeDetectionScheduler -> ApplicationRef -> InternalErrorHandler -> ErrorHandler -> (user code) -> ChangeDetectionScheduler.

    Really, user code shouldn't be instantiated as part of ApplicationRef's creation - we should make the ApplicationRef -> InternalErrorHandler injection lazy. We could also try to break the cycle at ChangeDetectionSchedulerImpl -> ApplicationRef, making it possible for CDS to be instantiated before ApplicationRef.

  6. JeanMeche commented on Nov 30, 2024

    @JeanMeche
    Member

    Right, I didn't disable the right code on the repro.

    #58984, should fix that issue by instantiating InternalErrorHandler lazily.

  7. mpo-dev commented on Dec 6, 2024

    @mpo-dev

    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.

  8. benelliott commented on Jan 13, 2025

    @benelliott
    Contributor

    @mpo-dev It should be possible to work around it by injecting Injector into the offending class (such as a custom ErrorHandler) and then obtaining the previously-injected dependencies from the Injector a bit later on.

    For example,

    // Throws the error:
    @Injectable()
    export class ErrorHandlerService implements ErrorHandler {
        private readonly dialog = inject(MatDialog);
    
        public handleError(error: any): void {
            this.dialog
                .open(ErrorDialogComponent, {
                    data: { error },
                    width: '80vw',
                    maxWidth: '600px',
                    maxHeight: '800px',
                    autoFocus: false
                });
        }
    }
    
    // Works:
    @Injectable()
    export class ErrorHandlerService implements ErrorHandler {
        private readonly injector = inject(Injector);
    
        public handleError(error: any): void {
            this.injector.get(MatDialog)
                .open(ErrorDialogComponent, {
                    data: { error },
                    width: '80vw',
                    maxWidth: '600px',
                    maxHeight: '800px',
                    autoFocus: false
                });
        }
    }
    
  9. TheTomasJ commented on Jan 28, 2025

    @TheTomasJ

    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.

  10. felixgeissler commented on Feb 12, 2025

    @felixgeissler

    We had a similar issue which might be related to this as well. In our case we provide a custom GlobalErrorHandler where we depend on the HttpClient to report errors to an API. I'm not certain how managed to get the Error: NG0200: Circular dependency in DI detected for ChangeDetectionScheduler error.

    Utilizing the Injector to inject all GlobalErrorHandler dependencies solved the issue for us.

  11. jmeinlschmidt commented on Feb 27, 2025

    @jmeinlschmidt

    The same exception is being thrown for us when using the effect function in our ErrorHandler after upgrading to v19 from v18. Similar to the already resolved issue here #52680

  12. added a commit that references this issue on Apr 1, 2025
    82df85e
  13. added a commit that references this issue on Apr 2, 2025
    1c1ad12
  14. angular-automatic-lock-bot commented on May 3, 2025

    @angular-automatic-lock-bot

    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.

  15. locked and limited conversation to collaborators on May 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corecore: dicore: reactivity

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions