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

feat(unstable): Support --watch flag for bundle and fmt subcommands #8276

Merged
merged 46 commits into from
Nov 22, 2020

Conversation

magurotuna
Copy link
Member

@magurotuna magurotuna commented Nov 7, 2020

This PR adds --watch flag support for both bundle and fmt subcommands.
In addition to this, it refactors run --watch command so that module resolution will occur every time the file watcher detects file addition/deletion, which allows the watcher to observe a file that is newly added to the dependency as well.


Fixes #8181

Currently, --watch flag is working with run only, but it is considered the flag is going to be added to other subcommands like fmt, lint, and so on. (see #7465)
For this purpose, the current implementation of watch_func function has abstract, extendable design - all we have to do to add the watcher feature to subcommands is passing both target file paths and an operation you want to execute to the watch_func function.

Considering it deeply, it seems like this watch_func design is not suitable for usecases, especially run subcommand. In case we want to detect file changes and then restart it, the watcher should also keep an eye on deletion and addition of files or dependency.
So target file paths will vary, although the current watch_func receives constant file paths. This is why the issue described in #8181 happened.

To address the issue, I think we should resolve dependency whenever the watcher detects changes. Actually this is relatively heavy task to be done every time, but it seems needed.

In this PR, I've added new watcher funtcion named watch_func_for_run which is dedicated to run subcommand.
Additionally I've removed the original watch_func function. I know this removal will have an effect on adding --watch flag to other subcommands, which are being worked on at #7740 and #7767. I think other subcommands, like fmt, also have the same issue as run. That said, if we would run deno fmt --watch, the watcher would have to detect any addition or deletion of files. Therefore we should probably consider again what watch_func function interface should look like, without using the current one.

TODO

  • add test

@magurotuna magurotuna changed the title fix(cli/watcher): detect changes happening on files newly added to dependency fix(cli/watcher): detect changes on files newly added to dependency Nov 7, 2020
@magurotuna magurotuna marked this pull request as ready for review November 7, 2020 07:48
@magurotuna
Copy link
Member Author

I also added fmt --watch feature to show how we can make subcommands other than run work with --watch flag. (a lot of code come from #7767)

@magurotuna
Copy link
Member Author

Just added bundle --watch as well. Bundle is similar to run rather than fmt, so I renamed watch_func_for_run to watch_func_with_module_resolution and fixed its parameter type a little.

@bartlomieju
Copy link
Member

watch_func_with_module_resolution - how about watch_func_with_module_graph?

@magurotuna
Copy link
Member Author

magurotuna commented Nov 11, 2020

@bartlomieju
bundle operation requires module_graph::Graph, while run requires deno_core::ModuleSpecifier. This is why I avoided using the word graphas function name.
But indeed watch_func_with_module_resolution is long and kind of redundant, so I'd be happy to find appropriate name...

cli/file_watcher.rs Outdated Show resolved Hide resolved
@magurotuna
Copy link
Member Author

I added tests for run and bundle in the following cases:

  • watcher exits immediately if module resolution fails at the first attempt due to invalid syntax
  • watcher tries to continue watching files using previously resolved data

@magurotuna
Copy link
Member Author

hmmm..... tests are sooooo flaky :(

@bartlomieju
Copy link
Member

hmmm..... tests are sooooo flaky :(

@magurotuna are you talking about tests for this PR or tests in general?

@magurotuna
Copy link
Member Author

@bartlomieju
Seems like both of them.

  1. https://github.com/denoland/deno/runs/1413182650#step:16:3855 -> operation timeout on macOS, which happened at the place unrelated to this PR
  2. https://github.com/denoland/deno/runs/1416308269#step:14:12158 -> rusty_v8 build failed on ubuntu, totally unrelated to this PR
  3. https://github.com/denoland/deno/runs/1416415130#step:16:3862 -> fmt_watch_test failed on macOS. This seems to be related to this PR and "run_watch" test is flaky #8316.

I have no idea why fmt_watch_test failed. I tried to run the tests on my local mac, they were totally fine.

@bartlomieju
Copy link
Member

@magurotuna thanks for detailed description. I'm aware of the first problem, but haven't found the reason yet. Second failure seems like an intermittent failure. As for third I'm not sure, let's keep a close eye on it.

@magurotuna
Copy link
Member Author

Several times of resolving conflict have been a little bit grunt work 😢

@bartlomieju
Copy link
Member

Several times of resolving conflict have been a little bit grunt work 😢

Sorry for that @magurotuna, I'll try to land your PR this weekend.

Can I ask you to take a look at this comment and check if that's still the case?

@magurotuna
Copy link
Member Author

@bartlomieju Sure, I'll check for that.

@bartlomieju bartlomieju changed the title fix(cli/watcher): detect changes on files newly added to dependency feat(unstable): Support --watch flag for bundle and fmt subcommands Nov 21, 2020
@bartlomieju
Copy link
Member

@magurotuna I've tested this PR locally and it's working very well! Code in this PR is a big improvement compared to current implementation and LGTM. Please prepare commit message before landing :)

@magurotuna
Copy link
Member Author

@bartlomieju
Thanks for your test!

Please prepare commit message before landing :)

Sorry if I misunderstand it, but does it mean that you want me to write a commit message that will be used in a squashed merge commit?

@bartlomieju
Copy link
Member

Sorry if I misunderstand it, but does it mean that you want me to write a commit message that will be used in a squashed merge commit?

Yes, exactly

@magurotuna
Copy link
Member Author

@bartlomieju
Sure, I've added a brief message to the head of the PR's body. Is this fine?

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bartlomieju bartlomieju merged commit e3f73d3 into denoland:master Nov 22, 2020
@magurotuna magurotuna deleted the watcher-module-resolve branch November 23, 2020 03:21
jannes pushed a commit to jannes/deno that referenced this pull request Dec 1, 2020
…enoland#8276)

This commit adds support for "--watch" flag for "bundle" 
and "fmt" subcommands.

In addition to this, it refactors "run --watch" command so that
module resolution will occur every time the file watcher detects 
file addition/deletion, which allows the watcher to observe a file 
that is newly added to the dependency as well.
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.

bug(watcher): changes on a file newly added to dependency are not watched
3 participants