From 01729abfb2682a654bd3911449a64b509b2e3ac5 Mon Sep 17 00:00:00 2001 From: Etienne Millon Date: Thu, 3 Jun 2021 17:03:35 +0200 Subject: [PATCH] Support (package) in (mdx) This adds a (package) field to (mdx) with the usual semantics. The syntax is not ideal since (packages) is already supported as sugar to specify dependencies. See #4691 Closes #3756 Signed-off-by: Etienne Millon --- CHANGES.md | 2 ++ doc/dune-files.rst | 9 +++++ src/dune_rules/mdx.ml | 29 +++++++++++---- .../mdx-stanza.t/package-old-lang-dune/a.opam | 0 .../mdx-stanza.t/package-old-lang-dune/b.opam | 0 .../package-old-lang-dune/doc-a.md | 3 ++ .../package-old-lang-dune/doc-b.md | 3 ++ .../package-old-lang-dune/doc-nopkg.md | 3 ++ .../mdx-stanza.t/package-old-lang-dune/dune | 10 ++++++ .../package-old-lang-dune/dune-project | 2 ++ .../test-cases/mdx-stanza.t/package/a.opam | 0 .../test-cases/mdx-stanza.t/package/b.opam | 0 .../test-cases/mdx-stanza.t/package/doc-a.md | 3 ++ .../test-cases/mdx-stanza.t/package/doc-b.md | 3 ++ .../mdx-stanza.t/package/doc-nopkg.md | 3 ++ .../test-cases/mdx-stanza.t/package/dune | 10 ++++++ .../mdx-stanza.t/package/dune-project | 2 ++ .../test-cases/mdx-stanza.t/run.t | 36 +++++++++++++++++++ 18 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/a.opam create mode 100644 test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/b.opam create mode 100644 test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/doc-a.md create mode 100644 test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/doc-b.md create mode 100644 test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/doc-nopkg.md create mode 100644 test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/dune create mode 100644 test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/dune-project create mode 100644 test/blackbox-tests/test-cases/mdx-stanza.t/package/a.opam create mode 100644 test/blackbox-tests/test-cases/mdx-stanza.t/package/b.opam create mode 100644 test/blackbox-tests/test-cases/mdx-stanza.t/package/doc-a.md create mode 100644 test/blackbox-tests/test-cases/mdx-stanza.t/package/doc-b.md create mode 100644 test/blackbox-tests/test-cases/mdx-stanza.t/package/doc-nopkg.md create mode 100644 test/blackbox-tests/test-cases/mdx-stanza.t/package/dune create mode 100644 test/blackbox-tests/test-cases/mdx-stanza.t/package/dune-project diff --git a/CHANGES.md b/CHANGES.md index 5706daf43e4..e346a6d9d36 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,8 @@ unreleased - 2.9 branch - Allow to set up merlin in a variant of the default context (#4145, @TheLortex, @voodoos) +- Add `(package ...)` to `(mdx ...)` (#4691, fixes #3756, @emillon) + 2.8.5 (28/03/2021) ------------------ diff --git a/doc/dune-files.rst b/doc/dune-files.rst index 5edf1314cf7..ffb34dfde5d 100644 --- a/doc/dune-files.rst +++ b/doc/dune-files.rst @@ -1819,6 +1819,15 @@ Where ```` are: See `MDX's documentation `__ for more details on preludes. +- ``(enabled_if )`` is the same as the corresponding field of + `library`_. + +- ``(package )`` specifies which package to attach this stanza to + (similarly to when ``(package)`` is attached to a ``(rule)`` stanza. When + ``-p`` is passed, ``(mdx)`` stanzas with an other package will be ignored. + Note that this is feature is completely separate from ``(packages)``, which + specifies some dependencies. + .. _plugin: plugin (since 2.8) diff --git a/src/dune_rules/mdx.ml b/src/dune_rules/mdx.ml index f12d8f4c1f6..e6772381fdb 100644 --- a/src/dune_rules/mdx.ml +++ b/src/dune_rules/mdx.ml @@ -124,6 +124,7 @@ type t = ; packages : (Loc.t * Package.Name.t) list ; preludes : Prelude.t list ; enabled_if : Blang.t + ; package : Package.t option } let enabled_if t = t.enabled_if @@ -150,8 +151,11 @@ let decode = and+ preludes = field ~default:[] "preludes" (repeat Prelude.decode) and+ enabled_if = Enabled_if.decode ~allowed_vars:Any ~since:(Some (2, 9)) () + and+ package = + Stanza_common.Pkg.field_opt () + ~check:(Dune_lang.Syntax.since Stanza.syntax (2, 9)) in - { loc; files; packages; preludes; enabled_if }) + { loc; files; packages; preludes; enabled_if; package }) let () = let open Dune_lang.Decoder in @@ -214,10 +218,21 @@ let gen_rules_for_single_file stanza ~sctx ~dir ~expander ~mdx_prog src = (** Generates the rules for a given mdx stanza *) let gen_rules t ~sctx ~dir ~expander = - let files_to_mdx = files_to_mdx t ~sctx ~dir in - let mdx_prog = - Super_context.resolve_program sctx ~dir ~loc:(Some t.loc) - ~hint:"opam install mdx" "ocaml-mdx" + let register_rules () = + let files_to_mdx = files_to_mdx t ~sctx ~dir in + let mdx_prog = + Super_context.resolve_program sctx ~dir ~loc:(Some t.loc) + ~hint:"opam install mdx" "ocaml-mdx" + in + List.iter files_to_mdx + ~f:(gen_rules_for_single_file t ~sctx ~dir ~expander ~mdx_prog) + in + let do_it = + match (!Clflags.only_packages, t.package) with + | None, _ + | Some _, None -> + true + | Some only, Some stanza_package -> + Package.Name.Set.mem only (Package.name stanza_package) in - List.iter files_to_mdx - ~f:(gen_rules_for_single_file t ~sctx ~dir ~expander ~mdx_prog) + if do_it then register_rules () diff --git a/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/a.opam b/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/a.opam new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/b.opam b/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/b.opam new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/doc-a.md b/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/doc-a.md new file mode 100644 index 00000000000..0d72701e564 --- /dev/null +++ b/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/doc-a.md @@ -0,0 +1,3 @@ +```ocaml +# let x = 1;; +``` diff --git a/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/doc-b.md b/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/doc-b.md new file mode 100644 index 00000000000..0d72701e564 --- /dev/null +++ b/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/doc-b.md @@ -0,0 +1,3 @@ +```ocaml +# let x = 1;; +``` diff --git a/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/doc-nopkg.md b/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/doc-nopkg.md new file mode 100644 index 00000000000..0d72701e564 --- /dev/null +++ b/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/doc-nopkg.md @@ -0,0 +1,3 @@ +```ocaml +# let x = 1;; +``` diff --git a/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/dune b/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/dune new file mode 100644 index 00000000000..5e56e55ec03 --- /dev/null +++ b/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/dune @@ -0,0 +1,10 @@ +(mdx + (files doc-a.md) + (package a)) + +(mdx + (files doc-b.md) + (package b)) + +(mdx + (files doc-nopkg.md)) diff --git a/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/dune-project b/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/dune-project new file mode 100644 index 00000000000..7704254d080 --- /dev/null +++ b/test/blackbox-tests/test-cases/mdx-stanza.t/package-old-lang-dune/dune-project @@ -0,0 +1,2 @@ +(lang dune 2.8) +(using mdx 0.1) diff --git a/test/blackbox-tests/test-cases/mdx-stanza.t/package/a.opam b/test/blackbox-tests/test-cases/mdx-stanza.t/package/a.opam new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/blackbox-tests/test-cases/mdx-stanza.t/package/b.opam b/test/blackbox-tests/test-cases/mdx-stanza.t/package/b.opam new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/blackbox-tests/test-cases/mdx-stanza.t/package/doc-a.md b/test/blackbox-tests/test-cases/mdx-stanza.t/package/doc-a.md new file mode 100644 index 00000000000..0d72701e564 --- /dev/null +++ b/test/blackbox-tests/test-cases/mdx-stanza.t/package/doc-a.md @@ -0,0 +1,3 @@ +```ocaml +# let x = 1;; +``` diff --git a/test/blackbox-tests/test-cases/mdx-stanza.t/package/doc-b.md b/test/blackbox-tests/test-cases/mdx-stanza.t/package/doc-b.md new file mode 100644 index 00000000000..0d72701e564 --- /dev/null +++ b/test/blackbox-tests/test-cases/mdx-stanza.t/package/doc-b.md @@ -0,0 +1,3 @@ +```ocaml +# let x = 1;; +``` diff --git a/test/blackbox-tests/test-cases/mdx-stanza.t/package/doc-nopkg.md b/test/blackbox-tests/test-cases/mdx-stanza.t/package/doc-nopkg.md new file mode 100644 index 00000000000..0d72701e564 --- /dev/null +++ b/test/blackbox-tests/test-cases/mdx-stanza.t/package/doc-nopkg.md @@ -0,0 +1,3 @@ +```ocaml +# let x = 1;; +``` diff --git a/test/blackbox-tests/test-cases/mdx-stanza.t/package/dune b/test/blackbox-tests/test-cases/mdx-stanza.t/package/dune new file mode 100644 index 00000000000..5e56e55ec03 --- /dev/null +++ b/test/blackbox-tests/test-cases/mdx-stanza.t/package/dune @@ -0,0 +1,10 @@ +(mdx + (files doc-a.md) + (package a)) + +(mdx + (files doc-b.md) + (package b)) + +(mdx + (files doc-nopkg.md)) diff --git a/test/blackbox-tests/test-cases/mdx-stanza.t/package/dune-project b/test/blackbox-tests/test-cases/mdx-stanza.t/package/dune-project new file mode 100644 index 00000000000..7ff0ffe65bc --- /dev/null +++ b/test/blackbox-tests/test-cases/mdx-stanza.t/package/dune-project @@ -0,0 +1,2 @@ +(lang dune 2.9) +(using mdx 0.1) diff --git a/test/blackbox-tests/test-cases/mdx-stanza.t/run.t b/test/blackbox-tests/test-cases/mdx-stanza.t/run.t index 34f97b4fcf0..358bf3d395c 100644 --- a/test/blackbox-tests/test-cases/mdx-stanza.t/run.t +++ b/test/blackbox-tests/test-cases/mdx-stanza.t/run.t @@ -86,3 +86,39 @@ The mdx stanza supports (enabled_if): Error: 'enabled_if' is only available since version 2.9 of the dune language. Please update your dune-project file to have (lang dune 2.9). [1] + +The mdx stanza supports (package): + + $ dune runtest --root package + Entering directory 'package' + File "doc-a.md", line 1, characters 0-0: + Error: Files _build/default/doc-a.md and + _build/default/.mdx/doc-a.md.corrected differ. + File "doc-b.md", line 1, characters 0-0: + Error: Files _build/default/doc-b.md and + _build/default/.mdx/doc-b.md.corrected differ. + File "doc-nopkg.md", line 1, characters 0-0: + Error: Files _build/default/doc-nopkg.md and + _build/default/.mdx/doc-nopkg.md.corrected differ. + [1] + + $ dune runtest --root package --only-packages b + Entering directory 'package' + File "doc-b.md", line 1, characters 0-0: + Error: Files _build/default/doc-b.md and + _build/default/.mdx/doc-b.md.corrected differ. + File "doc-nopkg.md", line 1, characters 0-0: + Error: Files _build/default/doc-nopkg.md and + _build/default/.mdx/doc-nopkg.md.corrected differ. + [1] + +(package) needs a recent (lang dune): + + $ dune runtest --root package-old-lang-dune --only-packages b + Entering directory 'package-old-lang-dune' + File "dune", line 3, characters 1-12: + 3 | (package a)) + ^^^^^^^^^^^ + Error: 'package' is only available since version 2.9 of the dune language. + Please update your dune-project file to have (lang dune 2.9). + [1]