Skip to content

Commit

Permalink
feat(dune): support + prefixes for PPX CLI flags (#11234)
Browse files Browse the repository at this point in the history
* test(ppx): show flags starting with `+` are interpreted as library names

Signed-off-by: Antonio Nuno Monteiro <[email protected]>

* feat(dune): support `+` prefixes for PPX CLI flags

Signed-off-by: Antonio Nuno Monteiro <[email protected]>

* cr: only enabled for Dune versions starting with 3.18

Signed-off-by: Antonio Nuno Monteiro <[email protected]>

* cr: add a better error message for `prefix = "+"`

Signed-off-by: Antonio Nuno Monteiro <[email protected]>

---------

Signed-off-by: Antonio Nuno Monteiro <[email protected]>
  • Loading branch information
anmonteiro authored Dec 25, 2024
1 parent 0a43607 commit 30af715
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/dune_rules/preprocess.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,23 @@ module Pps_and_flags = struct
and+ syntax_version = Dune_lang.Syntax.get_exn Stanza.syntax in
let pps, more_flags =
List.partition_map l ~f:(fun s ->
match String_with_vars.is_prefix ~prefix:"-" s with
| Yes -> Right s
| No | Unknown _ ->
match
( String_with_vars.is_prefix ~prefix:"-" s
, String_with_vars.is_prefix ~prefix:"+" s )
with
| Yes, _ -> Right s
| _, Yes ->
if syntax_version >= (3, 18)
then Right s
else
User_error.raise
~loc:(String_with_vars.loc s)
~hints:[ Pp.text "Upgrade your dune-project to `(lang dune 3.18)'" ]
[ Pp.text
"PPX args starting with `+' cannot be used before version 3.18 of the \
dune language"
]
| (No | Unknown _), _ ->
let loc = String_with_vars.loc s in
(match String_with_vars.text_only s with
| None ->
Expand Down
49 changes: 49 additions & 0 deletions test/blackbox-tests/test-cases/ppx/ppx-flags-plus.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Create ppx1 and exe:

$ cat > dune-project <<EOF
> (lang dune 3.7)
> EOF
$ cat > dune <<EOF
> (library
> (name ppx)
> (kind ppx_rewriter)
> (modules ppx)
> (ppx.driver (main Ppx.main)))
> (executable
> (name the_exe)
> (modules the_exe)
> (preprocess (pps ppx --alert ++foo)))
> EOF
$ cat > ppx.ml <<EOF
> let main () =
> let out = ref "" in
> let args =
> [ ("-o", Arg.Set_string out, "")
> ; ("--impl", Arg.Set_string (ref ""), "")
> ; ("--as-ppx", Arg.Set (ref false), "")
> ; ("--cookie", Arg.Set (ref false), "")
> ; ("--alert", Arg.Set_string (ref ""), "")
> ]
> in
> let anon _ = () in
> Arg.parse (Arg.align args) anon "";
> let out = open_out !out in
> close_out out;
> EOF
$ touch the_exe.ml

$ dune build ./the_exe.exe
File "dune", line 9, characters 30-35:
9 | (preprocess (pps ppx --alert ++foo)))
^^^^^
Error: PPX args starting with `+' cannot be used before version 3.18 of the
dune language
Hint: Upgrade your dune-project to `(lang dune 3.18)'
[1]

Works since Dune 3.18

$ cat > dune-project <<EOF
> (lang dune 3.18)
> EOF
$ dune build ./the_exe.exe

0 comments on commit 30af715

Please sign in to comment.