Dune: Make `--watch` work for `dune exec`

Created on 27 Nov 2019  路  5Comments  路  Source: ocaml/dune

When developing web services, it is often useful to rebuild and restart the executable on any file changes. Thus, it would be useful to be able to run dune exec foo --watch and have the program restarted on changes.

While --watch is a valid argument to dune exec (since is a common flag), afaict it is not implemented for dune exec. It seems to only be implemented for commands that go through Main.run_build_command, whereas Exec.command invokes the scheduler with Import.do_build directly, bypassing the handling for watch.

Any thoughts on how desirable this would be and how difficult it would be to implement?

Most helpful comment

A workaround I use from time to time is to define an alias which the correspond to the execution of the command, and build -w this alias:

(alias
  (name run)
  (action (run x.exe args)))

and dune build -w @run

All 5 comments

In the meantime (or as a substitute in case this feature proposal is rejected) users who want this functionality can drop this script into your project's root, and invoke it via a Makefile:

#!/usr/bin/env bash

# Runs the server, restarting it on file changes

source_dirs="lib bin"
args=${@:-"myprogram_name"}
cmd="dune exec ${args}"

sigint_handler()
{
  kill (jobs -pr)
  exit
}

trap sigint_handler SIGINT
while true; do
  echo "running: $cmd"
  $cmd &
  inotifywait -e modify -e move -e create -e delete -e attrib -r $source_dirs
  echo "restarting..."
  kill $(jobs -pr)
done

(Adapted from https://stackoverflow.com/a/34672970/1187277)

That seems fine to me.

A workaround I use from time to time is to define an alias which the correspond to the execution of the command, and build -w this alias:

(alias
  (name run)
  (action (run x.exe args)))

and dune build -w @run

@emillon This is a nice workaround for dune 1, but I wonder how this translates to dune 2, which complains with:

Error: 'action' was deleted in version 2.0 of the dune language. Use a rule
stanza with the alias field instead

However, using (rule ...) requires at least one target name, even when used as (rule ... (alias ...)). So do we need an empty dummy target file here, or is there a nicer version of this workaround?

So I think that in dune 2 it is even more important to finally have -w for dune exec instead.

@vog the workaround works in dune 2.x as well, with the following syntax:

(rule
 (alias run)
 (action
  (run ./x.exe args)))

(the ./ part is necessary in dune 1 as well, I edited my previous comment)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shonfeder picture shonfeder  路  6Comments

Chris00 picture Chris00  路  8Comments

bobot picture bobot  路  7Comments

bobzhang picture bobzhang  路  7Comments

XVilka picture XVilka  路  9Comments