diff --git a/build_system/clerk_driver.ml b/build_system/clerk_driver.ml index 904905af..4437d10a 100644 --- a/build_system/clerk_driver.ml +++ b/build_system/clerk_driver.ml @@ -278,10 +278,7 @@ module Poll = struct Some root | _ -> None) - let exec_dir : File.t = - (* Do not use Sys.executable_name, which may resolve symlinks: we want the - original path. (e.g. _build/install/default/bin/foo is a symlink) *) - Filename.dirname Sys.argv.(0) + let exec_dir : File.t = Catala_utils.Cli.exec_dir let clerk_exe : File.t Lazy.t = lazy (Unix.realpath Sys.executable_name) diff --git a/compiler/catala_utils/cli.ml b/compiler/catala_utils/cli.ml index 7aafdecf..eb9cbec4 100644 --- a/compiler/catala_utils/cli.ml +++ b/compiler/catala_utils/cli.ml @@ -113,6 +113,15 @@ let file_lang filename = @{%s@}, and @{--language@} was not specified" filename) +let exec_dir = + let cmd = Sys.argv.(0) in + if String.contains cmd '/' then + (* Do not use Sys.executable_name, which may resolve symlinks: we want the + original path. (e.g. _build/install/default/bin/foo is a symlink) *) + Filename.dirname cmd + else (* searched in PATH *) + Filename.dirname Sys.executable_name + let reverse_path ?(from_dir = Sys.getcwd ()) ~to_dir f = if Filename.is_relative from_dir then invalid_arg "File.with_reverse_path" else if not (Filename.is_relative f) then f @@ -225,7 +234,6 @@ module Flags = struct let env = Cmd.Env.info "CATALA_PLUGINS" in let default = let ( / ) = Filename.concat in - let exec_dir = Filename.(dirname Sys.argv.(0)) in let dev_plugin_dir = exec_dir / "plugins" in if Sys.file_exists dev_plugin_dir then (* When running tests in place, may need to lookup in _build/default diff --git a/compiler/catala_utils/cli.mli b/compiler/catala_utils/cli.mli index 10313147..8cfb4222 100644 --- a/compiler/catala_utils/cli.mli +++ b/compiler/catala_utils/cli.mli @@ -154,3 +154,8 @@ val s_plugins : string exception Exit_with of int (** Exit with a specific exit code (but less brutally than [Sys.exit] which would bypass all finalisers) *) + +(** {2 Other helpers} *) + +val exec_dir : file +(** Returns the directory of the currently running executable *)