-
Notifications
You must be signed in to change notification settings - Fork 26.2k
[ivy rc4] new unbound (static) input handling breaks existing code and makes component lifecycle inconsistent #34227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Another case pointing to the same root cause (inputs from static attrs set "too early") in #34992 |
Jumping in as well. This is super weird and going to break a lot of things out there It means that behaviour wise <div myDirectiveInput="abcd"></div> IS NOT EQUAL TO: <div [myDirectiveInput]="'abcd'"></div> A good example is the CDK's coercion utils, for example the boolean coercion. It accepts Now, anywhere someone implemented a reaction to this change via a property descriptor (setter) it will probably break behaviour cause things he expects to be there are probably not. It's breaking a lot of existing logic while behaving differently for the same input (lifecycle wise). I think this must be changed in 9.0.2 and not 10, cause up till 10 it will just get worse with people adopting it. |
Here's another, rather complex, scenario which i'm required to handle things in the setter. I have a grid library which is a rather complex component. The grid is designed to show template it get's from the outside (for rendering cells, rows, etc...). In other words, the grid might render templates from view's that branched out of the grid's view, i.e. their change detectors also branched out. It might get more complex and nest internally... Now, when the grid want to invoke a change detection cycle it will not trigger the change detection in the template, unless explicitly invoking that on the embedded view created, so far not a big issue. The problem is when the created template/component is changed, usually manual code (updating properties on the component ref instance). Of course, each component can expose its change detector ref so any update from grid can also trigger the CD, but that's just not right as the CDRef of each component should be private. In general, most simple scenarios does not notice the implications of this issue but when imperative component creation is used, especially one that cross change detection boundaries, this is a big deal. |
Confirming that this is a breaking change in ivy: the timing of static inputs assignment changed and now it is happening much earlier (as compared to VE). This creates all sorts of problems in practice, but the one that comes up quite often is the assumption of static queries presence when an input setter is invoked: https://stackblitz.com/edit/angular-czurvn?file=src/app/app.module.ts I think that the worst here is lack of predictability - a given setter will "work" or not depending on how input is provided (static vs. dynamic). I think that we should be more predictable here and set static inputs on the first update pass. Marking it as regression. It is not a 5-minutes fix but we should be able to restore compatibility with VE. For now there is a relatively easy work-around - move to the logic to the ngOnChanges. |
@pkozlowski-opensource Any update on this? It's a real deal for us, we have a large project and we use get/set descriptors a lot. The main problem for us is the lack of predictability, sometimes it works some times it does not. While moving to The other quick fix we have is to ignore all static bindings to angular inputs. We will force binding and loose the extra perf of one-time binding of static values.
Can this be in the next v10.x minor/patch? I understand you might have greater priorities but you stated it should be an easy fix and frankly this un-deterministic behavior is (IMHO) a major issue for a framework. Thanks! |
I don't understand why this has Disabling Ivy seems to be our only non-hacky option here, and to be clear, this is now the third or fourth time where disabling the main feature of Angular 9 had to come up as a possible resolution. |
Sad news that this is just a medium priority issue |
Frankly I think this issue is not prioritised due to lack of visibility. The angular team has made a promise to keep its predictable and deterministic. This is a real surprise to me. I can only assume they don't code this way at google. |
I believe if inputs for directives are set during change detection I would not have to implement it this way: |
any news? |
Ivy changed the timing of inputs with static values, it is true. But we've been living with this changed behaviour for almost 4 years now and the last comment on this issue is ~2 years old. Based on this it is fair to assume that most of the ecosystem adjusted to this change. More importantly - we don't want directive authors to make assumptions about the precise timing on the inputs settings. The only "safe" way of reading / responding to input changes is |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
🐞 bug report
Affected Package
Is this a regression?
No.
Description
In the ivy compatibility docs you can find this important sentence about a change coming with v9/ivy:
We ran into a lot of issues with this, e.g. when you use a
ViewChild
query in an input setter.I created a really simple example showing the problem and that the same code works for v8 but not in v9 (couldn't reproduce it in stackblitz)
https://github.com/Phil147/unbound-input-problem-v8
https://github.com/Phil147/unbound-input-problem-v9
The example is very simplified and not best practice but imagine you want to call a function of another custom component you query here.
In the v9 repo you will get an error once you start the app with
ng serve
because the ViewChild query is not yet resolved and thus undefined.With v8 this works fine because the inputs are set during first change detection and the ViewChild query was resolved (with static: true). With v9 this code breaks but only in the particular situation when the input is not used with a binding but with a static value. We only found this issue by accident because in our unit tests we typically used bindings, but one specific example in our documentation (of our component library) failed during runtime.
This is a very confusing behavior now that the timing changes depending on how the inputs are used in templates. For me it would make more sense to keep this consistent.
From what I found this change was made to get better performance because static inputs don't change and you can leave them out during change detection, which makes total sense.
I wonder if it might be possible to still use the information you gather about static attributes but run them together with all other inputs during the first change detection run and then ignore them instead of moving their calls before the change detection happens.
🔬 Minimal Reproduction
works fine:
https://github.com/Phil147/unbound-input-problem-v8
same code breaks in v9:
https://github.com/Phil147/unbound-input-problem-v9
🌍 Your Environment
Angular Version:
The text was updated successfully, but these errors were encountered: