diff --git a/bin/exec.ml b/bin/exec.ml index 7d6d7ae063a3..d18191afb0c5 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 000000000000..ed07efcb3262 --- /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 722c924e9ccd..f1ee08d2e39d 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 ..