Skip to content

Commit b32009d

Browse files
committed
Always test if file exists before sourcing, in all shells
1 parent 7719e44 commit b32009d

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/state/opamEnv.ml

+21-21
Original file line numberDiff line numberDiff line change
@@ -873,25 +873,21 @@ let env_hook_script shell =
873873
(env_hook_script_base shell)
874874

875875
let source root shell f =
876+
let if_exists_source: _ format = match shell with
877+
| SH_csh -> "if ( -f %s ) source %s\n"
878+
| SH_fish -> "test -r %s && source %s\n"
879+
| SH_sh | SH_bash -> "test -r %s && . %s\n"
880+
| SH_zsh -> "[[ -r %s ]] && source %s\n"
881+
| SH_cmd -> "if exist \"%s\" call \"%s\"\n"
882+
| SH_pwsh _ -> "if Test-Path \"%s\" { . \"%s\" }\n"
883+
in
876884
let fname = OpamFilename.prettify (OpamPath.init root // f) in
885+
Printf.sprintf if_exists_source fname fname
886+
887+
let if_not_login_script shell t =
877888
match shell with
878-
| SH_csh ->
879-
Printf.sprintf "if ( -f %s ) source %s >& /dev/null\n" fname fname
880-
| SH_fish ->
881-
if f = init_file shell then
882-
Printf.sprintf "if not status is-login; and test -r %s\n source %s\nend\n" fname fname
883-
else
884-
Printf.sprintf "source %s\n" fname
885-
| SH_sh | SH_bash ->
886-
Printf.sprintf "test -r %s && . %s > /dev/null 2> /dev/null || true\n"
887-
fname fname
888-
| SH_zsh ->
889-
Printf.sprintf "[[ ! -r %s ]] || source %s > /dev/null 2> /dev/null\n"
890-
fname fname
891-
| SH_cmd ->
892-
Printf.sprintf "if exist \"%s\" call \"%s\" >NUL 2>NUL\n" fname fname
893-
| SH_pwsh _ ->
894-
Printf.sprintf ". \"%s\" *> $null\n" fname
889+
| SH_fish -> Printf.sprintf "if not status is-login\n %send\n" t
890+
| _ -> t (* Seems this option is only needed for fish *)
895891

896892
let if_interactive_script shell t e =
897893
let ielse else_opt = match else_opt with
@@ -1116,7 +1112,7 @@ let update_dot_profile root dot_profile shell =
11161112
# This section can be safely removed at any time if needed.\n\
11171113
%s\
11181114
# END opam configuration\n"
1119-
(source root shell init_file) in
1115+
(if_not_login_script shell (source root shell init_file)) in
11201116
OpamFilename.write dot_profile (old_body ^ opam_section);
11211117
OpamConsole.msg " Added %d lines after line %d in %s.\n"
11221118
(count_lines opam_section - 1) (count_lines old_body) pretty_dot_profile
@@ -1168,17 +1164,21 @@ let setup
11681164
opam_root_msg;
11691165
begin match dot_profile with
11701166
| Some dot_profile ->
1167+
let re = Re.compile (Re.char '\n') in
1168+
let to_add = Re.replace_string re ~by:"\n " @@
1169+
if_not_login_script shell @@
1170+
source root shell @@ init_file shell
1171+
in
11711172
OpamConsole.msg
11721173
"If you allow it to, this initialisation step will update\n\
1173-
\ your %s configuration by adding the following line to %s:\n\
1174+
\ your %s configuration by adding the following line(s) to %s:\n\
11741175
\n\
11751176
\ %s\
11761177
\n\
11771178
\ Otherwise, every time"
11781179
(OpamConsole.colorise `bold (string_of_shell shell))
11791180
(OpamConsole.colorise `cyan @@ OpamFilename.prettify dot_profile)
1180-
(OpamConsole.colorise `bold @@ String.concat "\n " @@
1181-
String.split_on_char '\n' (source root shell (init_file shell)));
1181+
(OpamConsole.colorise `bold to_add);
11821182
| None ->
11831183
OpamConsole.msg "When"
11841184
end;

0 commit comments

Comments
 (0)