From 753c045c67a282f0b60d772d64b314636c7948ab Mon Sep 17 00:00:00 2001 From: Andre Maroneze <andre.maroneze@cea.fr> Date: Tue, 1 Aug 2023 15:40:04 +0200 Subject: [PATCH] [Kernel] remove reliance on 'PWD' environment variable The absence of Unix.realpath in OCaml < 4.13 led us to rely on the PWD environment variable to avoid issues with symbolic links in directories. Now that Frama-C requires OCaml >= 4.13, we can use it instead and avoid issues with Dune and Node.js related to out-of-date values for PWD. --- src/libraries/utils/filepath.ml | 25 ++++++++----------------- src/libraries/utils/filepath.mli | 11 ++--------- 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/src/libraries/utils/filepath.ml b/src/libraries/utils/filepath.ml index b2ea6e9bf6e..5e34b824d20 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 81a1e96120e..ed6d96033cb 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 *) -- GitLab