Skip to content

Commit

Permalink
Set and persist $HOME on Windows in opam init
Browse files Browse the repository at this point in the history
Windows setups will not normally have $HOME set, which causes opam init
to initialise in the cwd. Native Windows updated to query the user's
Documents folder and use that location instead.

Additionally, opam init offers to save $HOME permanently to the user's
registry, as having HOME set is important for OCaml (so that .ocamlinit
can be found) and also for related tools (Vim, for example, can display
and use ~\ paths if $HOME is set)

Signed-off-by: David Allsopp <david.allsopp@metastack.com>
  • Loading branch information
dra27 committed Sep 2, 2017
1 parent 836ae34 commit ed44880
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/client/opamClient.ml
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,30 @@ let init
let root_empty =
not (OpamFilename.exists_dir root) || OpamFilename.dir_is_empty root in

if update_config <> `no && (OpamStd.Sys.(os () = Win32) && (try ignore (Sys.getenv "HOME"); false with Not_found -> true)) then begin
let home = OpamStd.Sys.home () in
let persistHomeDirectory home =
OpamConsole.msg "Persisting HOME (this may take a few seconds)...";
OpamStd.Win32.persistHomeDirectory home;
OpamConsole.msg " done\n" in
let get_action () =
if update_config = `yes || OpamCoreConfig.(!r.answer <> None) then
Some "y"
else
OpamConsole.read
"The HOME environment variable is not set, so OPAM has defaulted to:\n\
\ %s\n\
OCaml ideally requires $HOME to be set properly (in order to find .ocamlinit)\n\
Do you want OPAM to alter your persistent environment so that HOME is always set?\n\
(default is 'yes')\n\
\ [Y/n]" home in
match get_action () with
| Some ("y" | "Y" | "yes" | "YES") ->
persistHomeDirectory home
| _ ->
()
end;

let gt, rt, default_compiler =
if OpamFile.exists config_f then (
OpamConsole.msg "Opam has already been initialized.\n";
Expand Down
15 changes: 14 additions & 1 deletion src/core/opamStd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,20 @@ module OpamSys = struct
else default_columns

let home =
let home = lazy (try Env.get "HOME" with Not_found -> Sys.getcwd ()) in
let home = lazy (
try
Env.get "HOME"
with Not_found ->
if Sys.os_type = "Win32" then
(*
* Windows setups will rarely have $HOME set, so cwd is a poor default. Instead, return
* the value of the user's My Documents folder.
*
* CSIDL_PERSONAL = 0x5; SHGFP_TYPE_CURRENT = 0x0
*)
Win32.shGetFolderPath 5 0
else
Sys.getcwd ()) in
fun () -> Lazy.force home

let etc () = "/etc"
Expand Down

0 comments on commit ed44880

Please sign in to comment.