Skip to content

Commit 93e1d88

Browse files
authored
fix: interpret relative paths in %{bin:..} and %{bin-available:..} (#13712)
fixes #9564 Signed-off-by: Antonio Nuno Monteiro <anmonteiro@gmail.com>
1 parent f8558e7 commit 93e1d88

File tree

12 files changed

+107
-19
lines changed

12 files changed

+107
-19
lines changed

doc/changes/fixed/13712.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Fixed interpreting relative paths in `%{bin:..}` and `%{bin-available:..}`.
2+
These are now interpreted correctly, relative to the dune file they're in.
3+
(#13712, fixes #9564, @anmonteiro)

src/dune_rules/action_unexpanded.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,9 @@ end = struct
403403
Action_builder.of_memo
404404
@@
405405
let open Memo.O in
406+
let dir = env.dir in
406407
let* where =
407-
let+ project = Dune_load.find_project ~dir:env.dir in
408+
let+ project = Dune_load.find_project ~dir in
408409
if Dune_project.dune_version project >= (3, 14)
409410
then Artifacts.Original_path
410411
else Install_dir
@@ -415,7 +416,7 @@ end = struct
415416
| "rescript_syntax" -> Some "opam install rescript-syntax"
416417
| _ -> None
417418
in
418-
Artifacts.binary ?hint ~loc:(Some loc) ~where artifacts s)
419+
Artifacts.binary ?hint ~loc:(Some loc) ~where ~dir artifacts s)
419420
in
420421
let args = Value.L.to_strings ~dir args in
421422
match prog with

src/dune_rules/artifacts.ml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,23 @@ let local_binaries { local_bins; _ } =
4646
| _, Origin _origins -> None)
4747
;;
4848

49-
let analyze_binary t name =
49+
let analyze_binary t ~dir name =
5050
match Filename.is_relative name with
5151
| false -> Memo.return (`Resolved (Path.of_filename_relative_to_initial_cwd name))
5252
| true ->
5353
let* local_bins = Memo.Lazy.force t.local_bins in
54+
let lookup_name =
55+
match Filename.analyze_program_name name with
56+
| Absolute | In_path -> name
57+
| Relative_to_current_dir -> Path.Build.relative dir name |> Path.Build.basename
58+
in
5459
let which () =
55-
Context.which t.context name
60+
Context.which t.context lookup_name
5661
>>| function
5762
| None -> `None
5863
| Some path -> `Resolved path
5964
in
60-
(match Filename.Map.find local_bins name with
65+
(match Filename.Map.find local_bins lookup_name with
6166
| Some (Resolved p) -> Memo.return (`Resolved (Path.build p.path))
6267
| None -> which ()
6368
| Some (Origin origins) ->
@@ -82,8 +87,8 @@ let analyze_binary t name =
8287
]))
8388
;;
8489

85-
let binary t ?hint ?(where = Install_dir) ~loc name =
86-
analyze_binary t name
90+
let binary t ?hint ?(where = Install_dir) ~dir ~loc name =
91+
analyze_binary t ~dir name
8792
>>= function
8893
| `Resolved path -> Memo.return @@ Ok path
8994
| `None ->
@@ -107,8 +112,8 @@ let binary t ?hint ?(where = Install_dir) ~loc name =
107112
Ok (Path.build src))
108113
;;
109114

