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

Use fs.watch instead of chokidar if Node.js >=v19.1 #12495

Closed
7 tasks done
gajus opened this issue Mar 20, 2023 · 8 comments
Closed
7 tasks done

Use fs.watch instead of chokidar if Node.js >=v19.1 #12495

gajus opened this issue Mar 20, 2023 · 8 comments

Comments

@gajus
Copy link
Contributor

gajus commented Mar 20, 2023

Describe the bug

File changes are not being detected when multiple instances of chokidar are running in the directory.

When using Turbowatch (which used to default to chokidar) and Vite together, Vite's HMR would just stop working.

Change watching method to polling fixes the issue, but mixes CPU go brrrrrrrr.

As far as I can tell, it is due to this issue paulmillr/chokidar#1240

For what it is worth, Turbowatch was also updated to use the suggested logic.

gajus/turbowatch#22

Theoretically Vite could just use Turbowatch to implement watching.

Reproduction

N/A

Steps to reproduce

N/A

System Info

N/A

Used Package Manager

npm

Logs

No response

Validations

@silverwind
Copy link

silverwind commented Mar 20, 2023

How is fs.watch these days? Chokidar was specifically created because of the shortcomings mentioned in its README, have these been resolved?

Generally I'm for trying it because it removes the pain that is fsevents.

@gajus
Copy link
Contributor Author

gajus commented Mar 20, 2023

Doesn't report filenames on MacOS.

No longer a problem.

Doesn't report events at all when using editors like Sublime on MacOS.

Cannot replicate.

Often reports events twice.

True.

Emits most changes as rename.

True.

Does not provide an easy way to recursively watch file trees.

No longer true, as of Node.js v19.1

Does not support recursive watching on Linux.

No longer true, as of Node.js v19.1

@silverwind
Copy link

silverwind commented Mar 20, 2023

Often reports events twice.
True.

Can and should be worked around via debounce or better, a shared interval that batches events together.

Emits most changes as rename.
True.

Probably does not matter in context of vite's usage.

@gajus
Copy link
Contributor Author

gajus commented Mar 20, 2023

Indeed.

@gajus
Copy link
Contributor Author

gajus commented Mar 21, 2023

@silverwind This might be useful vercel/turbo#986 (comment)

@snnsnn
Copy link

snnsnn commented Apr 18, 2023

Often reports events twice.

This is a common misconception among Node users. NodeJS does not fire multiple events for a single change, it is the editor updates the file multiple times. Checkout the link for details.

If fs watch is not enough, best alternative would be parcel watcher which powers vscode:

@gajus
Copy link
Contributor Author

gajus commented Apr 18, 2023

Parcel watcher implements the same backends that Turbowatch does:

@parcel/watcher has the following watcher backends, listed in priority order:

@bluwy
Copy link
Member

bluwy commented Jun 22, 2023

Closing this to continue the conversation at #13593. We have also discussed using fs.watch, but it is a bit low level that we would have to eventually re-implement a chokidar-like interface to be able to nicely use it. So if we're switching away from chokidar, it would likely be @parcel/watcher

@bluwy bluwy closed this as completed Jun 22, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jul 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants