ocaml-community/utop

utop/init.ml does not respect XDG on Windows

jonahbeckford opened this issue · 0 comments

Context

You can see in my utop session below that XDG correctly reports the configuration directory is LOCALAPPDATA, but some magic in utop is mangling the path badly.

You can follow along with DkML 2.0.1 at https://gitlab.com/diskuv-ocaml/distributions/dkml/-/releases/2.0.1/downloads/setup64u.exe in a Windows Sandbox or a VM. Then run utop in a Command Prompt or PowerShell.

utop # #require "utop";;

utop # let xdg = Xdg.create ~env:Sys.getenv_opt () ;;
val xdg : Xdg.t = <abstr>

utop # Xdg.config_dir xdg ;;
- : string = "C:\\Users\\WDAGUtilityAccount\\AppData\\Local"

utop # LTerm_resources.xdgbd_file ~loc:LTerm_resources.Config "utop/init.ml";;
- : string =
"C:\\Users\\WDAGUtilityAccount\\AppData\\Local\\Programs\\DiskuvOCaml\\tools\\MSYS2\\home\\WDAGUtilityAccount\\Local Settings\\utop/init.ml"

Root Cause Guess

Somehow xdgbd_file is combining both HOME and XDG (on Windows HOME should either be ignored or be the fallback to LOCALAPPDATA). My guess is that something is interpreting Xdg.config_dir xdg as a relative path (a common mistake is to assume that absolute paths have to start with a / forward slash), and then making Xdg.config_dir xdg an absolute path relative to $HOME.

That means utop can't locate the correct init.ml file:

utop/src/lib/uTop_main.ml

Lines 1406 to 1417 in 5b98d28

if Sys.file_exists ".ocamlinit" && Sys.getcwd () <> LTerm_resources.home then
Some ".ocamlinit"
else
let xdg_fn = LTerm_resources.xdgbd_file ~loc:LTerm_resources.Config "utop/init.ml" in
if Sys.file_exists xdg_fn then
Some xdg_fn
else
let fn = Filename.concat LTerm_resources.home ".ocamlinit" in
if Sys.file_exists fn then
Some fn
else
None

Related