Crates.io page API Docs Crate license: Apache 2.0 MSRV: 1.56.0 (breaking) CI status

Watchexec library

The library which powers Watchexec CLI and other tools.

Quick start

```rust use miette::{IntoDiagnostic, Result}; use watchexec::{ Watchexec, action::{Action, Outcome}, config::{InitConfig, RuntimeConfig}, handler::{Handler as _, PrintDebug}, };

[tokio::main]

async fn main() -> Result<()> { let mut init = InitConfig::default(); init.on_error(PrintDebug(std::io::stderr()));

let mut runtime = RuntimeConfig::default();
runtime.pathset(["watchexec.conf"]);

let conf = YourConfigFormat::load_from_file("watchexec.conf").await?;
conf.apply(&mut runtime);

let we = Watchexec::new(init, runtime.clone())?;
let w = we.clone();

let c = runtime.clone();
runtime.on_action(move |action: Action| {
    let mut c = c.clone();
    let w = w.clone();
    async move {
        for event in &action.events {
            if event.paths().any(|(p, _)| p.ends_with("/watchexec.conf")) {
                let conf = YourConfigFormat::load_from_file("watchexec.conf").await?;

                conf.apply(&mut c);
                w.reconfigure(c.clone());
                // tada! self-reconfiguring watchexec on config file change!

                break;
            }
        }

        action.outcome(Outcome::if_running(
            Outcome::DoNothing,
            Outcome::both(Outcome::Clear, Outcome::Start),
        ));

        Ok(())
    }
});

we.main().await.into_diagnostic()?;
Ok(())

} ```

Kitchen sink

The library also exposes a large amount of components which are available to make your own tool, or to make anything else you may want:

There are also separate, standalone crates used to build Watchexec which you can tap into:

Tagged filters (alpha)

This library is also the home of Watchexec's current two filtering implementations: the v1 behaviour which has proven confusing and inconsistent over the years, and an upcoming complete overhaul called "tagged filtering."

Have a look at the docs!