Skip to content

Commit 1488dc7

Browse files
gridbugsrjbou
authored andcommitted
Update PATH in place in env hook
Prior to this change, the default behaviour of `opam init` was to register a shell hook which ran before each command, and added/moved the bin dir from the current opam switch to the beginning of the PATH variable. If a user has a custom bin directory at the beginning of their PATH containing an executable with the same name as an executable in their current opam switch, the executable in the opam switch will take precedence. This commit changes the behaviour of the shell hook to update the existing opam switch in PATH in place rather than prepending it, so the precedence of the opam switch relative to other dirs in PATH is preserved. This change doesn't affect the behaviour of running `eval $(opam env)`. Fixes #6815 Signed-off-by: Stephen Sherratt <stephen@sherra.tt>
1 parent 567ba2d commit 1488dc7

7 files changed

Lines changed: 13 additions & 12 deletions

File tree

master_changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ users)
151151

152152
## Shell
153153
* added return 0 for zsh/bash/sh/fish to prevent leaking error codes [#6930 @ta2005]
154+
* Update PATH in place in env hook [#6859 @gridbugs fix #6815]
154155

155156
## Internal
156157
* Improve cache-loading performance when using OCaml >= 5.4 by using `Gc.ramp_up` [#6515 @dra27]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
alias precmd 'eval `opam env --shell=csh --readonly`'
1+
alias precmd 'eval `opam env --shell=csh --readonly --inplace-path`'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
function __opam_env_export_eval --on-event fish_prompt
2-
eval (opam env --shell=fish --readonly 2> /dev/null)
2+
eval (opam env --shell=fish --readonly --inplace-path 2> /dev/null)
33
end

src/state/shellscripts/env_hook.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
_opam_env_hook() {
22
local previous_exit_status=$?;
3-
eval $(opam env --shell=bash --readonly 2> /dev/null <&- );
3+
eval $(opam env --shell=bash --readonly --inplace-path 2> /dev/null <&- );
44
return $previous_exit_status;
55
};
66
if ! [[ "$PROMPT_COMMAND" =~ _opam_env_hook ]]; then

src/state/shellscripts/env_hook.zsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
_opam_env_hook() {
2-
eval $(opam env --shell=zsh --readonly 2> /dev/null <&-);
2+
eval $(opam env --shell=zsh --readonly --inplace-path 2> /dev/null <&-);
33
}
44
typeset -ag precmd_functions;
55
if [[ -z ${precmd_functions[(r)_opam_env_hook]} ]]; then

tests/reftests/init-scripts.unix.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ $env:PATH='${BASEDIR}/root/fake/bin:' + "$env:PATH"
100100
OPAMNOENVNOTICE=true; export OPAMNOENVNOTICE;
101101
_opam_env_hook() {
102102
local previous_exit_status=$?;
103-
eval $(opam env --shell=bash --readonly 2> /dev/null <&- );
103+
eval $(opam env --shell=bash --readonly --inplace-path 2> /dev/null <&- );
104104
return $previous_exit_status;
105105
};
106106
if ! [[ "$PROMPT_COMMAND" =~ _opam_env_hook ]]; then
@@ -109,7 +109,7 @@ fi
109109
### cat root/opam-init/env_hook.zsh
110110
OPAMNOENVNOTICE=true; export OPAMNOENVNOTICE;
111111
_opam_env_hook() {
112-
eval $(opam env --shell=zsh --readonly 2> /dev/null <&-);
112+
eval $(opam env --shell=zsh --readonly --inplace-path 2> /dev/null <&-);
113113
}
114114
typeset -ag precmd_functions;
115115
if [[ -z ${precmd_functions[(r)_opam_env_hook]} ]]; then
@@ -118,11 +118,11 @@ fi
118118
### cat root/opam-init/env_hook.fish
119119
set -gx OPAMNOENVNOTICE true;
120120
function __opam_env_export_eval --on-event fish_prompt
121-
eval (opam env --shell=fish --readonly 2> /dev/null)
121+
eval (opam env --shell=fish --readonly --inplace-path 2> /dev/null)
122122
end
123123
### cat root/opam-init/env_hook.csh
124124
setenv OPAMNOENVNOTICE true
125-
alias precmd 'eval `opam env --shell=csh --readonly`'
125+
alias precmd 'eval `opam env --shell=csh --readonly --inplace-path`'
126126
### test -f root/opam-init/env_hook.cmd
127127
# Return code 1 #
128128
### test -f root/opam-init/env_hook.ps1

tests/reftests/init-scripts.win32.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ $env:PATH='${BASEDIR}/root/fake/bin;' + "$env:PATH"
9595
OPAMNOENVNOTICE=true; export OPAMNOENVNOTICE;
9696
_opam_env_hook() {
9797
local previous_exit_status=$?;
98-
eval $(opam env --shell=bash --readonly 2> /dev/null <&- );
98+
eval $(opam env --shell=bash --readonly --inplace-path 2> /dev/null <&- );
9999
return $previous_exit_status;
100100
};
101101
if ! [[ "$PROMPT_COMMAND" =~ _opam_env_hook ]]; then
@@ -104,7 +104,7 @@ fi
104104
### cat root/opam-init/env_hook.zsh
105105
OPAMNOENVNOTICE=true; export OPAMNOENVNOTICE;
106106
_opam_env_hook() {
107-
eval $(opam env --shell=zsh --readonly 2> /dev/null <&-);
107+
eval $(opam env --shell=zsh --readonly --inplace-path 2> /dev/null <&-);
108108
}
109109
typeset -ag precmd_functions;
110110
if [[ -z ${precmd_functions[(r)_opam_env_hook]} ]]; then
@@ -113,11 +113,11 @@ fi
113113
### cat root/opam-init/env_hook.fish
114114
set -gx OPAMNOENVNOTICE true;
115115
function __opam_env_export_eval --on-event fish_prompt
116-
eval (opam env --shell=fish --readonly 2> /dev/null)
116+
eval (opam env --shell=fish --readonly --inplace-path 2> /dev/null)
117117
end
118118
### cat root/opam-init/env_hook.csh
119119
setenv OPAMNOENVNOTICE true
120-
alias precmd 'eval `opam env --shell=csh --readonly`'
120+
alias precmd 'eval `opam env --shell=csh --readonly --inplace-path`'
121121
### test -f root/opam-init/env_hook.cmd
122122
# Return code 1 #
123123
### test -f root/opam-init/env_hook.ps1

0 commit comments

Comments
 (0)