Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce env_vars alias for env-vars #11442

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading