Skip to content

Commit 2d647ef

Browse files
authored
fix: account for workspace binaries when generating rules for ./.bin (#12952)
fixes #6220 - [x] changes entry --------- Signed-off-by: Antonio Nuno Monteiro <anmonteiro@gmail.com>
1 parent b4e845a commit 2d647ef

8 files changed

Lines changed: 33 additions & 44 deletions

File tree

doc/changes/fixed/12952.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Resolve context and workspace binaries introduced by the respective `(env
2+
(binaries ..))` stanzas. (#12952, fixes #6220, @anmonteiro)
3+

src/dune_rules/artifacts.ml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ type where =
1616
| Original_path
1717

1818
type path =
19-
| Resolved of Path.Build.t
19+
| Resolved of
20+
{ binding : File_binding.Expanded.t
21+
; path : Path.Build.t
22+
}
2023
| Origin of origin list
2124

2225
type local_bins = path Filename.Map.t
@@ -36,6 +39,13 @@ let force { local_bins; _ } =
3639
()
3740
;;
3841

42+
let local_binaries { local_bins; _ } =
43+
let+ local_bins = Memo.Lazy.force local_bins in
44+
List.filter_map (Filename.Map.to_list local_bins) ~f:(function
45+
| _, Resolved p -> Some p.binding
46+
| _, Origin _origins -> None)
47+
;;
48+
3949
let analyze_binary t name =
4050
match Filename.is_relative name with
4151
| false -> Memo.return (`Resolved (Path.of_filename_relative_to_initial_cwd name))
@@ -48,7 +58,7 @@ let analyze_binary t name =
4858
| Some path -> `Resolved path
4959
in
5060
(match Filename.Map.find local_bins name with
51-
| Some (Resolved p) -> Memo.return (`Resolved (Path.build p))
61+
| Some (Resolved p) -> Memo.return (`Resolved (Path.build p.path))
5262
| None -> which ()
5363
| Some (Origin origins) ->
5464
Memo.parallel_map origins ~f:(fun origin ->
@@ -108,9 +118,9 @@ let add_binaries t ~dir l =
108118
let local_bins =
109119
Memo.lazy_ ~name:"Artifacts.Bin.add_binaries" (fun () ->
110120
let+ local_bins = Memo.Lazy.force t.local_bins in
111-
List.fold_left l ~init:local_bins ~f:(fun acc fb ->
112-
let path = File_binding.Expanded.dst_path fb ~dir:(local_bin dir) in
113-
Filename.Map.set acc (Path.Build.basename path) (Resolved path)))
121+
List.fold_left l ~init:local_bins ~f:(fun acc binding ->
122+
let path = File_binding.Expanded.dst_path binding ~dir:(local_bin dir) in
123+
Filename.Map.set acc (Path.Build.basename path) (Resolved { binding; path })))
114124
in
115125
{ t with local_bins }
116126
;;

src/dune_rules/artifacts.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ val bin_dir_basename : Filename.t
2424
rules defined in [dir] *)
2525
val local_bin : Path.Build.t -> Path.Build.t
2626

27+
(** Binaries that are symlinked in the associated .bin directory *)
28+
val local_binaries : t -> File_binding.Expanded.t list Memo.t
29+
2730
(** A named artifact that is looked up in the PATH if not found in the tree If
2831
the name is an absolute path, it is used as it. *)
2932
val binary

src/dune_rules/env_node.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ let make
3838
>>= extend)
3939
in
4040
let config_binaries = Option.value config.binaries ~default:[] in
41-
let local_binaries =
42-
Memo.lazy_ (fun () ->
43-
Memo.parallel_map
44-
config_binaries
45-
~f:(File_binding_expand.expand ~dir ~f:(expand_str_lazy expander)))
46-
in
4741
let external_env =
4842
inherited ~field:external_env ~root:default_env (fun env ->
4943
let env =
@@ -58,7 +52,13 @@ let make
5852
in
5953
let artifacts =
6054
inherited ~field:artifacts ~root:default_artifacts (fun binaries ->
61-
Memo.Lazy.force local_binaries >>| Artifacts.add_binaries binaries ~dir)
55+
Memo.parallel_map
56+
config_binaries
57+
~f:(File_binding_expand.expand ~dir ~f:(expand_str_lazy expander))
58+
>>| Artifacts.add_binaries binaries ~dir)
59+
in
60+
let local_binaries =
61+
Memo.lazy_ (fun () -> Memo.Lazy.force artifacts >>= Artifacts.local_binaries)
6262
in
6363
{ external_env; artifacts; local_binaries }
6464
;;

src/dune_rules/env_node.mli

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ val make
1616

1717
val external_env : t -> Env.t Memo.t
1818

19-
(** Binaries that are symlinked in the associated .bin directory of [dir]. This
20-
associated directory is *)
19+
(** Binaries that are symlinked in the associated .bin directory of [dir]. *)
2120
val local_binaries : t -> File_binding.Expanded.t list Memo.t
2221

2322
val artifacts : t -> Artifacts.t Memo.t

test/blackbox-tests/test-cases/dune-workspace-binaries-overlap.t

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,11 @@ Workspace binary should print "Workspace."
7676

7777
$ dune clean
7878
$ dune build ./message.txt
79-
File "dune", lines 1-3, characters 0-78:
80-
1 | (rule
81-
2 | (target message.txt)
82-
3 | (action (with-stdout-to %{target} (run foobar))))
83-
Error: No rule found for .bin/foobar
84-
File "dune", lines 1-3, characters 0-78:
85-
1 | (rule
86-
2 | (target message.txt)
87-
3 | (action (with-stdout-to %{target} (run foobar))))
88-
Error: No rule found for .bin/foobar (context other_context)
89-
[1]
9079
$ cat _build/default/message.txt
91-
cat: _build/default/message.txt: No such file or directory
92-
[1]
80+
Workspace.
9381

9482
Context binaries override the workspace binaries. Expecting "Context."
9583

9684
$ cat _build/other_context/message.txt
97-
cat: _build/other_context/message.txt: No such file or directory
98-
[1]
85+
Context.
9986

test/blackbox-tests/test-cases/dune-workspace-binaries.t

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,3 @@ a dune-workspace file:
2727

2828
$ chmod +x test.sh
2929
$ dune build ./message.txt
30-
File "dune", lines 1-3, characters 0-78:
31-
1 | (rule
32-
2 | (target message.txt)
33-
3 | (action (with-stdout-to %{target} (run foobar))))
34-
Error: No rule found for .bin/foobar
35-
[1]

test/blackbox-tests/test-cases/github5555.t

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This test is about `binaries` in `env` stanzas in `dune-workspace` files.
1111
> EOF
1212

1313
$ cat >x <<EOF
14-
> #/bin/sh
14+
> #!/bin/sh
1515
> echo foo
1616
> EOF
1717

@@ -72,18 +72,11 @@ With 3.2, this fixes the error.
7272
In the default context:
7373

7474
$ t 3.2 _ x
75-
Error: No rule found for .bin/x
76-
-> required by %{bin:x} at dune:3
77-
-> required by alias foo in dune:1
78-
[1]
75+
foo
7976

8077
And for explicit profiles:
8178

8279
$ t 3.2 dev x
83-
Error: No rule found for .bin/x
84-
-> required by %{bin:x} at dune:3
85-
-> required by alias foo in dune:1
86-
[1]
8780

8881
And for another profile:
8982

0 commit comments

Comments
 (0)