Skip to content

Commit

Permalink
fix: enabled_if with env variable
Browse files Browse the repository at this point in the history
Signed-off-by: Alpha DIALLO <moyodiallo@gmail.com>
  • Loading branch information
moyodiallo committed Sep 19, 2024
1 parent 3f31387 commit 481f810
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/dune_rules/enabled_if.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let common_vars_list =
; "ocaml_version"
; "context_name"
; "arch_sixtyfour"
; "env"
]
;;

Expand All @@ -29,6 +30,7 @@ let common_vars ~since =
match var with
| "context_name" -> var, (2, 7)
| "arch_sixtyfour" -> var, (3, 11)
| "env" -> var, (1, 14)
| _ -> var, since)
common_vars_list)
;;
Expand All @@ -53,12 +55,12 @@ let decode_value ~allowed_vars ?(is_error = true) () =
Blang.Ast.decode
~override_decode_bare_literal:None
(String_with_vars.decode_manually (fun env var ->
let name = Dune_lang.Template.Pform.name var in
match Dune_lang.Template.Pform.payload var with
| Some _ ->
| _ when not @@ List.exists ~f:(String.equal name) common_vars_list ->
emit_warning allowed_vars is_error var;
Pform.Env.parse env var
| None ->
let name = Dune_lang.Template.Pform.name var in
| _ ->
(match List.assoc allowed_vars name with
| None ->
emit_warning allowed_vars is_error var;
Expand Down
30 changes: 30 additions & 0 deletions test/blackbox-tests/test-cases/enabled_if/eif-env-vars.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

$ cat > dune-project <<EOF
> (lang dune 3.16)
> (name dune-test)
> (package
> (name dune-test))
> EOF

$ cat > dune <<EOF
> (executable
> (name main)
> (public_name dune_test)
> (modules main)
> (package dune-test)
> (modes exe))
> (executable
> (name main_2)
> (public_name dune_test_2)
> (enabled_if (= enabled %{env:MYVAR=disabled}))
> (modules main_2)
> (package dune-test)
> (modes exe))
> EOF

$ cat > main.ml <<EOF
> let () = print_string "Hello world"
> EOF

$ MYVAR=disabled dune exec -- dune_test
Hello world
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ The next ones use forbidden variables For dune 2.3 -> 2.5 it is a warning
3 | (enabled_if (<> %{project_root} "")))
^^^^^^^^^^^^^^^
Warning: Only architecture, system, model, os_type, ccomp_type, profile,
ocaml_version, context_name and arch_sixtyfour variables are allowed in this
'enabled_if' field. Please upgrade your dune language to at least 3.15.
ocaml_version, context_name, arch_sixtyfour and env variables are allowed in
this 'enabled_if' field. Please upgrade your dune language to at least 3.15.
bar

For dune >= 2.6 it is an error
Expand All @@ -21,6 +21,6 @@ For dune >= 2.6 it is an error
3 | (enabled_if (<> %{project_root} "")))
^^^^^^^^^^^^^^^
Error: Only architecture, system, model, os_type, ccomp_type, profile,
ocaml_version, context_name and arch_sixtyfour variables are allowed in this
'enabled_if' field. Please upgrade your dune language to at least 3.15.
ocaml_version, context_name, arch_sixtyfour and env variables are allowed in
this 'enabled_if' field. Please upgrade your dune language to at least 3.15.
[1]
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ Tests for enabled_if in install stanza using forbidden variable.
6 | (enabled_if (= %{project_root} ""))
^^^^^^^^^^^^^^^
Error: Only architecture, system, model, os_type, ccomp_type, profile,
ocaml_version, context_name and arch_sixtyfour variables are allowed in this
'enabled_if' field. Please upgrade your dune language to at least 3.15.
ocaml_version, context_name, arch_sixtyfour and env variables are allowed in
this 'enabled_if' field. Please upgrade your dune language to at least 3.15.
[1]

0 comments on commit 481f810

Please sign in to comment.