From 30af715e40bcc8e0565f36a5e729f5f55ec36135 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Wed, 25 Dec 2024 01:10:07 -0800 Subject: [PATCH] feat(dune): support `+` prefixes for PPX CLI flags (#11234) * test(ppx): show flags starting with `+` are interpreted as library names Signed-off-by: Antonio Nuno Monteiro * feat(dune): support `+` prefixes for PPX CLI flags Signed-off-by: Antonio Nuno Monteiro * cr: only enabled for Dune versions starting with 3.18 Signed-off-by: Antonio Nuno Monteiro * cr: add a better error message for `prefix = "+"` Signed-off-by: Antonio Nuno Monteiro --------- Signed-off-by: Antonio Nuno Monteiro --- src/dune_rules/preprocess.ml | 20 ++++++-- .../test-cases/ppx/ppx-flags-plus.t | 49 +++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 test/blackbox-tests/test-cases/ppx/ppx-flags-plus.t diff --git a/src/dune_rules/preprocess.ml b/src/dune_rules/preprocess.ml index 73cf4a1a093..b39dbd0eb6f 100644 --- a/src/dune_rules/preprocess.ml +++ b/src/dune_rules/preprocess.ml @@ -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 -> diff --git a/test/blackbox-tests/test-cases/ppx/ppx-flags-plus.t b/test/blackbox-tests/test-cases/ppx/ppx-flags-plus.t new file mode 100644 index 00000000000..ec95aac9429 --- /dev/null +++ b/test/blackbox-tests/test-cases/ppx/ppx-flags-plus.t @@ -0,0 +1,49 @@ +Create ppx1 and exe: + + $ cat > dune-project < (lang dune 3.7) + > EOF + $ cat > dune < (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 < 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 < (lang dune 3.18) + > EOF + $ dune build ./the_exe.exe