Skip to content
Open
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 master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ users)
## Clean

## Env
* Env now sets `XDG_DATA_DIRS` to include the opam share directory and `FPATH` to include share/zsh/site-functions. Tools which install shell autocomplete scripts in these directories will now be picked up automatically. [#6427 @WardBrian - fix #6812]

## Opamfile

Expand Down
58 changes: 40 additions & 18 deletions src/state/opamEnv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ let env_update_resolved_with_default ?comment var =
let rewrite = Some (SPF_Resolved (Some (default_sep_fmt_str var))) in
env_update_resolved ?comment ~rewrite var

let compute_updates ?(force_path=false) st =
let compute_updates ?(force_path=false) ?(shell=None) st =
(* Todo: put these back into their packages!
let perl5 = OpamPackage.Name.of_string "perl5" in
let add_to_perl5lib = OpamPath.Switch.lib t.root t.switch t.switch_config perl5 in
Expand All @@ -640,14 +640,34 @@ let compute_updates ?(force_path=false) st =
st.switch st.switch_config))
~comment:"Current opam switch man dir"
]
in
let switch_env =
(env_update_resolved_with_default "OPAM_SWITCH_PREFIX" Eq
(OpamFilename.Dir.to_string
(OpamPath.Switch.root st.switch_global.root st.switch))
~comment:"Prefix of the current opam switch")
::
List.map (env_expansion st) (OpamFile.Switch_config.env st.switch_config)
in
let share_dir = OpamFilename.Dir.to_string (OpamPath.Switch.share_dir st.switch_global.root st.switch st.switch_config) in
let shell = match shell with
| Some s -> s
| None -> OpamStd.Sys.guess_shell_compat () in
let zsh_fpath =
match shell with
| SH_zsh ->
(* add share/zsh/site-functions to FPATH for function lookup *)
[ env_update_resolved_with_default "FPATH"
EqPlusEq (Filename.concat share_dir (Filename.concat "zsh" "site-functions"))
~comment:"Current opam switch zsh site-functions"
]
| _ -> []
in
let xdg_data_dirs =
(* in particular, this is used by bash-completion, but it is not specific to it *)
[ env_update_resolved_with_default "XDG_DATA_DIRS"
EqPlusEq share_dir
~comment:"Current opam switch share dir" ]
in
let switch_env =
(env_update_resolved_with_default "OPAM_SWITCH_PREFIX" Eq
(OpamFilename.Dir.to_string
(OpamPath.Switch.root st.switch_global.root st.switch))
~comment:"Prefix of the current opam switch")
::
List.map (env_expansion st) (OpamFile.Switch_config.env st.switch_config)
in
let pkg_env = (* XXX: Does this need a (costly) topological sort? *)
let updates =
Expand All @@ -660,7 +680,8 @@ let compute_updates ?(force_path=false) st =
in
List.map resolve_separator_and_format updates
in
switch_env @ pkg_env @ man_path @ [path]
switch_env @ pkg_env @ man_path @ xdg_data_dirs @ zsh_fpath @ [path]


let updates_common ~set_opamroot ~set_opamswitch root switch =
let root =
Expand Down Expand Up @@ -688,11 +709,11 @@ let updates_nix st =
| Some env -> List.map resolve_separator_and_format env)
| _ -> []

let updates ~set_opamroot ~set_opamswitch ?force_path st =
let updates ~set_opamroot ~set_opamswitch ?force_path ?shell st =
let common =
updates_common ~set_opamroot ~set_opamswitch st.switch_global.root st.switch
in
common @ compute_updates ?force_path st @ updates_nix st
common @ compute_updates ?force_path ?shell st @ updates_nix st

let get_pure ?(updates=[]) () =
let env = List.map (fun (v,va) -> v,va,None) (OpamStd.Env.list ()) in
Expand Down Expand Up @@ -943,7 +964,8 @@ let env_hook_file = function
| SH_pwsh _ | SH_cmd -> None

