angular: Angular v5 ngc compiler: Error encountered in metadata generated for exported symbol 'Subscription'

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

Building an Angulary library using 5.0.0-beta.4 ngc compiler, I’m getting the following error:

Error: Error: C:/Users/Roberto/Documents/GitHub/angular-library-starter/node_modules/rxjs/Subscription.d.ts:21:1: Error encountered in met
adata generated for exported symbol 'Subscription':
 C:/Users/Roberto/Documents/GitHub/angular-library-starter/node_modules/rxjs/Subscription.d.ts:22:12: Metadata collected contains an error
 that will be reported at runtime: Only initialized variables and constants can be referenced because the value of this variable is needed
 by the template compiler.
  {"__symbolic":"error","message":"Variable not initialized","line":21,"character":11}
    at C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\tsc-wrapped\src\collector.js:658:27
    at Array.forEach (native)
    at validateMetadata (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\tsc-wrapped\src\collector.js:646:
42)
    at MetadataCollector.getMetadata (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\tsc-wrapped\src\coll
ector.js:503:17)
    at LowerMetadataCache.getMetadataAndRequests (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\compiler
-cli\src\transformers\lower_expressions.js:169:39)
    at LowerMetadataCache.ensureMetadataAndRequests (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\compi
ler-cli\src\transformers\lower_expressions.js:147:27)
    at LowerMetadataCache.getMetadata (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\compiler-cli\src\tr
ansformers\lower_expressions.js:139:21)
    at CompilerHost.upgradeVersion1Metadata (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\compiler-cli\
src\compiler_host.js:236:45)
    at CompilerHost.getMetadataFor (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\compiler-cli\src\compi
ler_host.js:195:30)
    at StaticSymbolResolver.getModuleMetadata (C:\Users\Roberto\Documents\GitHub\angular-library-starter\node_modules\@angular\compiler\bu
ndles\compiler.umd.js:25829:62)

Minimal reproduction of the problem with instructions

Download the following repo: https://github.com/robisim74/angular-library-starter

Upgrade to Angular 5.0.0-beta.4 and run npm run build.

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

Up to Angular 5.0.0-beta.3 it worked without problems. The library does not use itself rxjs/Subscription.

The only way to avoid the error seems to be to set strictMetadataEmit to false, but it is not recommended.

Environment


Angular version: 5.0.0-beta.4

Thanks.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 22 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Got this problem. Added // @dynamic before the class that declares static method:

// @dynamic
export class SomeClass {
    public static get() { return 'someValue'; } 
}

I believe that static methods within classes are used ie for “MyModule.forRoot()”, and angular thinks you are defining a module. As your function would not be compatible with AOT, it compilation fails, and the // @dynamic directive is like “don’t worry Angular, this class is not intented to be used for bootstapping some modules.”

After upgrading my application to angular 6.0.0-rc.1 and rxjs 6.0.0-rc.0, it seems that the problem has come back when i use ngc. If i add // @dynamic on node_modules/rxjs/internal/nSubscription.d.ts, it fixes the problem

@tommueller Try below code

export class JsUtils {
    private static entityMap = {
        '&': '&',
        '<': '&lt;',
        '>': '&gt;',
        '"': '&quot;',
        '\'': '&#39;',
        '/': '&#x2F;'
    };
    public static escapeHtml(source: string): string {
       let regEx = /[&<>"'\/]/g; 
       return String(source).replace(regEx, (s) => this.entityMap[s]);
    }
}

I had the Expression form not supported error, and it was an inline typing, and it was solved exporting an interface and using it to type that parameter. Thanks to this article tho: https://blog.angularindepth.com/making-your-angular-2-library-statically-analyzable-for-aot-e1c6f3ebedd5

I fixed my issue with providing initial value for module variable as below:

export class AppUniverseModule { static config: any = null; // THIS WAS THE FIX static fromConfig(config: any): ModuleWithProviders { AppUniverseModule.config = config; return { ngModule: AppUniverseModule }; }

Confirmed this is an issue in 5.0.0-beta.6 and the issue is not seen in 5.0.0-beta.3. This issue blocks the ability to build libraries per the spec in https://github.com/steveblue/angular2-rollup

Hey @ochezeau i have an Angular 5 project with same issue, static fields in an injectable service in a library, but the // @dynamic comment does nothing in my case, do i really need to update ngpackagr version?:

image