Skip to content

Commit

Permalink
feat: introduce env_vars alias for env-vars
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne Marais <[email protected]>
  • Loading branch information
maiste committed Feb 4, 2025
1 parent 26d680d commit dde98f9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
5 changes: 5 additions & 0 deletions doc/reference/dune/env.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ Fields supported in ``<settings>`` are:
variables to the environment where the build commands are executed and are
used by ``dune exec``.

- ``(env_vars (<var1> <val1>) .. (<varN> <valN>))`` is an alias for ``env-vars``.
It was introduced to stick to the underscore naming convention.

.. versionadded:: 3.18

- ``(menhir_flags <flags>))`` specifies flags for Menhir stanzas. This flag was
replaced by the ``(menhir)`` field (see below) starting in version 3.0 of the
Menhir extension.
Expand Down
29 changes: 19 additions & 10 deletions src/dune_rules/dune_env.ml
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,25 @@ let inline_tests_field =
;;

let env_vars_field =
field
"env-vars"
~default:Env.empty
(Dune_lang.Syntax.since Stanza.syntax (1, 5)
>>> located (repeat (pair string string))
>>| fun (loc, pairs) ->
match Env.Map.of_list pairs with
| Ok vars -> Env.extend Env.empty ~vars
| Error (k, _, _) ->
User_error.raise ~loc [ Pp.textf "Variable %s is specified several times" k ])
let env_decoder =
located (repeat (pair string string))
>>| fun (loc, pairs) ->
match Env.Map.of_list pairs with
| Ok vars -> Env.extend Env.empty ~vars
| Error (k, _, _) ->
User_error.raise ~loc [ Pp.textf "Variable %s is specified several times" k ]
in
let+ env_vars_legacy =
field_o "env-vars" (Dune_lang.Syntax.since Stanza.syntax (1, 5) >>> env_decoder)
and+ env_vars =
field_o "env_vars" (Dune_lang.Syntax.since Stanza.syntax (3, 18) >>> env_decoder)
in
match env_vars_legacy, env_vars with
| None, None -> Env.empty
| Some env, None -> env
| None, Some env -> env
| Some _, Some _ ->
Code_error.raise "(env_vars) and (env-vars) are aliases and cannot both be present" []
;;

let odoc_field =
Expand Down
19 changes: 19 additions & 0 deletions test/blackbox-tests/test-cases/env/env_vars_alias.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Since Dune 3.18, we support `env_vars` as an alias for `env-vars` for naming
consistency.

$ cat > dune-project << EOF
> (lang dune 3.18)
> EOF

We check the rule is evaluated:

$ cat > dune << 'EOF'
> (env
> (_ (env_vars (VAR default ))))
> (rule
> (alias runtest)
> (action (system "echo $VAR")))
> EOF

$ dune runtest
default

0 comments on commit dde98f9

Please sign in to comment.