let variables_file = function
| SH_sh | SH_bash | SH_zsh -> "variables.sh"
| SH_sh | SH_bash -> "variables.sh"
| SH_zsh -> "variables.zsh"
| SH_csh -> "variables.csh"
| SH_fish -> "variables.fish"
| SH_pwsh _ -> "variables.ps1"
Expand Down Expand Up @@ -1226,10 +1248,6 @@ let write_dynamic_init_scripts st =
end
| _ -> true
in
let updates =
updates ~set_opamroot:false ~set_opamswitch:false st
|> List.filter is_not_empty_update
in
try
if OpamStateConfig.is_newer_than_self
~lock_kind:`Lock_write st.switch_global then
Expand All @@ -1239,11 +1257,15 @@ let write_dynamic_init_scripts st =
@@ fun _ ->
List.iter
(fun shell ->
let updates =
updates ~set_opamroot:false ~set_opamswitch:false ~shell:(Some shell) st
|> List.filter is_not_empty_update
in
write_script (OpamPath.init st.switch_global.root)
(variables_file shell,
abort_if_set "OPAM_SWITCH_PREFIX" shell
^ string_of_update st shell updates))
[SH_sh; SH_csh; SH_fish; SH_pwsh Powershell; SH_cmd]
[SH_sh; SH_zsh; SH_csh; SH_fish; SH_pwsh Powershell; SH_cmd]
with OpamSystem.Locked ->
OpamConsole.warning
"Global shell init scripts not installed (could not acquire lock)"
Expand Down
6 changes: 4 additions & 2 deletions src/state/opamEnv.mli
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ val add: env -> (spf_resolved, euok_internal) env_update list -> env
(** Like {!get_opam} computes environment modification by OPAM , but returns
these [updates] instead of the new environment. *)
val updates:
set_opamroot:bool -> set_opamswitch:bool -> ?force_path:bool ->
set_opamroot:bool -> set_opamswitch:bool -> ?force_path:bool -> ?shell:shell option ->
'a switch_state -> (spf_resolved, [> euok_writeable ]) env_update list

(** Check if the shell environment is in sync with the current OPAM switch,
Expand All @@ -89,7 +89,9 @@ val is_up_to_date_switch: dirname -> switch -> bool

(** Returns the current environment updates to configure the current switch with
its set of installed packages *)
val compute_updates: ?force_path:bool -> 'a switch_state -> (spf_resolved, [> euok_writeable ]) env_update list
val compute_updates:
?force_path:bool -> ?shell:shell option -> 'a switch_state ->
(spf_resolved, [> euok_writeable ]) env_update list

(** Returns shell-appropriate statement to evaluate [cmd]. *)
val shell_eval_invocation:
Expand Down
5 changes: 5 additions & 0 deletions tests/reftests/action-disk.test
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ FILE(switch-config) Wrote ${BASEDIR}/OPAM/install-from-repo/.opam-sw
FILE(environment) Wrote ${BASEDIR}/OPAM/install-from-repo/.opam-switch/environment atomically in 0.000s
FILE(config) Wrote ${BASEDIR}/OPAM/config atomically in 0.000s
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.sh
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.zsh
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.csh
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.fish
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.ps1
Expand Down Expand Up @@ -602,6 +603,7 @@ FILE(switch-config) Wrote ${BASEDIR}/OPAM/install-from-path-pin-all/
FILE(environment) Wrote ${BASEDIR}/OPAM/install-from-path-pin-all/.opam-switch/environment atomically in 0.000s
FILE(config) Wrote ${BASEDIR}/OPAM/config atomically in 0.000s
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.sh
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.zsh
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.csh
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.fish
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.ps1
Expand Down Expand Up @@ -1246,6 +1248,7 @@ FILE(switch-config) Wrote ${BASEDIR}/OPAM/install-from-git-pin-all/.
FILE(environment) Wrote ${BASEDIR}/OPAM/install-from-git-pin-all/.opam-switch/environment atomically in 0.000s
FILE(config) Wrote ${BASEDIR}/OPAM/config atomically in 0.000s
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.sh
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.zsh
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.csh
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.fish
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.ps1
Expand Down Expand Up @@ -1942,6 +1945,7 @@ FILE(switch-config) Wrote ${BASEDIR}/OPAM/install-from-version-pin/.
FILE(environment) Wrote ${BASEDIR}/OPAM/install-from-version-pin/.opam-switch/environment atomically in 0.000s
FILE(config) Wrote ${BASEDIR}/OPAM/config atomically in 0.000s
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.sh
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.zsh
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.csh
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.fish
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.ps1
Expand Down Expand Up @@ -2339,6 +2343,7 @@ FILE(switch-state) Wrote ${BASEDIR}/OPAM/package-switch/.opam-switc
FILE(environment) Wrote ${BASEDIR}/OPAM/package-switch/.opam-switch/environment atomically in 0.000s
FILE(config) Wrote ${BASEDIR}/OPAM/config atomically in 0.000s
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.sh
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.zsh
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.csh
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.fish
SYSTEM write ${BASEDIR}/OPAM/opam-init/variables.ps1
Expand Down
1 change: 1 addition & 0 deletions tests/reftests/env-idempotent.test
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ User configuration:
### rm -rf root/fake2
### bash bash-lc.sh "'$OPAMBIN' exec --root root --switch=fake -- env" | grep -v MANPATH | grep /root/fake2/ | '=.*' -> ''
PATH
XDG_DATA_DIRS
2 changes: 2 additions & 0 deletions tests/reftests/env.test
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ OPAMSWITCH=bd
OPAM_PACKAGE_NAME=to-build
OPAM_PACKAGE_VERSION=2
OPAM_SWITCH_PREFIX=${BASEDIR}/OPAM/bd
XDG_DATA_DIRS=${BASEDIR}/OPAM/bd/share
PATH=${BASEDIR}/OPAM/bd/bin
### opam remove to-build
The following actions will be performed:
Expand Down Expand Up @@ -650,4 +651,5 @@ OPAMSWITCH=bd
OPAM_PACKAGE_NAME=another-package
OPAM_PACKAGE_VERSION=another-version
OPAM_SWITCH_PREFIX=${BASEDIR}/OPAM/bd
XDG_DATA_DIRS=${BASEDIR}/OPAM/bd/share
PATH=${BASEDIR}/OPAM/bd/bin:another-path
26 changes: 23 additions & 3 deletions tests/reftests/init-scripts.unix.test
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ variables.csh
variables.fish
variables.ps1
variables.sh
variables.zsh
### : Init scripts :
### cat root/opam-init/init.sh
if [ -t 0 ]; then
Expand All @@ -42,7 +43,7 @@ if [[ -o interactive ]]; then
[[ ! -r '${BASEDIR}/root/opam-init/env_hook.zsh' ]] || source '${BASEDIR}/root/opam-init/env_hook.zsh' > /dev/null 2> /dev/null
fi

[[ ! -r '${BASEDIR}/root/opam-init/variables.sh' ]] || source '${BASEDIR}/root/opam-init/variables.sh' > /dev/null 2> /dev/null
[[ ! -r '${BASEDIR}/root/opam-init/variables.zsh' ]] || source '${BASEDIR}/root/opam-init/variables.zsh' > /dev/null 2> /dev/null
### cat root/opam-init/init.fish
if status is-interactive
test -r '${BASEDIR}/root/opam-init/env_hook.fish' && source '${BASEDIR}/root/opam-init/env_hook.fish' > /dev/null 2> /dev/null; or true
Expand All @@ -64,14 +65,26 @@ if Test-Path "${BASEDIR}/root/opam-init/variables.ps1" { . "${BASEDIR}/root/opam
test -z "${OPAM_SWITCH_PREFIX:+x}" || return
# Prefix of the current opam switch
OPAM_SWITCH_PREFIX='${BASEDIR}/root/fake'; export OPAM_SWITCH_PREFIX;
# Current opam switch share dir
XDG_DATA_DIRS='${BASEDIR}/root/fake/share':"$XDG_DATA_DIRS"; export XDG_DATA_DIRS;
# Binary dir for opam switch fake
PATH='${BASEDIR}/root/fake/bin':"$PATH"; export PATH;
### cat root/opam-init/variables.zsh | grep -v man | grep -v MANPATH
test -z "${OPAM_SWITCH_PREFIX:+x}" || return
# Prefix of the current opam switch
OPAM_SWITCH_PREFIX='${BASEDIR}/root/fake'; export OPAM_SWITCH_PREFIX;
# Current opam switch share dir
XDG_DATA_DIRS='${BASEDIR}/root/fake/share':"$XDG_DATA_DIRS"; export XDG_DATA_DIRS;
# Current opam switch zsh site-functions
FPATH='${BASEDIR}/root/fake/share/zsh/site-functions':"$FPATH"; export FPATH;
# Binary dir for opam switch fake
PATH='${BASEDIR}/root/fake/bin':"$PATH"; export PATH;
### test -f root/opam-init/variables.zsh
# Return code 1 #
### cat root/opam-init/variables.fish | grep -v man | grep -v MANPATH
test -z "$OPAM_SWITCH_PREFIX"; or return
# Prefix of the current opam switch
set -gx OPAM_SWITCH_PREFIX '${BASEDIR}/root/fake';
# Current opam switch share dir
set -gx XDG_DATA_DIRS '${BASEDIR}/root/fake/share':"$XDG_DATA_DIRS";
# Binary dir for opam switch fake
set -gx PATH '${BASEDIR}/root/fake/bin' $PATH;
### cat root/opam-init/variables.csh | grep -v man | grep -v MANPATH
Expand All @@ -80,19 +93,26 @@ if ( ${?OPAM_SWITCH_PREFIX} ) then
endif
# Prefix of the current opam switch
setenv OPAM_SWITCH_PREFIX '${BASEDIR}/root/fake'
# Current opam switch share dir
if ( ! ${?XDG_DATA_DIRS} ) setenv XDG_DATA_DIRS ""
setenv XDG_DATA_DIRS '${BASEDIR}/root/fake/share':"$XDG_DATA_DIRS"
# Binary dir for opam switch fake
if ( ! ${?PATH} ) setenv PATH ""
setenv PATH '${BASEDIR}/root/fake/bin':"$PATH"
### cat root/opam-init/variables.cmd | grep -v man | grep -v MANPATH
if defined OPAM_SWITCH_PREFIX if "%OPAM_SWITCH_PREFIX%" neq "" goto :EOF
:: Prefix of the current opam switch
set "OPAM_SWITCH_PREFIX=${BASEDIR}/root/fake"
:: Current opam switch share dir
set "XDG_DATA_DIRS=${BASEDIR}/root/fake/share:%XDG_DATA_DIRS%"
:: Binary dir for opam switch fake
set "PATH=${BASEDIR}/root/fake/bin:%PATH%"
### cat root/opam-init/variables.ps1 | grep -v man | grep -v MANPATH
if ($env:OPAM_SWITCH_PREFIX -ne $null -and $env:OPAM_SWITCH_PREFIX -ne '') { return }
# Prefix of the current opam switch
$env:OPAM_SWITCH_PREFIX='${BASEDIR}/root/fake'
# Current opam switch share dir
$env:XDG_DATA_DIRS='${BASEDIR}/root/fake/share:' + "$env:XDG_DATA_DIRS"
# Binary dir for opam switch fake
$env:PATH='${BASEDIR}/root/fake/bin:' + "$env:PATH"
### : Env hook scripts :
Expand Down