Skip to content

Commit 57cdaa2

Browse files
committed
Make variables.* back off if already set
1 parent a759985 commit 57cdaa2

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/state/opamEnv.ml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,14 @@ let env_hook_script shell =
10301030
^ script)
10311031
(env_hook_script_base shell)
10321032

1033+
let abort_if_set var shell =
1034+
match shell with
1035+
| SH_zsh | SH_bash | SH_sh -> Printf.sprintf "test -z \"$%s\" || return\n" var
1036+
| SH_csh -> Printf.sprintf "if ( ${?%s} ) then\n if ( \"$%s\" != \"\") exit\nendif\n" var var
1037+
| SH_pwsh _ -> Printf.sprintf "if ($env:%s) { return }\n" var
1038+
| SH_fish -> Printf.sprintf "test -n \"$%s\"; and return\n" var
1039+
| SH_cmd -> Printf.sprintf "if defined %s goto :EOF\n" var
1040+
10331041
let source root shell f =
10341042
let fname = OpamFilename.to_string (OpamPath.init root // f) in
10351043
let unix_transform ?using_backslashes () =
@@ -1223,7 +1231,7 @@ let write_dynamic_init_scripts st =
12231231
List.iter
12241232
(fun shell ->
12251233
write_script (OpamPath.init st.switch_global.root)
1226-
(variables_file shell, string_of_update st shell updates))
1234+
(variables_file shell, abort_if_set "OPAM_SWITCH_PREFIX" shell ^ string_of_update st shell updates))
12271235
[SH_sh; SH_csh; SH_fish; SH_pwsh Powershell; SH_cmd]
12281236
with OpamSystem.Locked ->
12291237
OpamConsole.warning

tests/reftests/init-scripts.unix.test

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ User configuration:
1818
### # we need the switch for variables file
1919
### opam switch create fake --empty --root root
2020
### sh locally-in-sh.sh sh -lc "sh -lc env" | grep MANPATH
21-
MANPATH=:${BASEDIR}/root/fake/man:${BASEDIR}/root/fake/man
21+
MANPATH=:${BASEDIR}/root/fake/man
2222
### ls root/opam-init | unordered
2323
complete.sh
2424
complete.zsh
@@ -73,29 +73,36 @@ if exist "${BASEDIR}/root/opam-init/variables.cmd" call "${BASEDIR}/root/opam-in
7373
if Test-Path "${BASEDIR}/root/opam-init/variables.ps1" { . "${BASEDIR}/root/opam-init/variables.ps1" *> $null }
7474
### : Variables scripts :
7575
### cat root/opam-init/variables.sh | grep -v man | grep -v MANPATH
76+
test -z "$OPAM_SWITCH_PREFIX" || return
7677
# Prefix of the current opam switch
7778
OPAM_SWITCH_PREFIX='${BASEDIR}/root/fake'; export OPAM_SWITCH_PREFIX;
7879
# Binary dir for opam switch fake
7980
PATH='${BASEDIR}/root/fake/bin':"$PATH"; export PATH;
8081
### test -f root/opam-init/variables.zsh
8182
# Return code 1 #
8283
### cat root/opam-init/variables.fish | grep -v man | grep -v MANPATH
84+
test -n "$OPAM_SWITCH_PREFIX"; and return
8385
# Prefix of the current opam switch
8486
set -gx OPAM_SWITCH_PREFIX '${BASEDIR}/root/fake';
8587
# Binary dir for opam switch fake
8688
set -gx PATH '${BASEDIR}/root/fake/bin' $PATH;
8789
### cat root/opam-init/variables.csh | grep -v man | grep -v MANPATH
90+
if ( ${?OPAM_SWITCH_PREFIX} ) then
91+
if ( "$OPAM_SWITCH_PREFIX" != "") exit
92+
endif
8893
# Prefix of the current opam switch
8994
setenv OPAM_SWITCH_PREFIX '${BASEDIR}/root/fake'
9095
# Binary dir for opam switch fake
9196
if ( ! ${?PATH} ) setenv PATH ""
9297
setenv PATH '${BASEDIR}/root/fake/bin':"$PATH"
9398
### cat root/opam-init/variables.cmd | grep -v man | grep -v MANPATH
99+
if defined OPAM_SWITCH_PREFIX goto :EOF
94100
:: Prefix of the current opam switch
95101
set "OPAM_SWITCH_PREFIX=${BASEDIR}/root/fake"
96102
:: Binary dir for opam switch fake
97103
set "PATH=${BASEDIR}/root/fake/bin:%PATH%"
98104
### cat root/opam-init/variables.ps1 | grep -v man | grep -v MANPATH
105+
if ($env:OPAM_SWITCH_PREFIX) { return }
99106
# Prefix of the current opam switch
100107
$env:OPAM_SWITCH_PREFIX='${BASEDIR}/root/fake'
101108
# Binary dir for opam switch fake

tests/reftests/init-scripts.win32.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ if exist "${BASEDIR}/root/opam-init/variables.cmd" call "${BASEDIR}/root/opam-in
6060
if Test-Path "${BASEDIR}/root/opam-init/variables.ps1" { . "${BASEDIR}/root/opam-init/variables.ps1" *> $null }
6161
### : Variables scripts :
6262
### cat root/opam-init/variables.sh | grep -v man | grep -v MANPATH
63+
test -z "$OPAM_SWITCH_PREFIX" || return
6364
# Prefix of the current opam switch
6465
OPAM_SWITCH_PREFIX='${BASEDIR}/root/fake'; export OPAM_SWITCH_PREFIX;
6566
# Binary dir for opam switch fake
@@ -69,17 +70,22 @@ PATH='${BASEDIR}/root/fake/bin':"$PATH"; export PATH;
6970
### # ERROR, broken, see opam#5854 testing only its existence
7071
### test -f root/opam-init/variables.fish
7172
### cat root/opam-init/variables.csh | grep -v man | grep -v MANPATH
73+
if ( ${?OPAM_SWITCH_PREFIX} ) then
74+
if ( "$OPAM_SWITCH_PREFIX" != "") exit
75+
endif
7276
# Prefix of the current opam switch
7377
setenv OPAM_SWITCH_PREFIX '${BASEDIR}/root/fake'
7478
# Binary dir for opam switch fake
7579
if ( ! ${?PATH} ) setenv PATH ""
7680
setenv PATH '${BASEDIR}/root/fake/bin':"$PATH"
7781
### cat root/opam-init/variables.cmd | grep -v man | grep -v MANPATH
82+
if defined OPAM_SWITCH_PREFIX goto :EOF
7883
:: Prefix of the current opam switch
7984
set "OPAM_SWITCH_PREFIX=${BASEDIR}/root/fake"
8085
:: Binary dir for opam switch fake
8186
set "PATH=${BASEDIR}/root/fake/bin;%PATH%"
8287
### cat root/opam-init/variables.ps1 | grep -v man | grep -v MANPATH
88+
if ($env:OPAM_SWITCH_PREFIX) { return }
8389
# Prefix of the current opam switch
8490
$env:OPAM_SWITCH_PREFIX='${BASEDIR}/root/fake'
8591
# Binary dir for opam switch fake

0 commit comments

Comments
 (0)