Dune exec + dune build running in the same project (web server + melange)

In a full stack project with both a web server and a melange frontend:

  • it’s nice to start the web server with dune exec -w my_server
  • build the melange project with dune build -w @my_react_app

but when running either one after the other you get a dune lock error: A running dune (pid: ...) instance has locked the build directory.

How can you run both watchers at the same time? Or is there a better way?

You can’t. You can use another Dune instance as a RPC Client to the watching Dune process, but that does only work (for some definition of “work”) with dune build not with dune exec.


Isn’t exporting DUNE_CONFIG__GLOBAL_LOCK=disabled in one of the terminal an option?


That does actually work!? :open_mouth: I haven’t found that in the documentation or the Dune sources (granted, locally they are at 3.7)

this works!

I wonder if there are other options as well :thinking:

Those are not documented on purpose since we don’t guarantee their stability and are really a backdoor unsafe workaround. The only documentation is in https://github.com/ocaml/dune/blob/main/src/dune_config/config.mli.

The ability to run two instances of Dune with one automatically connecting to the RPC server of the other is something we want to add in the future, but most of our development time has been focused on package management these past few months.

For dune exec there is actually a specific option that would fit your use case better called --no-build. You will need to build the executable in the watch mode dune but you should be able to execute it without running into the lock separately. This is probably what you want to do. I am however unsure how this interacts with -w for exec.

1 Like

thanks, unfortunately it seems that it doesn’t work with -w, I still get:

Error: A running dune (pid: 17976) instance has locked the build directory.
If this is not the case, please delete _build/.lock

Without -w it works, but then the web server doesn’t restart automatically whenever a change is made

@mudrz Could you create an issue in the Dune repo. I think it should be straight forward to support such a feature and it would be beneficial to other users too. We can flesh out a design in the issue.

Created here: allow dune exec -w + dune build -w running at the same time · Issue #9290 · ocaml/dune · GitHub

1 Like