You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Angular 19 forRoot() gets called twice in developer build (v18 only once) #60905
changed the title [-]Angular 19 forRoot() gets called twice[/-][+]Angular 19 forRoot() gets called twice in developer build (v18 only once)[/+]on Apr 17, 2025
It could cause some headaches and debugging for library authors who use it for initialization.
We have a warning in the demo app for the library that appears if forRoot is accidentally initialized twice (a potential regression due to misconfiguration); that's how we detected it.
The reason is that there might be some logic in a library that should only be done once.
This can easily be controlled using an already initialized state if Angular doesn't guarantee that it's only done once, and we want the same behavior in development and production.
If Angular decides to proceed with this, that's fine, but maybe it shouldn't be in a patch release and should be announced in the migration notes.
It wasn't a known side-effect of angular/angular-cli#30021 and that issue is fixing a limitation that used to be present for AOT compiled apps in tests, hence it was included in a patch release. Angular's ModuleWithProviders functions are expected to be side-effect free.
We have a warning in the demo app for the library that appears if forRoot is accidentally initialized twice (a potential regression due to misconfiguration); that's how we detected it.
How is this done, exactly? I assume this is achieved using some global state that is used as side-channel to detect multiple calls?
Closing as a duplicate of #38376, which is now closed as well. In general, this is working as intended. In general, ModuleWithProviders functions shouldn't have side effects. Angular has provideAppInitializer and provideEnvironmentInitializer for adding initialization logic, which is where such validation should take place. A common pattern I've seen is to provide a multiprovider token in forRoot, and use an initializer to check the number of values received for that token. If there's more than one, then forRoot was included too many times.
Activity
[-]Angular 19 forRoot() gets called twice[/-][+]Angular 19 forRoot() gets called twice in developer build (v18 only once)[/+]JeanMeche commentedon Apr 17, 2025
Interesting, something changed in 19.2.7.
19.2.6 and previous version do not exhibit this.
Edit: This is due to #30021.
JoostK commentedon Apr 18, 2025
This behavior itself is tracked in #38376. It started to become relevant for AOT apps again due to angular/angular-cli#30021.
teoeter commentedon Apr 18, 2025
This change might not be suited for a patch.
It could cause some headaches and debugging for library authors who use it for initialization.
We have a warning in the demo app for the library that appears if forRoot is accidentally initialized twice (a potential regression due to misconfiguration); that's how we detected it.
The reason is that there might be some logic in a library that should only be done once.
This can easily be controlled using an already initialized state if Angular doesn't guarantee that it's only done once, and we want the same behavior in development and production.
If Angular decides to proceed with this, that's fine, but maybe it shouldn't be in a patch release and should be announced in the migration notes.
JoostK commentedon Apr 18, 2025
It wasn't a known side-effect of angular/angular-cli#30021 and that issue is fixing a limitation that used to be present for AOT compiled apps in tests, hence it was included in a patch release. Angular's
ModuleWithProvidersfunctions are expected to be side-effect free.How is this done, exactly? I assume this is achieved using some global state that is used as side-channel to detect multiple calls?
alxhub commentedon Apr 18, 2025
Closing as a duplicate of #38376, which is now closed as well. In general, this is working as intended. In general,
ModuleWithProvidersfunctions shouldn't have side effects. Angular hasprovideAppInitializerandprovideEnvironmentInitializerfor adding initialization logic, which is where such validation should take place. A common pattern I've seen is to provide a multiprovider token inforRoot, and use an initializer to check the number of values received for that token. If there's more than one, thenforRootwas included too many times.angular-automatic-lock-bot commentedon May 19, 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.