Skip to content

Commit 200fa2d

Browse files
committed
fix: expand %{deps} in (cat) properly
Signed-off-by: Ali Caglayan <alizter@gmail.com>
1 parent 0b191f1 commit 200fa2d

3 files changed

Lines changed: 27 additions & 17 deletions

File tree

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ Unreleased
33

44
- Add `dune show rules` as alias of the `dune rules` command. (#8000, @Alizter)
55

6+
- Fix `%{deps}` to expand properly in `(cat ...)` when containing 2 or more
7+
items. (#8196, @Alizter)
8+
69
- Add `dune show installed-libraries` as an alias of the `dune
710
installed-libraries` command. (#8135, @Alizter)
811

src/dune_rules/action_unexpanded.ml

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ module Action_expander : sig
6868
(* Evaluate a path in a position of dependency, such as in [(cat <dep>)] *)
6969
val dep : String_with_vars.t -> Path.t t
7070

71+
(* Evaluate paths in the position of dependencies, such as in [(cat <deps>)] *)
72+
val deps : String_with_vars.t -> Path.t list t
73+
7174
(* Evaluate a path in a position of optional dependency, such as in [(diff
7275
<dep_if_exists> ...)] *)
7376
val dep_if_exists : String_with_vars.t -> Path.t t
@@ -241,6 +244,14 @@ end = struct
241244
Value.to_path_in_build_or_external v
242245
~error_loc:(String_with_vars.loc sw) ~dir:t.dir
243246

247+
let expand_paths t sw =
248+
let+ v, vs = expand t ~mode:At_least_one sw in
249+
List.map
250+
~f:
251+
(Value.to_path_in_build_or_external
252+
~error_loc:(String_with_vars.loc sw) ~dir:t.dir)
253+
(v :: vs)
254+
244255
let expand_string env sw =
245256
let+ v = expand env ~mode:Single sw in
246257
Value.to_string v ~dir:(Path.build env.dir)
@@ -285,24 +296,26 @@ end = struct
285296
let path sw ~f = make ~expand:Expander.No_deps.expand_path sw ~f
286297
end
287298

288-
let register_dep x ~f env acc =
299+
let register_deps x ~f env acc =
289300
Memo.return
290301
(if not env.infer then (x, acc)
291302
else
292-
let x = Action_builder.memoize "dep" x in
303+
let x = Action_builder.memoize "deps" x in
293304
( x
294305
, { acc with
295306
deps =
296307
(let+ x = x
297308
and+ set = acc.deps in
298-
match f x with
299-
| None -> set
300-
| Some fn -> Path.Set.add set fn)
309+
Path.Set.union set (Path.Set.of_list (f x)))
301310
} ))
302311

303312
let dep sw env acc =
304313
let fn = Expander.expand_path env sw in
305-
register_dep fn ~f:Option.some env acc
314+
register_deps fn ~f:List.singleton env acc
315+
316+
let deps sw env acc =
317+
let fn = Expander.expand_paths env sw in
318+
register_deps fn ~f:Fun.id env acc
306319

307320
let dep_if_exists sw env acc =
308321
Memo.return
@@ -366,9 +379,9 @@ end = struct
366379
let args = Value.L.to_strings ~dir args in
367380
(prog, args)
368381
in
369-
register_dep b env acc ~f:(function
370-
| Ok p, _ -> Some p
371-
| Error _, _ -> None)
382+
register_deps b env acc ~f:(function
383+
| Ok p, _ -> [ p ]
384+
| Error _, _ -> [])
372385
end
373386
end
374387

@@ -427,8 +440,8 @@ let rec expand (t : Dune_lang.Action.t) ~context : Action.t Action_expander.t =
427440
let l = List.concat l in
428441
O.Echo l
429442
| Cat xs ->
430-
let+ xs = A.all (List.map xs ~f:E.dep) in
431-
O.Cat xs
443+
let+ xs = A.all (List.map xs ~f:E.deps) in
444+
O.Cat (List.concat xs)
432445
| Copy (x, y) ->
433446
let+ x = E.dep x
434447
and+ y = E.target y in

test/blackbox-tests/test-cases/quoting/cat.t/run.t

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,3 @@ arguments.
1313
> EOF
1414

1515
$ dune build @foo
16-
File "dune", line 5, characters 7-14:
17-
5 | (cat %{deps})))
18-
^^^^^^^
19-
Error: Variable %{deps} expands to 2 values, however a single value is
20-
expected here. Please quote this atom.
21-
[1]

0 commit comments

Comments
 (0)