110-
let binary_available t name =
111-
analyze_binary t name
115+
let binary_available t ~dir name =
116+
analyze_binary t ~dir name
112117
>>| function
113118
| `None -> false
114119
| `Resolved _ | `Origin _ -> true

src/dune_rules/artifacts.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ val binary
3333
: t
3434
-> ?hint:string
3535
-> ?where:where
36+
-> dir:Path.Build.t
3637
-> loc:Loc.t option
3738
-> Filename.t
3839
-> Action.Prog.t Memo.t
3940

40-
val binary_available : t -> string -> bool Memo.t
41+
val binary_available : t -> dir:Path.Build.t -> string -> bool Memo.t
4142
val add_binaries : t -> dir:Path.Build.t -> File_binding.Expanded.t list -> t
4243

4344
val create

src/dune_rules/coq/coq_config.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,12 @@ let by_name { coqlib; coqcorelib; coq_native_compiler_default } name =
248248
[ "name", Dyn.string name ]
249249
;;
250250

251-
let expand source macro artifacts_host =
251+
let expand source macro ~dir artifacts_host =
252252
let s = Pform.Macro_invocation.Args.whole macro in
253253
let open Memo.O in
254-
let* coqc = Artifacts.binary artifacts_host ~where:Original_path ~loc:None "coqc" in
254+
let* coqc =
255+
Artifacts.binary artifacts_host ~where:Original_path ~dir ~loc:None "coqc"
256+
in
255257
let expand m k s =
256258
let+ t = m ~coqc in
257259
match t with

src/dune_rules/coq/coq_config.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ val by_name : t -> string -> Value.t Option.t
6565
val expand
6666
: Dune_lang.Template.Pform.t
6767
-> Pform.Macro_invocation.t
68+
-> dir:Path.Build.t
6869
-> Artifacts.t
6970
-> Dune_lang.Value.t list Memo.t

src/dune_rules/expander.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ let expand_pform_macro
664664
let* artifacts_host = t.artifacts_host in
665665
Artifacts.binary
666666
~loc:(Some (Dune_lang.Template.Pform.loc source))
667+
~dir:t.dir
667668
artifacts_host
668669
s)
669670
in
@@ -692,7 +693,7 @@ let expand_pform_macro
692693
Without
693694
(let open Memo.O in
694695
let* artifacts_host = t.artifacts_host in
695-
let+ b = Artifacts.binary_available artifacts_host s in
696+
let+ b = Artifacts.binary_available artifacts_host ~dir:t.dir s in
696697
b |> string_of_bool |> string))
697698
| File_available ->
698699
Direct
@@ -723,7 +724,7 @@ let expand_pform_macro
723724
Without
724725
(let open Memo.O in
725726
let* artifacts_host = t.artifacts_host in
726-
Coq_config.expand source macro_invocation artifacts_host))
727+
Coq_config.expand source macro_invocation ~dir:t.dir artifacts_host))
727728
| Ppx ->
728729
Need_full_expander
729730
(fun t ->
@@ -747,7 +748,7 @@ let expand_pform_macro
747748
Without
748749
(let open Memo.O in
749750
let* artifacts_host = t.artifacts_host in
750-
Rocq_config.expand source macro_invocation artifacts_host))
751+
Rocq_config.expand source macro_invocation ~dir:t.dir artifacts_host))
751752
;;
752753

753754
let expand_pform_gen ~(context : Context.t) ~bindings ~dir ~source (pform : Pform.t)

src/dune_rules/rocq/rocq_config.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,12 @@ let by_name { rocqlib; rocq_native_compiler_default } name =
257257
[ "name", Dyn.string name ]
258258
;;
259259

260-
let expand source macro artifacts_host =
260+
let expand source macro ~dir artifacts_host =
261261
let s = Pform.Macro_invocation.Args.whole macro in
262262
let open Memo.O in
263-
let* rocq = Artifacts.binary artifacts_host ~where:Original_path ~loc:None "rocq" in
263+
let* rocq =
264+
Artifacts.binary artifacts_host ~where:Original_path ~dir ~loc:None "rocq"
265+
in
264266
let expand m k s =
265267
let+ t = m ~rocq in
266268
match t with

src/dune_rules/rocq/rocq_config.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,6 @@ val by_name : t -> string -> Value.t Option.t
7777
val expand
7878
: Dune_lang.Template.Pform.t
7979
-> Pform.Macro_invocation.t
80+
-> dir:Path.Build.t
8081
-> Artifacts.t
8182
-> Dune_lang.Value.t list Memo.t

src/dune_rules/super_context.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ let add_alias_action t alias ~dir ~loc action =
181181

182182
let resolve_program_memo t ~dir ?where ?hint ~loc bin =
183183
let* artifacts = artifacts_host t ~dir in
184-
Artifacts.binary ?hint ?where ~loc artifacts bin
184+
Artifacts.binary ?hint ?where ~dir ~loc artifacts bin
185185
;;
186186

187187
let resolve_program t ~dir ?where ?hint ~loc bin =

0 commit comments

Comments
 (0)