Tamás Polgár
Tamás Polgár

Reputation: 2262

Angular APP_INITIALIZER random compiler bug - has anyone else encountered it yet?

I think I found a bug in Angular, but I am not sure about it, and it actually popped up in an area which I am not very familiar with yet. If anyone could confirm that it may exist, that'd be great, because I am reluctant to report it.

I have a service named SettingsService and it has a function called loadSettings(). As the name tells, it loads settings. It is supposed to be called before the app loads, which I achieve by adding this to the providers part of app.module.ts:

providers: [SettingsService,
                { provide: APP_INITIALIZER,
                  useFactory: (config: SettingsService) => () => config.loadSettings(), 
                  deps: [SettingsService], 
                  multi: true
                },

The bug is that it sometimes works, sometimes not, without even changing either app.module.ts or the service itself. Sometimes I just stop the NG Live Development Server, restart it, and the code suddenly doesn't compile. The error message I receive is:

ERROR in Error encountered resolving symbol values statically. Function calls are not supported. Consider replacing the function or lambda with a reference to an exported function (position 79:31 in the original .ts file), resolving symbol AppModule in c:/Node/boltmotor/src/app/app.module.ts

At first I tried all kind of tinkering, like removing the lambda function and using a named one, but once I rolled back to the original version, and it suddenly worked. I was able to reproduce it, but it is only randomly happening. Just now I got this error message again, and I fixed it simply by adding a space, deleting it, saving, and as ng-cli recompiled the app, it now worked. Not a single bit of actual code was changed!

I wonder if anyone has some idea about the reason.

Upvotes: 1

Views: 104

Answers (1)

yurzui
yurzui

Reputation: 214095

Your code should be statically analized. So

export function init(config: SettingsService) {
  return () => config.loadSettings();
}

providers: [
  SettingsService,
  {
    provide: APP_INITIALIZER,
    useFactory: init,
    deps: [SettingsService],
    multi: true
  }
]

See also

Upvotes: 2

Related Questions