camlspotter avatar camlspotter committed bcf9a42

cwd of Env.t was not considered, and file loading failed

Comments (0)

Files changed (4)

   open Value
   module Format = OCaml.Format
 
-  let str_of_global_ident = ref (fun ~load_paths:_ _ -> assert false : load_paths: string list -> Ident.t -> string * Value.structure)
+  let str_of_global_ident = ref (fun ~cwd:_ ~load_paths:_ _ -> assert false : cwd: string -> load_paths: string list -> Ident.t -> string * Value.structure)
   let packed = ref (fun _ _ -> assert false : Env.t -> string -> Value.t)
 
   let rec find_path env (kind, p) : Value.z = 
         | None -> 
             if Ident.global id then
               lazy begin try
-                let path, str = !str_of_global_ident ~load_paths:env.load_paths id in
+                let path, str = !str_of_global_ident ~cwd:env.cwd ~load_paths:env.load_paths id in
                 let str = Structure ( { PIdent.path = path; ident = None }, 
                                       str,
                                       None (* CR jfuruse: todo (read .mli *))
 module Eval : sig
   (** These function ref will be initialized from ocamlspot.ml *)
   val str_of_global_ident :
-    (load_paths:string list -> Ident.t -> string * Value.structure) ref
+    (cwd:string -> load_paths:string list -> Ident.t -> string * Value.structure) ref
   val packed : (Env.t -> string -> Value.t) ref
     
   val find_path : Env.t -> Kind.t * Path.t -> Value.z
 module Load : sig
   exception Old_cmt of string (* cmt *) * string (* source *)
   val load : load_paths:string list -> string -> Unit.t
-  val load_module : ?spit:bool -> load_paths:string list -> string -> Unit.t
+  val load_module : ?spit:bool -> cwd:string -> load_paths:string list -> string -> Unit.t
 end = struct
 
   let check_time_stamp ~cmt source =
         | Some cmtname -> load ~load_paths cmtname
         | None -> raise e
 
+  let with_cwd cwd f = 
+    let d = Sys.getcwd () in
+    protect ~f:(fun () -> Sys.chdir cwd; f ()) () 
+      ~finally: (fun _ -> Sys.chdir d)
+
   (* CR jfuruse: searching algorithm must be reconsidered *)        
-  let load_module ?(spit=false) ~load_paths name =
+  let load_module ?(spit=false) ~cwd ~load_paths name =
     let cmtname = name ^ if spit then ".cmti" else ".cmt" in
     try
-      load ~load_paths cmtname
+      with_cwd cwd (fun () -> load ~load_paths cmtname)
     with
     | Failure s ->
         let spitname = name ^ if spit then ".cmt" else ".cmti" in
   in
   eval_and_find path
 
-let str_of_global_ident ~load_paths id =
+let str_of_global_ident ~cwd ~load_paths id =
   assert (Ident.global id);
-  let file = Load.load_module ~spit:Spotconfig.print_interface ~load_paths (Ident0.name id) in
+  let file = Load.load_module ~spit:Spotconfig.print_interface ~cwd ~load_paths (Ident0.name id) in
   file.Unit.path,
   Eval.structure (empty_env file) file.Unit.top
 
 
 exception Old_cmt of string * string
 val load : load_paths:string list -> string -> Unit.t
-val load_module : ?spit:bool -> load_paths:string list -> string -> Unit.t (* CR jfuruse: spit *)
+val load_module : ?spit:bool -> cwd:string -> load_paths:string list -> string -> Unit.t (* CR jfuruse: spit *)
 
 val empty_env   : Unit.t -> Env.t
 val invalid_env : Unit.t -> Env.t
 type result = File_itself | Found_at of Region.t | Predefined
 
 val find_path_in_flat : Unit.t -> Kind.t * Path.t -> PIdent.t * result
-val str_of_global_ident : load_paths:string list -> Ident.t -> string * Value.structure
+val str_of_global_ident : cwd:string -> load_paths:string list -> Ident.t -> string * Value.structure
 val eval_packed : Env.t -> string -> Value.t
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.