question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AOT build breaks with InjectionToken used cross library boundaries

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x ] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

I get the following error when using “ng build --prod”

ERROR in Error: Internal error: unknown identifier undefined
    at Object.importExpr$$1 [as importExpr] (C:\d\ContentHub\samples-wch-sdk\navigation\node_modules\@angular\compiler\bundles\compiler.umd.js:24097:23)
    at tokenExpr (C:\d\ContentHub\samples-wch-sdk\navigation\node_modules\@angular\compiler\bundles\compiler.umd.js:18447:39)
    at providerDef (C:\d\ContentHub\samples-wch-sdk\navigation\node_modules\@angular\compiler\bundles\compiler.umd.js:18349:20)
    at C:\d\ContentHub\samples-wch-sdk\navigation\node_modules\@angular\compiler\bundles\compiler.umd.js:18574:77
    at Array.map (<anonymous>)
    at NgModuleCompiler.compile (C:\d\ContentHub\samples-wch-sdk\navigation\node_modules\@angular\compiler\bundles\compiler.umd.js:18574:44)
    at AotCompiler._compileModule (C:\d\ContentHub\samples-wch-sdk\navigation\node_modules\@angular\compiler\bundles\compiler.umd.js:24030:32)
    at C:\d\ContentHub\samples-wch-sdk\navigation\node_modules\@angular\compiler\bundles\compiler.umd.js:23942:66
    at Array.forEach (<anonymous>)
    at AotCompiler._compileImplFile (C:\d\ContentHub\samples-wch-sdk\navigation\node_modules\@angular\compiler\bundles\compiler.umd.js:23942:19)
    at C:\d\ContentHub\samples-wch-sdk\navigation\node_modules\@angular\compiler\bundles\compiler.umd.js:23855:87
    at Array.map (<anonymous>)
    at AotCompiler.emitAllImpls (C:\d\ContentHub\samples-wch-sdk\navigation\node_modules\@angular\compiler\bundles\compiler.umd.js:23855:52)
    at CodeGenerator.emit (C:\d\ContentHub\samples-wch-sdk\navigation\node_modules\@angular\compiler-cli\src\codegen.js:42:46)
    at C:\d\ContentHub\samples-wch-sdk\navigation\node_modules\@angular\compiler-cli\src\codegen.js:33:61
    at <anonymous>

As far as I can tell, the undefined identifier is the value of the provider field in a ClassProvider. Doing a console log for the AST node yields the following result:

