diff --git a/src/action.ml b/src/action.ml index 67295852..f6083123 100644 --- a/src/action.ml +++ b/src/action.ml @@ -741,7 +741,7 @@ let exec_run_direct ~ectx ~dir ~env ~stdout_to ~stderr_to prog args = Process.run Strict ~dir ~env ~stdout_to ~stderr_to ~purpose:ectx.purpose - (Path.reach_for_running ~from:dir prog) args + prog args let exec_run ~stdout_to ~stderr_to = let stdout_to = get_std_output stdout_to in diff --git a/src/process.ml b/src/process.ml index f1d8bace..000d8364 100644 --- a/src/process.ml +++ b/src/process.ml @@ -112,7 +112,7 @@ module Fancy = struct | x :: rest -> x :: colorize_args rest let command_line ~prog ~args ~dir ~stdout_to ~stderr_to = - let prog = Path.to_string prog in + let prog = Path.reach_for_running ?from:dir prog in let quote = quote_for_shell in let prog = colorize_prog (quote prog) in let s = @@ -232,7 +232,7 @@ let run_internal ?dir ?(stdout_to=Terminal) ?(stderr_to=Terminal) ~env ~purpose if display = Verbose then Format.eprintf "@{Running@}[@{%d@}]: %s@." id (Colors.strip_colors_for_stderr command_line); - let prog = Path.to_string prog in + let prog = Path.reach_for_running ?from:dir prog in let argv = Array.of_list (prog :: args) in let output_filename, stdout_fd, stderr_fd, to_close = match stdout_to, stderr_to with @@ -347,11 +347,11 @@ let run_capture_line ?dir ~env ?(purpose=Internal_job) fail_mode prog args = | [x] -> x | l -> let cmdline = - let prog = Path.to_string prog in - let s = String.concat (prog :: args) ~sep:" " in + let prog = Path.reach_for_running ?from:dir prog in + let prog_display = String.concat (prog :: args) ~sep:" " in match dir with - | None -> s - | Some dir -> sprintf "cd %s && %s" (Path.to_string dir) s + | None -> prog_display + | Some dir -> sprintf "cd %s && %s" (Path.to_string dir) prog_display in match l with | [] -> diff --git a/src/stdune/path.ml b/src/stdune/path.ml index 22c8a334..02d8429c 100644 --- a/src/stdune/path.ml +++ b/src/stdune/path.ml @@ -302,7 +302,7 @@ let reach t ~from = | Local t, Local from -> Local.reach t ~from -let reach_for_running t ~from = +let reach_for_running ?(from=root) t = match kind t, kind from with | External _, _ -> t | Local _, External _ -> diff --git a/src/stdune/path.mli b/src/stdune/path.mli index 44663266..7936ed11 100644 --- a/src/stdune/path.mli +++ b/src/stdune/path.mli @@ -70,7 +70,9 @@ val absolute : string -> t val to_absolute_filename : t -> root:string -> string val reach : t -> from:t -> string -val reach_for_running : t -> from:t -> t + +(** [from] defaults to [Path.root] *) +val reach_for_running : ?from:t -> t -> string val descendant : t -> of_:t -> t option val is_descendant : t -> of_:t -> bool diff --git a/test/unit-tests/path.mlt b/test/unit-tests/path.mlt index 3f01e31b..dfcb4fed 100644 --- a/test/unit-tests/path.mlt +++ b/test/unit-tests/path.mlt @@ -284,3 +284,31 @@ Path.is_in_build_dir Path.build_dir [%%expect{| - : bool = false |}] + +Path.reach_for_running Path.build_dir ~from:Path.root +[%%expect{| +- : string = "./_build" +|}] + +Path.(reach_for_running (relative build_dir "foo/baz") + ~from:(relative build_dir "foo/bar/baz")) +[%%expect{| +- : string = "../../baz" +|}] + +Path.(reach_for_running (Path.absolute "/fake/path") + ~from:(relative build_dir "foo/bar/baz")) +[%%expect{| +- : string = "/fake/path" +|}] + +Path.(reach_for_running (relative build_dir "foo/baz") + ~from:(Path.absolute "/fake/path")) +[%%expect{| +Exception: Stdune__Exn.Code_error . +|}] + +Path.(reach_for_running (relative root "foo") ~from:(Path.relative root "foo")) +[%%expect{| +- : string = "./." +|}]