Notify v5 just... Does nothing

Hey! I'm having a pretty annoying non-rust-like error with the new version of notify-rs, I have a CLI app that watches some files, it was created and tested with v4. Some days ago I found out that notify has released a new major, so I updated, reviewed the code, applying the changes but now I just have a piece of code that does not throws any error, neither Clippy nor rust-analyzer points out anything strange.

Does anyone knows about that, or is the best way for rust to watch for file changes?

 fn main() {
    let (tx, rx) = std::sync::mpsc::channel();

    let mut watcher = RecommendedWatcher::new(tx, Config::default()).unwrap();
    watcher.watch(path.as_ref(), RecursiveMode::Recursive).unwrap();

    for res in rx {
        match res {
            Ok(event) => println!("changed: {:?}", event),
            Err(e) => println!("watch error: {:?}", e),
        }
    }
}

Does the upgrading guide contain something useful for you?

1 Like

Yeap, I updated the code based on the example present on this page, and the question snippet its the result of the new version.

I tried your code (modified a little to make it compile)

use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};

fn main() {
    let path = "Cargo.toml";

    let (tx, rx) = std::sync::mpsc::channel();

    let mut watcher = RecommendedWatcher::new(tx, Config::default()).unwrap();
    watcher
        .watch(path.as_ref(), RecursiveMode::Recursive)
        .unwrap();

    for res in rx {
        match res {
            Ok(event) => println!("changed: {:?}", event),
            Err(e) => println!("watch error: {:?}", e),
        }
    }
}

and tried changing Cargo.toml and got

changed: Event { kind: Modify(Data(Any)), paths: ["/drive/ssd_ext4/Dev/sandbox/sandbox_rs/Cargo.toml"], attr:tracker: None, attr:flag: None, attr:info: None, attr:source: None }
changed: Event { kind: Modify(Data(Any)), paths: ["/drive/ssd_ext4/Dev/sandbox/sandbox_rs/Cargo.toml"], attr:tracker: None, attr:flag: None, attr:info: None, attr:source: None }
changed: Event { kind: Access(Close(Write)), paths: ["/drive/ssd_ext4/Dev/sandbox/sandbox_rs/Cargo.toml"], attr:tracker: None, attr:flag: None, attr:info: None, attr:source: None }

I guess in your case you're not getting anything? I also tried it with a directory and editing/adding/removing files inside it and it seemed to work as expected. I'm on linux (openSUSE),

1 Like

Yeap, that's a solid boilerplate, I figured out that in my case the thread was busy so, the watcher was created successfully but does not start watching, that's not a runtime error nor syntax, so none of them was giving a message, just simplifying threading stuff solved, but that's for the answer, will help more people overtime! ;D

1 Like