From f072bb503861804ccfca07f82e420dbdb2151e45 Mon Sep 17 00:00:00 2001 From: Stephen Sherratt Date: Wed, 13 Mar 2024 15:16:49 +1100 Subject: [PATCH] Exec watch mode spawns processes with initial cwd Fixes a bug where commands run from `dune exec -w` would have the cwd of the project root rather than the directory where the command was run. Signed-off-by: Stephen Sherratt --- bin/exec.ml | 3 ++- doc/changes/10262.md | 1 + test/blackbox-tests/test-cases/exec-watch/exec-pwd.t/run.t | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 doc/changes/10262.md diff --git a/bin/exec.ml b/bin/exec.ml index 7d6d7ae063a..d18191afb0c 100644 --- a/bin/exec.ml +++ b/bin/exec.ml @@ -82,7 +82,8 @@ module Command_to_exec = struct let path = Path.to_string path in let env = Env.to_unix env |> Spawn.Env.of_list in let argv = path :: args in - Spawn.spawn ~prog:path ~env ~argv () + let cwd = Spawn.Working_dir.Path Fpath.initial_cwd in + Spawn.spawn ~prog:path ~env ~cwd ~argv () in Pid.of_int pid ;; diff --git a/doc/changes/10262.md b/doc/changes/10262.md new file mode 100644 index 00000000000..ed07efcb326 --- /dev/null +++ b/doc/changes/10262.md @@ -0,0 +1 @@ +- Fix bug with `dune exec --watch` where the working directory would always be set to the project root rather than the directory where the command was run (#10262, @gridbugs) diff --git a/test/blackbox-tests/test-cases/exec-watch/exec-pwd.t/run.t b/test/blackbox-tests/test-cases/exec-watch/exec-pwd.t/run.t index 722c924e9cc..f1ee08d2e39 100644 --- a/test/blackbox-tests/test-cases/exec-watch/exec-pwd.t/run.t +++ b/test/blackbox-tests/test-cases/exec-watch/exec-pwd.t/run.t @@ -10,7 +10,7 @@ In normal (non-watching) mode, pwd is the folder from which dune is launched $ rm -rf $OUTPUT $ cd .. -While in watch mode, pwd is always the root project folder +In watch mode, pwd is also the folder from which dune is launched. $ cd bin $ dune exec --root .. -w -- pwd > $OUTPUT & Entering directory '..' @@ -20,6 +20,6 @@ While in watch mode, pwd is always the root project folder $ until test -s $OUTPUT; do sleep 0.1; done; $ kill $PID $ cat $OUTPUT - $TESTCASE_ROOT + $TESTCASE_ROOT/bin $ rm -rf $OUTPUT $ cd ..