Jelajahi Sumber

fix(cli): prevent infinite loop in dev file watcher

Lucas Nogueira 3 tahun lalu
induk
melakukan
47be3514a7
3 mengubah file dengan 24 tambahan dan 9 penghapusan
  1. 2 1
      tooling/cli/Cargo.toml
  2. 18 8
      tooling/cli/src/dev.rs
  3. 4 0
      tooling/cli/tauri-dev-watcher.gitignore

+ 2 - 1
tooling/cli/Cargo.toml

@@ -19,7 +19,8 @@ include = [
   "scripts/",
   "*.json",
   "*.rs",
-  "tauri.gitignore"
+  "tauri.gitignore",
+  "tauri-dev-watcher.gitignore"
 ]
 
 [[bin]]

+ 18 - 8
tooling/cli/src/dev.rs

@@ -39,6 +39,8 @@ static KILL_BEFORE_DEV_FLAG: OnceCell<AtomicBool> = OnceCell::new();
 #[cfg(unix)]
 const KILL_CHILDREN_SCRIPT: &[u8] = include_bytes!("../scripts/kill-children.sh");
 
+const TAURI_DEV_WATCHER_GITIGNORE: &[u8] = include_bytes!("../tauri-dev-watcher.gitignore");
+
 #[derive(Debug, Parser)]
 #[clap(about = "Tauri dev", trailing_var_arg(true))]
 pub struct Options {
@@ -280,16 +282,24 @@ fn command_internal(options: Options) -> Result<()> {
   }
 }
 
-fn lookup<F: FnMut(&OsStr, FileType, PathBuf)>(dir: &Path, mut f: F) {
+fn lookup<F: FnMut(FileType, PathBuf)>(dir: &Path, mut f: F) {
+  let mut default_gitignore = std::env::temp_dir();
+  default_gitignore.push(".tauri-dev");
+  let _ = std::fs::create_dir_all(&default_gitignore);
+  default_gitignore.push(".gitignore");
+  if !default_gitignore.exists() {
+    if let Ok(mut file) = std::fs::File::create(default_gitignore.clone()) {
+      use std::io::Write;
+      let _ = file.write_all(TAURI_DEV_WATCHER_GITIGNORE);
+    }
+  }
+
   let mut builder = ignore::WalkBuilder::new(dir);
+  let _ = builder.add_ignore(default_gitignore);
   builder.require_git(false).ignore(false).max_depth(Some(1));
 
   for entry in builder.build().flatten() {
-    f(
-      entry.file_name(),
-      entry.file_type().unwrap(),
-      dir.join(entry.path()),
-    );
+    f(entry.file_type().unwrap(), dir.join(entry.path()));
   }
 }
 
@@ -309,8 +319,8 @@ fn watch(
   let (tx, rx) = channel();
 
   let mut watcher = watcher(tx, Duration::from_secs(1)).unwrap();
-  lookup(&tauri_path, |file_name, file_type, path| {
-    if file_name != "target" && file_name != "Cargo.lock" {
+  lookup(&tauri_path, |file_type, path| {
+    if path != tauri_path {
       let _ = watcher.watch(
         path,
         if file_type.is_dir() {

+ 4 - 0
tooling/cli/tauri-dev-watcher.gitignore

@@ -0,0 +1,4 @@
+node_modules/
+target/
+WixTools
+Cargo.lock