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

Fix worker call IDs with multiple worker instances #3481

Merged
merged 2 commits into from
Aug 14, 2024

Conversation

mogzol
Copy link
Contributor

@mogzol mogzol commented Jul 27, 2024

Fixes #3423, #3040

Previously, currentCallMethodId was a field on each instance of PrettierWorkerInstance, however, there is only a single instance of worker. So when multiple instances of PrettierWorkerInstance sent messages to the worker, they would each use their own currentCallMethodId, resulting in multiple messages with the same ID. This meant that the instances could not reliably get results back from the worker, as there were multiple results with the same ID. This caused many issues, such as overwriting files with the contents of other files while formatting.

Additionally, the "import" messages to the worker did not use IDs at all, so a PrettierWorkerInstance could end up setting the wrong version number.

This PR changes the call IDs to use a variable outside PrettierWorkerInstance, so that all instances will use the same value and cannot have overlapping IDs. It also ensures that all messages to and from the worker include an ID, so that the PrettierWorkerInstances can properly determine who a message is for.

  • Run tests
  • Update the CHANGELOG.md with a summary of your changes

Previously, 'currentCallMethodId' was a field on each instance of
PrettierWorkerInstance, however, there was only a single instance of 'worker'.
So when there were multiple instances of PrettierWorkerInstance, they would
each have their own 'currentCallMethodId' which could overlap. This meant that
the instances could not reliably get results back from the worker, as there
were multiple results with the same ID. This results in many issues, such as
overwriting files with the contents of other files while formatting.

This change moves 'currentCallMethodId' outside PrettierWorkerInstance, so
that all instances will use the same value and cannot have overlapping IDs.
@mogzol mogzol force-pushed the fix-worker-call-id branch from 0a539b7 to ccc9e6a Compare July 27, 2024 03:00
The same issue that existed for the callMethod messages also existed for the
import messages, where multiple PrettierWorkerInstances could get messages
meant for a different instance, so the 'version' field could end up with the
wrong value.

This changes the code to include an ID in all messages sent to the worker, so
that the PrettierWorkerInstances can properly determine whether a message is
meant for them or not.
@mogzol
Copy link
Contributor Author

mogzol commented Jul 27, 2024

Noticed that the same issue existed for the "import" messages (which didn't use IDs at all), resulting in PrettierWorkerInstances having incorrect version numbers. I just pushed a change to fix that issue as well.

@mogzol mogzol closed this Aug 6, 2024
@mogzol
Copy link
Contributor Author

mogzol commented Aug 6, 2024

I did not mean to close this, oops, reopening.

@ntotten Any chance you could review this? I think it is a pretty serious issue for people working in monorepos with multiple packages that use prettier.
It has caused me to lose my un-committed changes on files several times and is incredibly frustrating whenever it happens. I've had to get into the habit of disabling the prettier extension whenever I want to do a find and replace.

@mogzol mogzol reopened this Aug 6, 2024
@ntotten ntotten merged commit ba90037 into prettier:main Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Running find & replace on files with formatOnSave enabled overwrites files with incorrect content
2 participants