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.

Custom decorators not working using AoT and Ivy

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package @angular/compiler-cli (I guess)

Is this a regression?

I had never tried AoT with Ivy

Description

I have a custom property decorator like this:
export function Language(): PropertyDecorator {
    function DecoratorFactory(target: any): void {
        const targetNgOnInit: () => void = target.ngOnInit;
        function ngOnInit(this: any): void {
            
            // Just for example
            console.log('onInit');

            if (targetNgOnInit) {
                targetNgOnInit.apply(this);
            }
        }
        target.ngOnInit = ngOnInit;
    }
    return DecoratorFactory;
}

used as:

@Language() lang: string;

ngOnInit() {
  // For aot
}
  • With AoT compiler, it works (it writes onInit in console)
  • If I enable Ivy and disable AoT, it works
  • If I enable Ivy and AoT, it doesn’t work

πŸ”¬ Minimal Reproduction

angular-ivy-decorators

πŸ”₯ Exception or Error

No errors during compilation or in console. The ngOnInit method in decorator function is ignored.

🌍 Your Environment

Angular Version:


Angular CLI: 8.1.1
Node: 10.14.2
OS: win32 x64
Angular: 8.1.1
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.801.1
@angular-devkit/build-angular     0.801.1
@angular-devkit/build-optimizer   0.801.1
@angular-devkit/build-webpack     0.801.1
@angular-devkit/core              8.1.1
@angular-devkit/schematics        8.1.1
@ngtools/webpack                  8.1.1
@schematics/angular               8.1.1
@schematics/update                0.801.1
rxjs                              6.4.0
typescript                        3.4.5
webpack                           4.35.2

Anything else relevant? Tried without success also the next version of Angular.

Thanks

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:34
  • Comments:61 (18 by maintainers)

github_iconTop GitHub Comments

24reactions
smnbbrvcommented, Feb 9, 2020

A 1-cent from me. Before Angular 9 I used a Memoize decorator across all my components in many apps and now it comes to the point that I can’t use it anymore.

  @Memoize()
  getPermission(account: Account) {
    return this.permissions.filter(p => account.permissions.includes(p.id));
  }

As one might see, the operation is quite heavy and I cache it after one run. Now after the production build (in a non-prod build it works fine) it simply throws the runtime error Reference error: Account is undefined. I spent quite a lot of time until it became clear that the decorator was the reason.

Sad, but looks like I need to find another way now, probably less magical.

However, this is a breaking change and it’s not mentioned anywhere. Maybe it’s worth to give at least some note that custom decorators will fail now on the components’ members

18reactions
smnbbrvcommented, Feb 11, 2020

This post will probably get million times downvoted, however my personal finding is since I use early beta versions of Angular 2: do not ever extend angular components and do not use custom decorators on angular components and their members. This always led to the problems; during upgrades 2 -> @angular/cli -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 I rewrote tons of components just because of these two things.

Here are similar AoT issues that are not fixed for years and it seems to not happen soon https://github.com/angular/angular/issues/23038 https://github.com/angular/angular/issues/16023 and many others.

I clearly understand the reasoning behind. The decorators are changing the content, behaviour and the lifecycle of the components; they can actually rewrite everything we know of the component in the runtime. It’s really hard (impossible?) to catch this with the custom compiler and create a performant application. It’s a very hard job that Angular team is already doing. Thank you for that!

Maybe problem is not in Angular. Probably we should change the way we think.

But, Angular team, please help us. The framework documentation is super-silent about all these issues. I will probably repeat myself here https://github.com/angular/angular/issues/31495#issuecomment-583767197 and ask for the easiest solution. Maybe politically it’s not the best for angular, or there are some religious beliefs against that, but please add those two clear lines to the components’ documentation:

  • we do not yet fully support components inheritance
  • we do not yet fully support custom decorators on components

And then the issues like this would be resolved. The StackOverflow answers would not propose to extend components. People would not be annoyed of rewriting their applications.

I am pretty sure everybody here will have no problems with using no decorators. The only thing that is missing: this should be properly communicated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular AoT Custom Decorator Error encountered ...
It seems this approach doesn't work with inheritance (decorators on the parent class are not executed). Is there a way to do that?...
Read more >
Angular Ivy migration, a contrasted feeling, this is not what ...
This article is a feedback of our Angular Ivy migration. ... The custom decorators are not working using AoT and Ivy.
Read more >
Migration for missing @Injectable() decorators and ...
This schematic adds an @Injectable() decorator to classes which are provided in the application but are not decorated. The schematic updates providers whichΒ ......
Read more >
Ahead-of-time (AOT) compilation - Angular
The AOT compiler does not support function expressions and arrow functions, also called lambda functions. Consider the following component decorator:.
Read more >
Ahead of Time Compilation - AoT in Angular (Performance ...
If you are using Angular CLI, the built-in webpack process takes care of it. ... Custom decorators won't be used while generating code....
Read more >

github_iconTop Related Medium Post

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