Skip to content

Commit 988af36

Browse files
authored
Merge pull request ocaml#12094 from Alizter/dune-exec-abs
feat(exec): support absolute paths for dune exec
2 parents 7777d3f + dbff7f5 commit 988af36

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

bin/exec.ml

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,19 @@ let get_path common sctx ~prog =
136136
| true -> Memo.return path
137137
| false -> not_found_with_suggestions ~dir ~prog)
138138
| Absolute ->
139-
(match
140-
let prog = Path.of_string prog in
141-
if Path.exists prog
142-
then Some prog
143-
else if not Sys.win32
144-
then None
145-
else (
146-
let prog = Path.extend_basename prog ~suffix:Bin.exe in
147-
Option.some_if (Path.exists prog) prog)
148-
with
149-
| Some prog -> Memo.return prog
150-
| None -> not_found_with_suggestions ~dir ~prog)
139+
let path =
140+
Path.of_string prog
141+
|> Path.Expert.try_localize_external
142+
|> Path.to_string
143+
|> Path.relative_to_source_in_build_or_external ~dir
144+
in
145+
if Path.equal (Path.external_ Path.External.root) path
146+
then not_found ~hints:[] ~prog
147+
else
148+
Build_system.file_exists path
149+
>>= (function
150+
| true -> Memo.return path
151+
| false -> not_found_with_suggestions ~dir ~prog)
151152
;;
152153

153154
let get_path_and_build_if_necessary common sctx ~no_rebuild ~prog =

doc/changes/12094.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `dune exec` now accepts absolute paths inside the workspace. (#12094,
2+
@Alizter)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Testing interaction of dune exec and absolute directories.
2+
3+
$ cat > dune-project <<EOF
4+
> (lang dune 3.20)
5+
> EOF
6+
7+
$ cat > dune <<EOF
8+
> (executable
9+
> (name foo))
10+
> EOF
11+
12+
$ cat > foo.ml <<EOF
13+
> let () = print_endline "hi" ;;
14+
> EOF
15+
16+
$ dune exec ./foo.exe
17+
hi
18+
19+
Dune exec is able to handle absolute executable paths.
20+
21+
$ dune exec $PWD/foo.exe
22+
hi
23+
24+
Lets check some validation:
25+
26+
$ dune exec ..
27+
Error: Program '..' not found!
28+
[1]
29+
$ dune exec .
30+
Error: Program '.' not found!
31+
[1]
32+
$ dune exec /.
33+
Error: Program '/.' not found!
34+
[1]
35+
$ dune exec /
36+
Error: Program '/' not found!
37+
[1]
38+

0 commit comments

Comments
 (0)