Skip to content

Commit

Permalink
Fix dune exec -- ../run.exe
Browse files Browse the repository at this point in the history
Signed-off-by: François Bobot <[email protected]>
  • Loading branch information
bobot committed Apr 22, 2022
1 parent e043e9b commit 175dbae
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion bin/exec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ let term =
| Error (_ : Action.Prog.Not_found.t) -> not_found ()
| Ok prog -> build_prog prog)
| Relative_to_current_dir -> (
let path = Path.relative (Path.build dir) prog in
let path = Path.relative_to_source_in_build ~dir prog in
(Build_system.file_exists path >>= function
| true -> Memo.return (Some path)
| false -> (
Expand Down
11 changes: 11 additions & 0 deletions otherlibs/stdune/path.ml
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,17 @@ let explode_exn t =
| Some s -> s
| None -> Code_error.raise "Path.explode_exn" [ ("path", to_dyn t) ]

let relative_to_source_in_build ?error_loc ~dir s =
match Build.extract_build_context dir with
| None -> relative ?error_loc (In_build_dir dir) s
| Some (bctxt, source) -> (
let path = relative ?error_loc (In_source_tree source) s in
match path with
| In_source_tree s ->
In_build_dir
(Build.relative (Build.of_string bctxt) (Source0.to_string s))
| In_build_dir _ | External _ -> path)

let exists t = try Sys.file_exists (to_string t) with Sys_error _ -> false

let readdir_unsorted t = Dune_filesystem_stubs.read_directory (to_string t)
Expand Down
5 changes: 5 additions & 0 deletions otherlibs/stdune/path.mli
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ val is_managed : t -> bool

val relative : ?error_loc:Loc0.t -> t -> string -> t

(** [relative_to_source_in_build ~dir s] compute the path [s] relative to the
source directory corresponding to [dir] *)
val relative_to_source_in_build :
?error_loc:Loc0.t -> dir:Build.t -> string -> t

(** Create an external path. If the argument is relative, assume it is relative
to the initial directory dune was launched in. *)
val of_filename_relative_to_initial_cwd : string -> t
Expand Down
13 changes: 1 addition & 12 deletions src/dune_util/value.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,7 @@ let to_path ?error_loc t ~dir =

let to_path_in_build ?error_loc t ~dir =
match t with
| String s -> (
match Path.Build.extract_build_context dir with
| None -> Path.relative ?error_loc (Path.build dir) s
| Some (bctxt, source) -> (
let path = Path.relative ?error_loc (Path.source source) s in
match path with
| In_source_tree s ->
Path.build
(Path.Build.relative
(Path.Build.of_string bctxt)
(Path.Source.to_string s))
| In_build_dir _ | External _ -> path))
| String s -> Path.relative_to_source_in_build ?error_loc ~dir s
| Dir p | Path p -> p

module L = struct
Expand Down
24 changes: 24 additions & 0 deletions test/blackbox-tests/test-cases/rule/dependency-external.t
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,27 @@ rules with dependencies outside the build dir are allowed
$ dune build --root=a/b @test
Entering directory 'a/b'
txt2

# Test dune exec absolute
$ cat >a/script.sh <<EOF
> #!/bin/sh
> echo "txt1"
> EOF

$ chmod u+x a/script.sh

$ dune exec --root=a/b -- $(pwd)/a/script.sh
Entering directory 'a/b'
txt1

# Test dune exec 1 level below
$ dune exec --root=a/b -- ../script.sh
Entering directory 'a/b'
txt1

# Test dune exec 2 level below
$ mv a/script.sh .

$ dune exec --root=a/b -- ../../script.sh
Entering directory 'a/b'
txt1

0 comments on commit 175dbae

Please sign in to comment.