Skip to content
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

refactor: update schematics to work with new UpdateBuffer #26504

Merged
merged 2 commits into from
Jan 26, 2023

Conversation

alan-agius4
Copy link
Contributor

This change is needed for the upcoming changes in Angular CLI version 16 where the default UpdateBuffer is replaced with the UpdateBuffer2.

This commits changes how certain updates are applied to avoid overlapping changes which would otherwise result in broken output due to the offset would be out of sync.

This change is needed for the upcoming changes in Angular CLI version 16 where the default `UpdateBuffer` is replaced with the `UpdateBuffer2`.

This commits changes how certain updates are applied to avoid overlapping changes which would otherwise result in broken output due to the offset would be out of sync.
This commit adds a new step in CI to tests schematics using the `UpdateBuffer2` which will be the default and only `UpdateBuffer` in Angular CLI  vertsion 16.
@alan-agius4 alan-agius4 added target: patch This PR is targeted for the next patch release area: cdk/schematics cdk/schematics is *not* a public API area: ng-generate Schematics that generate code in user projects labels Jan 26, 2023
@angular-robot angular-robot bot added the area: build & ci Related the build and CI infrastructure of the project label Jan 26, 2023
@devversion
Copy link
Member

Is there a little more context on why this previously worked? The old update recorder always worked even if updates are applied not necessarily in reverse order. Just asking for better understanding.

@alan-agius4
Copy link
Contributor Author

alan-agius4 commented Jan 26, 2023

It works because of a bug which was leveraged in this case. We now switched to magic-string to better support multiple adjacent operations which previously were buggy.

For context here is the bug that was levraged

const buffer = Buffer.from('Hello beautiful World');
//Old 
const recorder = new UpdateBuffer(buffer);
recorder.remove(0, 5);
recorder.insertRight(0, Buffer.from('Hola amazing'));
recorder.remove(0, 5);
recorder.insertRight(0, Buffer.from(' and fantastic'));
recorder.toString() // Hola amazing and fantastic beautiful World

// New
const recorder2 = new UpdateBuffer2(buffer);
recorder2.remove(0, 5);
recorder2.insertRight(0, Buffer.from('Hola amazing'));
recorder2.remove(0, 5);
recorder2.insertRight(0, Buffer.from(' and fantastic'));
recorder2.toString() //  and fantastic beautiful World

@alan-agius4
Copy link
Contributor Author

Note: this reverse "trick" is not something new as it's used in other places

@devversion
Copy link
Member

Yeah, we did it a couple of times, but also quite a few migrations (especially older ones) did not follow that pattern because the update recorder was assumed to intentionally handle this out of the box, only ever considering the original source indices.

I will need to experiment a bit to get a better understanding

@alan-agius4
Copy link
Contributor Author

alan-agius4 commented Jan 26, 2023

There is definitely something weird with this migration though as there was an attempt to correct the node positions before as well which is odd.

https://github.com/angular/components/pull/26504/files#diff-d1860aa442910133e59b78c36e7947c6a0921723c3cbd77a78223a4aa6ab8e1bR432

@devversion
Copy link
Member

Yeah, those are not ideal. I'm good with this change anyway. I like the fact that magic-string is used under the hood. I just want to make sure we document the semantic changes well when Angular CLI releases. Arguably, the behavior you outlined in #26504 (comment) seemed expected to me and also reasonable to me. i.e. remove always operates on the original string indices. This makes a lot of sense.

Going forward the way remove works is definitely a breaking change, and personally I find it confusing given that the documentation of magic-string claims to only operate on the original string.

@alan-agius4 alan-agius4 added the action: merge The PR is ready for merge by the caretaker label Jan 26, 2023
@alan-agius4
Copy link
Contributor Author

Yeah that is indeed a confusing behaviour especially considering that docs state otherwise.

@angular-robot angular-robot bot merged commit c4414ad into angular:main Jan 26, 2023
@alan-agius4 alan-agius4 deleted the schematics-update branch January 26, 2023 12:57
angular-robot bot pushed a commit that referenced this pull request Jan 26, 2023
* refactor: update schematics to work with new UpdateBuffer

This change is needed for the upcoming changes in Angular CLI version 16 where the default `UpdateBuffer` is replaced with the `UpdateBuffer2`.

This commits changes how certain updates are applied to avoid overlapping changes which would otherwise result in broken output due to the offset would be out of sync.

* ci: add test step to test schematics with `UpdateBuffer2`

This commit adds a new step in CI to tests schematics using the `UpdateBuffer2` which will be the default and only `UpdateBuffer` in Angular CLI  vertsion 16.

(cherry picked from commit c4414ad)
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Feb 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: build & ci Related the build and CI infrastructure of the project area: cdk/schematics cdk/schematics is *not* a public API area: ng-generate Schematics that generate code in user projects target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants