Skip to content

Commit c0796a6

Browse files
committed
Fix exec -w for relative paths with --root argument
Passing --root to `dune build` and `dune exec` causes relative paths to files to be resolved relative to the workspace root rather than the working directory (in addition to its main function of explicitly setting the workspace root directory). This was not implemented correctly for exec watch mode, where relative paths would be resolved relative to the working directory instead. There was already a test for this which was failing, however the test is disabled in CI as it is known to be flaky. Signed-off-by: Stephen Sherratt <[email protected]>
1 parent 63fac22 commit c0796a6

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

bin/exec.ml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,26 @@ module Command_to_exec = struct
7777

7878
(* Helper function to spawn a new process running a command in an
7979
environment, returning the new process' pid *)
80-
let spawn_process path ~args ~env =
80+
let spawn_process (common : Common.t) path ~args ~env =
8181
let pid =
82-
let path = Path.to_string path in
82+
let prog = string_path_relative_to_specified_root common (Path.to_string path) in
8383
let env = Env.to_unix env |> Spawn.Env.of_list in
84-
let argv = path :: args in
84+
let argv = prog :: args in
8585
let cwd = Spawn.Working_dir.Path Fpath.initial_cwd in
86-
Spawn.spawn ~prog:path ~env ~cwd ~argv ()
86+
Spawn.spawn ~prog ~env ~cwd ~argv ()
8787
in
8888
Pid.of_int pid
8989
;;
9090

9191
(* Run the command, first (re)building the program which the command is
9292
invoking *)
93-
let build_and_run_in_child_process { get_path_and_build_if_necessary; prog; args; env } =
93+
let build_and_run_in_child_process
94+
common
95+
{ get_path_and_build_if_necessary; prog; args; env }
96+
=
9497
get_path_and_build_if_necessary prog
95-
|> Fiber.map ~f:(Result.map ~f:(spawn_process ~args ~env))
98+
|> Fiber.map
99+
~f:(Result.map ~f:(fun exe_path -> spawn_process common ~args ~env exe_path))
96100
;;
97101
end
98102

@@ -139,18 +143,18 @@ module Watch = struct
139143

140144
(* Kills the currently running process, then runs the given command after
141145
(re)building the program which it will invoke *)
142-
let run state ~command_to_exec =
146+
let run common state ~command_to_exec =
143147
let open Fiber.O in
144148
let* () = Fiber.return () in
145149
let* () = kill_currently_running_process state in
146150
let* command_to_exec = command_to_exec () in
147-
Command_to_exec.build_and_run_in_child_process command_to_exec
151+
Command_to_exec.build_and_run_in_child_process common command_to_exec
148152
>>| Result.map ~f:(fun pid -> state.currently_running_pid := Some pid)
149153
;;
150154

151-
let loop ~command_to_exec =
155+
let loop common ~command_to_exec =
152156
let state = init_state () in
153-
Scheduler.Run.poll (run state ~command_to_exec)
157+
Scheduler.Run.poll (run common state ~command_to_exec)
154158
;;
155159
end
156160

@@ -322,7 +326,7 @@ module Exec_context = struct
322326
; env
323327
}
324328
in
325-
Watch.loop ~command_to_exec
329+
Watch.loop common ~command_to_exec
326330
;;
327331
end
328332

bin/import.ml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,16 @@ module Scheduler = struct
237237
;;
238238
end
239239

240-
let restore_cwd_and_execve (common : Common.t) prog argv env =
241-
let prog =
242-
if Filename.is_relative prog
243-
then (
244-
let root = Common.root common in
245-
Filename.concat root.dir prog)
246-
else prog
247-
in
240+
let string_path_relative_to_specified_root (common : Common.t) path =
241+
if Filename.is_relative path
242+
then (
243+
let root = Common.root common in
244+
Filename.concat root.dir path)
245+
else path
246+
;;
247+
248+
let restore_cwd_and_execve common prog argv env =
249+
let prog = string_path_relative_to_specified_root common prog in
248250
Proc.restore_cwd_and_execve prog argv ~env
249251
;;
250252

test/blackbox-tests/test-cases/exec-watch/exec-watch-multi-levels.t/run.t

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ Perform the same test above but first enter the "bin" directory.
2222
Success, waiting for filesystem changes...
2323
foo
2424
Leaving directory '..'
25+
$ PID=$!
2526
$ cd ..
26-
$ wait
27+
$ ../wait-for-file.sh $DONE_FLAG
28+
$ kill $PID
2729

2830
Test that the behaviour is the same when not running with "--watch"
2931
$ cd bin && dune exec --root .. ./bin/main.exe

0 commit comments

Comments
 (0)