Skip to content

effect and computed throws circular dependency when ErrorHandler creates effect computed during construction (or one of its dependencies does) #52680

Closed

Description

@blueiceprj

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

Yes

Description

After upgrade to 17 signal objects not working properly

Injectable({ providedIn: 'root' })
export class PrincipalService {
    _loginCompleted = signal<boolean>(false);
    _token = signal<string>(null);
    user = signal<SessionState>(createInitialSessionState());
    user$ = toObservable(this.user);
apiCount = signal<number>(0);
constructor() {
        effect(() => {
            setTimeout(() => {
                this.visible.set(this.apiCount() != 0);
            }, 0);
        }, {
            allowSignalWrites: true
        });
    }

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

Error: NG0200: Circular dependency in DI detected for ErrorHandler. Find more at https://angular.io/errors/NG0200
    at throwCyclicDependencyError (core.mjs:292:11)
    at R3Injector.hydrate (core.mjs:6158:13)
    at R3Injector.get (core.mjs:6033:33)
    at effect (core.mjs:14259:35)
    at toObservable (rxjs-interop.mjs:48:27)
    at new PrincipalService (principal.service.ts:22:25)
    at Object.PrincipalService_Factory [as factory] (ɵfac.js:1:1)
    at core.mjs:6164:43
    at runInInjectorProfilerContext (core.mjs:867:9)
    at R3Injector.hydrate (core.mjs:6163:17)



Error: NG0200: Circular dependency in DI detected for ErrorHandler. Find more at https://angular.io/errors/NG0200
    at throwCyclicDependencyError (core.mjs:292:11)
    at R3Injector.hydrate (core.mjs:6158:13)
    at R3Injector.get (core.mjs:6033:33)
    at effect (core.mjs:14259:35)
    at new FuseProgressBarService (progress-bar.service.ts:24:15)
    at Object.FuseProgressBarService_Factory [as factory] (ɵfac.js:1:1)
    at core.mjs:6164:43
    at runInInjectorProfilerContext (core.mjs:867:9)
    at R3Injector.hydrate (core.mjs:6163:17)
    at R3Injector.get (core.mjs:6033:33)

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 17.0.0
Node: 18.18.2
Package Manager: npm 9.8.1
OS: darwin x64

Angular: 17.0.0
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.1700.0
@angular-devkit/build-angular      17.0.0
@angular-devkit/build-ng-packagr   0.1002.0
@angular-devkit/core               17.0.0
@angular-devkit/schematics         17.0.0
@angular/cdk                       16.2.12
@angular/flex-layout               15.0.0-beta.42
@angular/material                  16.2.12
@angular/material-moment-adapter   16.2.12
@angular/youtube-player            16.2.12
@schematics/angular                17.0.0
rxjs                               7.8.1
typescript                         5.2.2
zone.js                            0.14.2

Anything else?

No response

Activity

  1. atscott commented on Nov 8, 2023

    @atscott
    Contributor

    Can you provide a reproduction? The effect implementation attempts to inject ErrorHandler so I'm assuming you are using an effect inside a class that your custom ErrorHandler also injects.

    Edit: Here's a reproduction. Can you confirm that this is similar to your application?

  2. added
    area: core
    core: reactivity
    on Nov 8, 2023
  3. modified the milestone: needsTriage on Nov 8, 2023
  4. blueiceprj commented on Nov 8, 2023

    @blueiceprj
    Author

    Sorry! Its a huge project. Thats just part of code I shared. They are just root injected services. But these services call by custom error handler.

  5. added
    needs reproduction
    on Nov 8, 2023
  6. modified the milestones: needsTriage, Backlog on Nov 8, 2023
  7. removed
    needs reproduction
    on Nov 8, 2023
  8. modified the milestones: Backlog, needsTriage on Nov 8, 2023
  9. changed the title [-]signal effect and toObservable objects throws error[/-] [+]`effect` throws circular dependency when `ErrorHandler` creates `effect` during construction (or one of its dependencies does)[/+] on Nov 8, 2023
  10. blueiceprj commented on Nov 8, 2023

    @blueiceprj
    Author
  11. changed the title [-]`effect` throws circular dependency when `ErrorHandler` creates `effect` during construction (or one of its dependencies does)[/-] [+]`effect` and `computed` throws circular dependency when `ErrorHandler` creates `effect` `computed` during construction (or one of its dependencies does)[/+] on Nov 8, 2023
  12. 12 remaining items

  13. added a commit that references this issue on Dec 28, 2023
    7f6a38d
  14. added a commit that references this issue on Jan 3, 2024
    f9120d7
  15. blueiceprj commented on Jan 10, 2024

    @blueiceprj
    Author

    Fixed. Thanks guys

  16. blueiceprj commented on Jan 11, 2024

    @blueiceprj
    Author

    However, if you use ErrorHandler, there is a problem with this too. A simple data type, string, boolean etc. If I use them, it works, but when I change the data type with a complex interface like in this example, effect doesn't work. It works properly if you remove ErrorHandler.

    Please check this example https://stackblitz.com/edit/stackblitz-starters-2fvmzs?file=src%2Fmain.ts

    @atscott

  17. atscott commented on Jan 11, 2024

    @atscott
    Contributor

    @blueiceprj I don't understand what your demo is trying to show or what you're saying isn't working. Can you explain this more?

  18. blueiceprj commented on Jan 11, 2024

    @blueiceprj
    Author

    Same example. But I changed signal object from primitive type to object type like string to LoginState

    export interface UserState {
      loggedIn: boolean;
      username: string;
    }
    

    And try to change properties of this class (loggedIn, username) like that.

    effect(
          () => {
            console.log('Changed', this.loginState());
          },
          {
            allowSignalWrites: true,
          }
        );
    .............................
    .......................
    login() {
        this.loginState.update((v) => {
          /* BUT IT DOESN'T WORK */
          /*
          v.loggedIn = true;
          v.username = 'anyone';
          return v;
        */
          /* BUT IT WORKS */
          const newLogin = { ...v };
          newLogin.loggedIn = true;
          newLogin.username = 'anyone';
          return newLogin;
        });
      }
    

    First Version

    Ekran Resmi 2024-01-11 23 10 32

    Second version

    Ekran Resmi 2024-01-11 23 09 30

    Result : I always need to copy of signal value to trigger effect function.

  19. atscott commented on Jan 11, 2024

    @atscott
    Contributor

    Result : I always need to copy of signal value to trigger effect function.

    Ah, I see - I was expecting this to be related to the ErrorHandler again. This is about signal updates and mutability. The default equality function for signals is comparing by reference. If you want a mutable signal, you have to change its equality function. I've updated the example here

  20. blueiceprj commented on Jan 11, 2024

    @blueiceprj
    Author

    Ok. I see. Thanks.

  21. added a commit that references this issue on Jan 23, 2024
    0a26a5f
  22. added a commit that references this issue on Jan 26, 2024
    eb15277
  23. added a commit that references this issue on Jan 26, 2024
    6adfee6
  24. angular-automatic-lock-bot commented on Feb 11, 2024

    @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.

  25. locked and limited conversation to collaborators on Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3area: corebugcore: reactivitycross-cutting: signalsstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions