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

--watch not running with last file content when file changes multiple times #48909

Closed
GabbeV opened this issue Jul 24, 2023 · 3 comments
Closed
Labels
watch-mode Issues and PRs related to watch mode

Comments

@GabbeV
Copy link

GabbeV commented Jul 24, 2023

Version

v20.5.0

Platform

No response

Subsystem

No response

What steps will reproduce the bug?

  1. create an empty file called watchthis.js
  2. create another file called runthis.js with the following content:
const { writeFileSync } = require("fs")

writeFileSync("./watchthis.js", "console.log('foo')")
setTimeout(() => {
	writeFileSync("./watchthis.js", "console.log('bar')")
}, 100)
  1. run node --watch ./watchthis.js in a terminal
  2. run node runthis.js in another terminal

How often does it reproduce? Is there a required condition?

Every time

What is the expected behavior? Why is that the expected behavior?

The last printed value in the first terminal should be "bar".

What do you see instead?

The last printed value is "foo".

Additional information

No response

@MoLow MoLow added the watch-mode Issues and PRs related to watch mode label Jul 25, 2023
@MoLow
Copy link
Member

MoLow commented Jul 25, 2023

watch mode is throttled for a change once in every 500 ms,
see

constructor({ throttle = 500, mode = 'filter', signal } = kEmptyObject) {

that number was chosen arbitrarily just as a heuristic on how often ides/users save files, and obviously, watch mode is designed to work with ides and not scripts like the above.
that being said - if you believe we should decrease this value a PR is welcomed

@MoLow MoLow closed this as completed Jul 25, 2023
@GabbeV
Copy link
Author

GabbeV commented Jul 25, 2023

I believe people expect file watchers to act in a eventually consistent way where if files stop changing it eventually runs and produces an output based on the final state of the filesystem.

I tried to not prescribe a solution in my original ticket as I would personally be fine with any debounce/throttle logic that achieves this.

Some alternatives that would fulfill this would be:

  1. Only run at the end of the timeout.
  2. Same as 1 but extend the timeout every time a change is detected.
  3. Run immediately and if new changes are detected run again at the end of the timeout.
  4. Same as 3 but extend the timeout every time a change is detected.

I encountered this issue when i watched a file produced by a build tool. The build tool touched the file twice. First doing basically nothing to it and then updating it. It was very surprising to me that the the code that ran last didn't match the code i could see on disk.

@MoLow
Copy link
Member

MoLow commented Jul 25, 2023

reopening - maybe we should change from throttle to debounce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
watch-mode Issues and PRs related to watch mode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants