Skip to content

Commit 26f7462

Browse files
authored
Merge pull request #4904 from rjbou/var-shadow
Warn when setting a variable that shadow an option
2 parents 86f2dbd + 9bf7193 commit 26f7462

File tree

3 files changed

+86
-20
lines changed

3 files changed

+86
-20
lines changed

master_changes.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ users)
6262

6363
## Var/Option
6464
* Fix the value of the 'arch' variable when the current OS is 32bit on a 64bit machine [#5950 @kit-ty-kate - fix #5949]
65+
* Warn when setting a variable if an option is shadowed [#4904 @rjbou - fix #4730]
6566

6667
## Update / Upgrade
6768

src/client/opamConfigCommand.ml

+13
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,19 @@ type ('var,'config) var_confset =
936936
}
937937

938938
let set_var var value conf =
939+
let svar = OpamVariable.Full.to_string var in
940+
let global_option, switch_option =
941+
let exists f = List.exists (fun (name, _) -> String.equal svar name) f in
942+
(exists OpamFile.Config.fields),
943+
(exists OpamFile.Switch_config.fields)
944+
in
945+
if global_option || switch_option then
946+
(let scope = if global_option then "global" else "switch" in
947+
OpamConsole.warning
948+
"You are setting a variable that has the same name as a %s option.\n\
949+
Did you mean to use `opam option` instead? You can revert this variable \
950+
using: 'opam var %s= --%s'\n"
951+
scope svar scope);
939952
let conf = conf (OpamVariable.Full.variable var) in
940953
let global_vars = conf.stv_vars in
941954
let rest = List.filter (fun v -> not (conf.stv_find v)) global_vars in

tests/reftests/var-option.test

+72-20
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@ Removed variable group in switch var-option
1717
### opam option sys-pkg-manager-cmd= --global | grep -v configuration
1818
### opam var arch=x86_64 --global
1919
Added '[arch "x86_64" "Set through 'opam var'"]' to field global-variables in global configuration
20-
### opam var jobs=7 --global
21-
Added '[jobs "7" "Set through 'opam var'"]' to field global-variables in global configuration
2220
### opam var make=make --global
2321
Added '[make "make" "Set through 'opam var'"]' to field global-variables in global configuration
24-
### opam var opam-version=68.79 --global
25-
Added '[opam-version "68.79" "Set through 'opam var'"]' to field global-variables in global configuration
2622
### opam var os=linux --global
2723
Added '[os "linux" "Set through 'opam var'"]' to field global-variables in global configuration
2824
### opam var os-distribution=lorem --global
@@ -45,6 +41,8 @@ Set to '[]' the field wrap-build-commands in global configuration
4541
Set to '[]' the field wrap-install-commands in global configuration
4642
### opam option wrap-remove-commands=[] --global
4743
Set to '[]' the field wrap-remove-commands in global configuration
44+
### opam option jobs=7 --global
45+
Set to '7' the field jobs in global configuration
4846
### opam install i-got-the-variables --yes
4947
The following actions will be performed:
5048
=== install 2 packages
@@ -55,14 +53,13 @@ The following actions will be performed:
5553
-> installed base.2
5654
-> installed i-got-the-variables.2.4.6
5755
Done.
58-
### opam var | " *" -> " " | "[.]exe " -> ""
56+
### opam var | " *" -> " " | "[.]exe " -> "" | "opam-version.*" -> '\c'
5957

6058
<><> Global opam variables ><><><><><><><><><><><><><><><><><><><><><><><><><><>
6159
arch x86_64 # Set through 'opam var'
6260
exe # Suffix needed for executable filenames (Windows)
63-
jobs 7 # Set through 'opam var'
61+
jobs 7 # The number of parallel jobs set up in opam configuration
6462
make make # Set through 'opam var'
65-
opam-version 68.79 # Set through 'opam var'
6663
os linux # Set through 'opam var'
6764
os-distribution lorem # Set through 'opam var'
6865
os-family ipsum # Set through 'opam var'
@@ -113,12 +110,11 @@ etc ${BASEDIR}/OPAM/var-option/etc
113110
man ${BASEDIR}/OPAM/var-option/man
114111
toplevel ${BASEDIR}/OPAM/var-option/lib/toplevel
115112
stublibs ${BASEDIR}/OPAM/var-option/lib/stublibs
116-
### opam var --global | " *" -> " " | "[.]exe " -> ""
113+
### opam var --global | " *" -> " " | "[.]exe " -> "" | "opam-version.*" -> '\c'
117114
arch x86_64 # Set through 'opam var'
118115
exe # Suffix needed for executable filenames (Windows)
119-
jobs 7 # Set through 'opam var'
116+
jobs 7 # The number of parallel jobs set up in opam configuration
120117
make make # Set through 'opam var'
121-
opam-version 68.79 # Set through 'opam var'
122118
os linux # Set through 'opam var'
123119
os-distribution lorem # Set through 'opam var'
124120
os-family ipsum # Set through 'opam var'
@@ -231,7 +227,7 @@ depext-run-installs true
231227
download-command {}
232228
download-jobs 3
233229
git-location {}
234-
jobs {}
230+
jobs 7
235231
post-build-commands {}
236232
post-install-commands {}
237233
post-remove-commands {}
@@ -275,7 +271,9 @@ wrap-remove-commands {}
275271
[ERROR] Field or section download-jobs not found
276272
# Return code 5 #
277273
### opam option jobs
274+
7
278275
### opam option jobs --global
276+
7
279277
### opam option jobs --switch var-option
280278
[ERROR] Field or section jobs not found
281279
# Return code 5 #
@@ -488,7 +486,7 @@ Reverted field archive-mirrors in global configuration
488486
### opam option archive-mirrors
489487
### # Check that eval-variable is removed when a global variable is removed
490488
### opam option global-variables
491-
[[foo "global-foo" "Set through 'opam var'"] [bin "global-bin" "Set through 'opam var'"] [os-version "dolor" "Set through 'opam var'"] [os-family "ipsum" "Set through 'opam var'"] [os-distribution "lorem" "Set through 'opam var'"] [os "linux" "Set through 'opam var'"] [opam-version "68.79" "Set through 'opam var'"] [make "make" "Set through 'opam var'"] [jobs "7" "Set through 'opam var'"] [arch "x86_64" "Set through 'opam var'"] [sys-ocaml-version "4.08.0" "Set through 'opam var'"]]
489+
[[foo "global-foo" "Set through 'opam var'"] [bin "global-bin" "Set through 'opam var'"] [os-version "dolor" "Set through 'opam var'"] [os-family "ipsum" "Set through 'opam var'"] [os-distribution "lorem" "Set through 'opam var'"] [os "linux" "Set through 'opam var'"] [make "make" "Set through 'opam var'"] [arch "x86_64" "Set through 'opam var'"] [sys-ocaml-version "4.08.0" "Set through 'opam var'"]]
492490
### opam option 'eval-variables=[dolore ["mania"] "alica"]'
493491
Set to '[dolore ["mania"] "alica"]' the field eval-variables in global configuration
494492
### opam option eval-variables
@@ -500,8 +498,8 @@ Removed variable dolore in global configuration
500498
### opam option eval-variables
501499
[]
502500
### opam option global-variables
503-
[[foo "global-foo" "Set through 'opam var'"] [bin "global-bin" "Set through 'opam var'"] [os-version "dolor" "Set through 'opam var'"] [os-family "ipsum" "Set through 'opam var'"] [os-distribution "lorem" "Set through 'opam var'"] [os "linux" "Set through 'opam var'"] [opam-version "68.79" "Set through 'opam var'"] [make "make" "Set through 'opam var'"] [jobs "7" "Set through 'opam var'"] [arch "x86_64" "Set through 'opam var'"] [sys-ocaml-version "4.08.0" "Set through 'opam var'"]]
504-
### # Check uniable operations
501+
[[foo "global-foo" "Set through 'opam var'"] [bin "global-bin" "Set through 'opam var'"] [os-version "dolor" "Set through 'opam var'"] [os-family "ipsum" "Set through 'opam var'"] [os-distribution "lorem" "Set through 'opam var'"] [os "linux" "Set through 'opam var'"] [make "make" "Set through 'opam var'"] [arch "x86_64" "Set through 'opam var'"] [sys-ocaml-version "4.08.0" "Set through 'opam var'"]]
502+
### # Check disallowed operations
505503
### opam option 'variables+={esse: "cillum"}'
506504
[ERROR] Field variables can't be directly appended to, use `opam var` instead
507505
# Return code 2 #
@@ -534,10 +532,66 @@ jobs download-command download-jobs archive-mirrors solver-criteria solver-upgra
534532
[ERROR] There is no option named 'bar'. The allowed options are:
535533
synopsis setenv depext-bypass pre-build-commands pre-install-commands pre-remove-commands pre-session-commands wrap-build-commands wrap-install-commands wrap-remove-commands post-build-commands post-install-commands post-remove-commands post-session-commands variables
536534
# Return code 2 #
535+
### # Check option shadowing
536+
### opam option download-jobs
537+
1
538+
### opam var download-jobs
539+
[ERROR] Variable download-jobs not found in the global configuration
540+
# Return code 5 #
541+
### opam var download-jobs=42 --global
542+
[WARNING] You are setting a variable that has the same name as a global option.
543+
Did you mean to use `opam option` instead? You can revert this variable using: 'opam var download-jobs= --global'
544+
545+
Added '[download-jobs "42" "Set through 'opam var'"]' to field global-variables in global configuration
546+
### opam option download-jobs --global
547+
1
548+
### opam var download-jobs --global
549+
42
550+
### opam var download-jobs=44 --switch var-option
551+
[WARNING] You are setting a variable that has the same name as a global option.
552+
Did you mean to use `opam option` instead? You can revert this variable using: 'opam var download-jobs= --global'
553+
554+
Added 'download-jobs: "44"' to field variables in switch var-option
555+
### opam var download-jobs --switch var-option
556+
44
557+
### opam var synopsis --global
558+
[ERROR] Variable synopsis not found in the global configuration
559+
# Return code 5 #
560+
### opam var 'synopsis="lorem ipsum"' --global
561+
[WARNING] You are setting a variable that has the same name as a switch option.
562+
Did you mean to use `opam option` instead? You can revert this variable using: 'opam var synopsis= --switch'
563+
564+
Added '[synopsis "\"lorem ipsum\"" "Set through 'opam var'"]' to field global-variables in global configuration
565+
### opam var synopsis --global
566+
"lorem ipsum"
567+
### opam option synopsis --switch var-option
568+
""
569+
### opam var synopsis --switch var-option
570+
[ERROR] Variable synopsis not found in switch var-option
571+
# Return code 5 #
572+
### opam var 'synopsis="ipsum"' --switch var-option
573+
[WARNING] You are setting a variable that has the same name as a switch option.
574+
Did you mean to use `opam option` instead? You can revert this variable using: 'opam var synopsis= --switch'
575+
576+
Added 'synopsis: "\"ipsum\""' to field variables in switch var-option
577+
### opam option synopsis --switch var-option
578+
""
579+
### opam var synopsis --switch var-option
580+
"ipsum"
581+
### opam var synopsis= --global -y
582+
[WARNING] You are setting a variable that has the same name as a switch option.
583+
Did you mean to use `opam option` instead? You can revert this variable using: 'opam var synopsis= --switch'
584+
585+
Removed variable synopsis in global configuration
586+
### opam var download-jobs= --global -y
587+
[WARNING] You are setting a variable that has the same name as a global option.
588+
Did you mean to use `opam option` instead? You can revert this variable using: 'opam var download-jobs= --global'
589+
590+
Removed variable download-jobs in global configuration
537591
### : when no switch is present
538592
### opam switch remove var-option -y
539593
Switch var-option and all its packages will be wiped. Are you sure? [y/n] y
540-
### opam var | ' +#.*' -> '' | " +[.]exe" -> ""
594+
### opam var | ' +#.*' -> '' | " +[.]exe" -> "" | "opam-version.*" -> '\c'
541595

542596
<><> Global opam variables ><><><><><><><><><><><><><><><><><><><><><><><><><><>
543597
arch x86_64
@@ -546,7 +600,6 @@ exe
546600
foo global-foo
547601
jobs 7
548602
make make
549-
opam-version 68.79
550603
os linux
551604
os-distribution lorem
552605
os-family ipsum
@@ -577,14 +630,13 @@ PKG:hash
577630
PKG:dev
578631
PKG:build-id
579632
PKG:opamfile
580-
### opam var --global | ' +#.*' -> '' | " +[.]exe" -> ""
633+
### opam var --global | ' +#.*' -> '' | " +[.]exe" -> "" | "opam-version.*" -> '\c'
581634
arch x86_64
582635
bin global-bin
583636
exe
584637
foo global-foo
585638
jobs 7
586639
make make
587-
opam-version 68.79
588640
os linux
589641
os-distribution lorem
590642
os-family ipsum
@@ -615,7 +667,7 @@ depext-run-installs true
615667
download-command {}
616668
download-jobs 1
617669
git-location {}
618-
jobs {}
670+
jobs 7
619671
post-build-commands {}
620672
post-install-commands {}
621673
post-remove-commands {}
@@ -647,7 +699,7 @@ depext-run-installs true
647699
download-command {}
648700
download-jobs 1
649701
git-location {}
650-
jobs {}
702+
jobs 7
651703
post-build-commands {}
652704
post-install-commands {}
653705
post-remove-commands {}

0 commit comments

Comments
 (0)