providerAST {"token":{"identifier":{"reference":{"filePath":"C:/d/ContentHub/samples-wch-sdk/navigation/node_modules/@angular/common/common.d.ts","name":"NgLocalization","members":[]}}}
,"multiProvider":false,"eager":false,"providers":[{"token":{"identifier":{"reference":{"filePath":"C:/d/ContentHub/samples-wch-sdk/navigation/node_modules/@angular/common/common.d.ts","
name":"NgLocalization","members":[]}}},"useClass":{"reference":{"filePath":"C:/d/ContentHub/samples-wch-sdk/navigation/node_modules/@angular/common/common.d.ts","name":"NgLocaleLocaliza
tion","members":[]},"diDeps":[{"isAttribute":false,"isHost":false,"isSelf":false,"isSkipSelf":false,"isOptional":false,"token":{"identifier":{"reference":{"filePath":"C:/d/ContentHub/sa
mples-wch-sdk/navigation/node_modules/@angular/core/core.d.ts","name":"LOCALE_ID","members":[]}}}}],"lifecycleHooks":[]},"useFactory":null,"deps":[{"isAttribute":false,"isHost":false,"i
sSelf":false,"isSkipSelf":false,"isOptional":false,"token":{"identifier":{"reference":{"filePath":"C:/d/ContentHub/samples-wch-sdk/navigation/node_modules/@angular/core/core.d.ts","name
":"LOCALE_ID","members":[]}}}}],"multi":false}],"providerType":0,"lifecycleHooks":[],"sourceSpan":{"start":{"file":{"content":"","url":"in NgModule WchNgLoggingModule in C:/d/ContentHub
/samples-wch-sdk/navigation/node_modules/ibm-wch-sdk-ng-logger/ibm-wch-sdk-ng-logger.d.ts"},"offset":-1,"line":-1,"col":-1},"end":{"file":{"content":"","url":"in NgModule WchNgLoggingMo
dule in C:/d/ContentHub/samples-wch-sdk/navigation/node_modules/ibm-wch-sdk-ng-logger/ibm-wch-sdk-ng-logger.d.ts"},"offset":-1,"line":-1,"col":-1},"details":null}}
providerAST {"token":{"identifier":{}},"multiProvider":false,"eager":false,"providers":[{"token":{"identifier":{}},"useClass":{"reference":{"filePath":"C:/d/ContentHub/samples-wch-sdk/n
avigation/node_modules/ibm-wch-sdk-ng-logger/ibm-wch-sdk-ng-logger.d.ts","name":"ɵb","members":[],"test":"Carsten"},"diDeps":[],"lifecycleHooks":[]},"useFactory":null,"deps":[],"multi":
false}],"providerType":0,"lifecycleHooks":[],"sourceSpan":{"start":{"file":{"content":"","url":"in NgModule WchNgLoggingModule in C:/d/ContentHub/samples-wch-sdk/navigation/node_modules
/ibm-wch-sdk-ng-logger/ibm-wch-sdk-ng-logger.d.ts"},"offset":-1,"line":-1,"col":-1},"end":{"file":{"content":"","url":"in NgModule WchNgLoggingModule in C:/d/ContentHub/samples-wch-sdk/
navigation/node_modules/ibm-wch-sdk-ng-logger/ibm-wch-sdk-ng-logger.d.ts"},"offset":-1,"line":-1,"col":-1},"details":null}}
tokenMeta {"identifier":{}} {"statements":[],"genFilePath":"C:/d/ContentHub/samples-wch-sdk/navigation/node_modules/ibm-wch-sdk-ng-logger/ibm-wch-sdk-ng-logger.ngfactory.ts"}```

The scenario is this:

  • I have an ng4 CLI application using the standard CLI build
  • this application uses two libraries A and B, both ng4 libraries built against the ng4 module spec
  • A defines an InjectionToken that allows to inject services
  • B provides a service using the Injection token provided by A B’m module looks like this (in TS)
// the provider
export const PROVIDER: InjectionToken<LoggerFactory> = WchLoggerFactory;

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [
  ],
  providers: [{ provide: PROVIDER, useClass: Ng2LoggerFactory }],
  exports: [
  ],
  entryComponents: [
  ]
})
export class WchNgLoggingModule {

Where ‘WchLoggerFactory’ is imported from A.

The problem only occurs if I use modules, if I build everything in one large monolithic application, the build works.

Expected behavior

I expect that InjectionTokens can be used across library boundaries to provide services.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


Angular CLI version: 1.4.7
 
For Tooling issues:
- Node version: v8.6.0
- Platform:  Windows 7

Others:

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:8
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
kriscolemancommented, Feb 18, 2018

I’m having this same issue with the exact same error, it builds find in dev but fails with this error with --prod. Any idea how to fix it?

3reactions
montella1507commented, Jun 9, 2019

I Have similar problem…

I use @nrwl/nx, tokens are defined in @framework library and used in app.module to provide instances. In non-production build everything works as expected.

In production build, everything is built without any error, however instanced are NOT provided.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular - Can InjectionToken be used with AOT Compilation?
This works fine, however now I've been trying to build the application using AOT compilation. When I run the application with AOT, the...
Read more >
angular/angular - Gitter
hello guys can anybody tell me where i am making mistake. I have used router outlet in app.component.html template and its giving me...
Read more >
Dependency injection in action - Angular
Using a custom provider allows you to provide a concrete implementation for implicit dependencies, such as built-in browser APIs. The following example uses...
Read more >
@angular/core@15.0.4 - jsDocs.io
Internal injection token that can used to access an instance of a ... might be required by different libraries or parts of the...
Read more >
Angular Interview Questions — You Must Know (2022) - Medium
Angular built entirely in TypeScript and used as a primary language. ... Modules are logical boundaries in your application and the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found