From 12354535c571503bf7e72d299fe772f5fddc0f2b Mon Sep 17 00:00:00 2001 From: zp365238 Date: Fri, 5 Apr 2024 21:25:17 +0800 Subject: [PATCH 1/2] fix: fix "too many files open" error when watching with-antd example --- crates/mako/src/watch.rs | 43 +++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/crates/mako/src/watch.rs b/crates/mako/src/watch.rs index b98412174..1684fe236 100644 --- a/crates/mako/src/watch.rs +++ b/crates/mako/src/watch.rs @@ -39,11 +39,9 @@ impl<'a> Watcher<'a> { pub fn watch(&mut self) -> anyhow::Result<()> { let t_watch = Instant::now(); - let ignore_list = [".git", "node_modules", ".DS_Store", ".node"]; + let ignore_list = self.get_ignore_list(); - let mut root_ignore_list = ignore_list.to_vec(); - root_ignore_list.push(self.compiler.context.config.output.path.to_str().unwrap()); - self.watch_dir_recursive(self.root.into(), &root_ignore_list)?; + self.watch_dir_recursive(self.root.into(), &ignore_list)?; let module_graph = self.compiler.context.module_graph.read().unwrap(); let mut dirs = HashSet::new(); @@ -64,7 +62,7 @@ impl<'a> Watcher<'a> { } }); dirs.iter().try_for_each(|dir| { - self.watch_dir_recursive(dir.into(), ignore_list.as_slice())?; + self.watch_dir_recursive(dir.into(), &ignore_list)?; Ok(()) })?; @@ -99,7 +97,32 @@ impl<'a> Watcher<'a> { Ok(()) } - fn watch_dir_recursive(&mut self, path: PathBuf, ignore_list: &[&str]) -> anyhow::Result<()> { + fn get_ignore_list(&mut self) -> Vec { + let ignore_list = [ + ".git", + "node_modules", + ".DS_Store", + ".node", + self.compiler.context.config.output.path.to_str().unwrap(), + ]; + + let mut dirs = vec![]; + self.root.ancestors().for_each(|path| { + ignore_list.iter().for_each(|ignore| { + let mut path = PathBuf::from(path); + path.push(ignore); + dirs.push(path); + }) + }); + + dirs + } + + fn watch_dir_recursive( + &mut self, + path: PathBuf, + ignore_list: &[PathBuf], + ) -> anyhow::Result<()> { let items = std::fs::read_dir(path)?; items .into_iter() @@ -111,7 +134,7 @@ impl<'a> Watcher<'a> { Ok(()) } - fn watch_file_or_dir(&mut self, path: PathBuf, ignore_list: &[&str]) -> anyhow::Result<()> { + fn watch_file_or_dir(&mut self, path: PathBuf, ignore_list: &[PathBuf]) -> anyhow::Result<()> { if Self::should_ignore_watch(&path, ignore_list) { return Ok(()); } @@ -131,9 +154,11 @@ impl<'a> Watcher<'a> { Ok(()) } - fn should_ignore_watch(path: &Path, ignore_list: &[&str]) -> bool { + fn should_ignore_watch(path: &Path, ignore_list: &[PathBuf]) -> bool { let path = path.to_string_lossy(); - ignore_list.iter().any(|ignored| path.ends_with(ignored)) + ignore_list + .iter() + .any(|ignored| path.strip_prefix(ignored.to_str().unwrap()).is_some()) } fn should_ignore_event(path: &Path, kind: &EventKind) -> bool { From df5f2f20deb7ecdd48651568e6c06426fd96942c Mon Sep 17 00:00:00 2001 From: zp365238 Date: Sun, 5 May 2024 19:27:13 +0800 Subject: [PATCH 2/2] fix: include dist dictionary of root's parent dictionary when watching --- crates/mako/src/watch.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/crates/mako/src/watch.rs b/crates/mako/src/watch.rs index 1684fe236..5627f8cb4 100644 --- a/crates/mako/src/watch.rs +++ b/crates/mako/src/watch.rs @@ -39,9 +39,7 @@ impl<'a> Watcher<'a> { pub fn watch(&mut self) -> anyhow::Result<()> { let t_watch = Instant::now(); - let ignore_list = self.get_ignore_list(); - - self.watch_dir_recursive(self.root.into(), &ignore_list)?; + self.watch_dir_recursive(self.root.into(), &self.get_ignore_list(true))?; let module_graph = self.compiler.context.module_graph.read().unwrap(); let mut dirs = HashSet::new(); @@ -62,7 +60,7 @@ impl<'a> Watcher<'a> { } }); dirs.iter().try_for_each(|dir| { - self.watch_dir_recursive(dir.into(), &ignore_list)?; + self.watch_dir_recursive(dir.into(), &self.get_ignore_list(false))?; Ok(()) })?; @@ -97,15 +95,14 @@ impl<'a> Watcher<'a> { Ok(()) } - fn get_ignore_list(&mut self) -> Vec { - let ignore_list = [ - ".git", - "node_modules", - ".DS_Store", - ".node", - self.compiler.context.config.output.path.to_str().unwrap(), - ]; + fn get_ignore_list(&self, with_output_dir: bool) -> Vec { + let mut ignore_list = vec![".git", "node_modules", ".DS_Store", ".node"]; + if with_output_dir { + ignore_list.push(self.compiler.context.config.output.path.to_str().unwrap()); + } + // node_modules of root dictionary and root dictionary's parent dictionaries should be ignored + // for resolving the issue of "too many files open" in monorepo let mut dirs = vec![]; self.root.ancestors().for_each(|path| { ignore_list.iter().for_each(|ignore| {