Sample code of dune watching __external__ directory?

Does anyone have a snippet showing dune monitoring an external directory. My use case is something like this:

proj/
  rust/
    crate_a/
    crate_b/
    crate_c/
  ocaml/
    dune
    foo/
      dune
    bar/
      dune

Now, I want ocaml/bar/dune to be have as follows:

  1. watch rust/crate_b/**/*.rs for changes
  2. on change, recompile crate_b into a wasm
  3. copy wasm over into ocaml/bar/

I have code for #2 and #3. Is there a way to do #1? The goal is to use dune to monitor an EXTERNAL directory for changes.

I don’t know if there’s a dune specific option but for ad hoc stuff like this, I would consider using: entr: rerun your build when files change

Combined with a tool like foreman, you can build some pretty specific workflows, see this for instance.

  1. Thank you for telling me about entr , looks like a useful general tool.

  2. As a rule of thumb, would you say that the moment I’m asking /foo/bar/dune to monitor files outside of /foo/bar I’m probably mis-using dune ?

I have no idea, I’m new to OCaml like you :slight_smile:

But I’m pretty sure you can hack something with entr that would fit you requirements

1 Like

Do we assume the dune root is proj/ or ocaml/?
If it’s ocaml/, I’m no dune expert, but I would say yes, it’s a misuse of the tool to try to trigger action based on something happening outside the root.
If it’s proj/ then you could may obtain the desired effect by treating rust/ as a data_only_dirs (see Stanza Reference — Dune documentation)

I completely agree that entr is sufficient; it looks easier to use than inotifywatch.

The reason I’m curious about dune is I’m trying to minimize the # of tools I’m juggling to get everything automatically rebuilding.