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

fix: enabled_if with env variable #10936

Merged
merged 5 commits into from
Sep 24, 2024
Merged
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
1 change: 1 addition & 0 deletions doc/changes/10936.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix `enabled_if` when it uses `env` variable. (#10936, fixes #10905, @moyodiallo)
36 changes: 19 additions & 17 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, (3, 15)
| _ -> var, since)
common_vars_list)
;;
Expand All @@ -53,24 +55,24 @@ let decode_value ~allowed_vars ?(is_error = true) () =
Blang.Ast.decode
~override_decode_bare_literal:None
(String_with_vars.decode_manually (fun env var ->
match Dune_lang.Template.Pform.payload var with
| Some _ ->
let name = Dune_lang.Template.Pform.name var in
if List.exists ~f:(String.equal name) common_vars_list
then (
match List.assoc allowed_vars name with
| None ->
emit_warning allowed_vars is_error var;
Pform.Env.parse env var
| Some min_ver ->
let current_ver = Pform.Env.syntax_version env in
if min_ver > current_ver
then (
let loc = Dune_lang.Template.Pform.loc var in
let what = Dune_lang.Template.Pform.describe var in
Dune_lang.Syntax.Error.since loc Stanza.syntax min_ver ~what)
else Pform.Env.unsafe_parse_without_checking_version env var)
else (
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;
Pform.Env.parse env var
| Some min_ver ->
let current_ver = Pform.Env.syntax_version env in
if min_ver > current_ver
then (
let loc = Dune_lang.Template.Pform.loc var in
let what = Dune_lang.Template.Pform.describe var in
Dune_lang.Syntax.Error.since loc Stanza.syntax min_ver ~what)
else Pform.Env.unsafe_parse_without_checking_version env var)))
Pform.Env.parse env var)))
;;

let decode ~allowed_vars ?is_error ~since () =
Expand Down
52 changes: 52 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,52 @@
Test enabled_if with 'env' variable.

$ cat > dune-project <<EOF
> (lang dune 3.14)
> (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

$ MYVAR=disabled dune exec -- dune_test
File "dune", line 10, characters 24-45:
10 | (enabled_if (= enabled %{env:MYVAR=disabled}))
^^^^^^^^^^^^^^^^^^^^^
Error: %{env:..} is only available since version 3.15 of the dune language.
Please update your dune-project file to have (lang dune 3.15).
[1]

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

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

$ MYVAR=disabled dune exec -- dune_test
Leonidas-from-XIV marked this conversation as resolved.
Show resolved Hide resolved
Hello world
$ MYVAR=enabled dune exec -- dune_test
File "dune", line 11, characters 10-16:
11 | (modules main_2)
^^^^^^
Error: Module Main_2 doesn't exist.
[1]
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]
Loading