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

【with tokio】After executing git checkout . command and then modifying the file, unable to get response to file changes #541

Open
jiesia opened this issue Oct 16, 2023 · 0 comments

Comments

@jiesia
Copy link

jiesia commented Oct 16, 2023

System details

  • OS/Platform name and version:
    MacOS Sonoma 14.0 with Apple M1 chip
  • Rust version (if building from source): rustc --version:
    rustc 1.73.0-nightly (0e8e857b1 2023-07-16)
  • Notify version (or commit hash if building from git):
    { version = "6.0.1", default-features = false, features = ["macos_kqueue"] }

What you did (as detailed as you can)

I am using notify and tokio to monitor file changes. During the process, I found a bug: after I executed git checkout . command in the monitored directory, notify could no longer respond to file changes.

Below is all my code

use std::fs;

use notify::{
    event::{CreateKind, DataChange, ModifyKind, RemoveKind, RenameMode},
    EventKind, RecommendedWatcher, RecursiveMode, Watcher,
};
use tokio::sync::mpsc::channel;

#[tokio::main]
async fn main() {
    let _ = tokio::spawn(async move { watch().await }).await;
}

async fn watch() {
    let (tx, mut rx) = channel(2);
    let mut watcher = RecommendedWatcher::new(
        move |res| {
            tx.blocking_send(res).unwrap();
        },
        Default::default(),
    )
    .unwrap();

    fs::read_dir("example").unwrap().for_each(|path| {
        let path = path.unwrap().path();
        if path.is_file() {
            watcher
                .watch(path.as_path(), RecursiveMode::NonRecursive)
                .unwrap();
        } else {
            let path_str = path.to_string_lossy();
            if path_str.contains(".git") {
                return;
            }
            watcher
                .watch(path.as_path(), RecursiveMode::Recursive)
                .unwrap();
        }
    });

    while let Some(res) = rx.recv().await {
        match res {
            Ok(event) => match event.kind {
                EventKind::Any => {}
                EventKind::Access(_) => {}
                EventKind::Create(CreateKind::File) => {
                    println!("\nwatch create file");
                }
                EventKind::Modify(ModifyKind::Data(DataChange::Any)) => {
                    println!("\nwatch modify data");
                }
                EventKind::Modify(ModifyKind::Name(RenameMode::Any)) => {
                    println!("\nwatch modify name");
                }
                EventKind::Remove(RemoveKind::Any) => {
                    println!("\nwatch remove any");
                }
                EventKind::Remove(_) => {
                    println!("\nwatch remove other");
                }
                _ => {}
            },
            Err(e) => {
                println!("watch error: {:?}", e);
            }
        }
    }
}

What you expected

I expect that after executing git checkout . command and then modifying the file, I can still monitor the file changes.

What happened

After I executed git checkout . command in the monitored directory, notify could no longer respond to file changes.

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

No branches or pull requests

1 participant