diff --git a/src/libraries/utils/filepath.ml b/src/libraries/utils/filepath.ml index b2ea6e9bf6e0db85d1e1970015e9133a92dccf73..5e34b824d20cf2d7d29efda01bd08f6c9775e6c1 100644 --- a/src/libraries/utils/filepath.ml +++ b/src/libraries/utils/filepath.ml @@ -125,22 +125,15 @@ let insert base path_name = Array.set cache (hash land 255) (Some (path_name, path)); path -(* TODO: we currently use PWD instead of Sys.getcwd () because OCaml has - no function in its stdlib to resolve symbolic links (e.g. realpath) - for a given path. 'getcwd' always resolves them, but if the user - supplies a path with symbolic links, this may cause issues. - Instead of forcing the user to always provide resolved paths, we - currently choose to never resolve them. - Note that, in rare situations (e.g. some Docker images), PWD does not - exist in the environment, so in that case, we fallback to Sys.getcwd. - - REMARK[LC]: when the Frama-C binary is directly invoked by Node without - going through a shell script wrapper like ./bin/frama-c, the environment - variable "PWD" is _no more_ synchronized with Sys.getcwd. - This problem has been solved in `Dome.spawn()` by forcing the `PWD` - environement variable accordingly. +(* Note: the call to Unix.realpath prevents some issues with symbolic links + in directory names. If you have problems with this, please contact us. + For the same reason, Sys.getcwd should _not_ be called directly, but only + via this function, to avoid conflicting results in case the user forgot + to call Unix.realpath. *) -let cwd = insert dummy (try Sys.getenv "PWD" with Not_found -> Sys.getcwd ()) +let pwd () = Unix.(realpath (getcwd ())) + +let cwd = insert dummy (pwd ()) type existence = | Must_exist @@ -336,8 +329,6 @@ type position = let pp_pos fmt pos = Format.fprintf fmt "%a:%d" Normalized.pretty pos.pos_path pos.pos_lnum -let pwd () = try Unix.getenv "PWD" with Not_found -> Sys.getcwd () - (* Local Variables: compile-command: "make -C ../../.." diff --git a/src/libraries/utils/filepath.mli b/src/libraries/utils/filepath.mli index 81a1e96120e69bc9c797a056441d2bf913ffad81..ed6d96033cba38422aa88004c828af72019b8396 100644 --- a/src/libraries/utils/filepath.mli +++ b/src/libraries/utils/filepath.mli @@ -209,15 +209,8 @@ type position = val pp_pos : Format.formatter -> position -> unit (** Return the current working directory. - Currently uses the environment's PWD instead of Sys.getcwd () because OCaml - has no function in its stdlib to resolve symbolic links (e.g. realpath) - for a given path. 'getcwd' always resolves them, but if the user - supplies a path with symbolic links, this may cause issues. - Instead of forcing the user to always provide resolved paths, we - currently choose to never resolve them. - We only resort to getcwd() to avoid issues when PWD does not exist. - Note that this function does not validate that PWD has not been tampered - with. + Implicitly uses {!Unix.realpath} to normalize paths and avoid issues with + symbolic links in directory names. @since 25.0-Manganese *)