Skip to content

Commit 43c2073

Browse files
committed
Fix open_modules: use temp file in dedicated tmp directory
This avoids a race when multiple sessions run concurrently in the same directory.
1 parent adb952e commit 43c2073

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

plugin/iOLib.ml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,11 @@ let extract ~opaque_access ~file ident =
156156
CWarnings.set_flags warnings
157157

158158
(* Add any modules that have been marked "open" *)
159-
let open_modules ms mlf =
159+
let open_modules ~dir ms mlf =
160160
let prefix = String.concat "" List.(concat @@ map (fun m -> ["open "; m; ";;"]) ms) in
161-
let open_cmd = Printf.sprintf "awk -v n=1 -v s=\"%s\" 'NR == n {print s} {print}' %s > __qc_tmp && mv __qc_tmp %s" prefix mlf mlf in
162-
ignore (Sys.command open_cmd)
161+
let open_cmd = Printf.sprintf "awk -v n=1 -v s=\"%s\" 'NR == n {print s} {print}' %s > %s/__qc_tmp && mv %s/__qc_tmp %s" prefix mlf dir dir mlf in
162+
if Sys.command open_cmd <> 0 then
163+
CErrors.user_err (str "awk command failed on " ++ str mlf ++ fnl ())
163164

164165
let tmp_int_re = Str.regexp "type int =[ ]*int"
165166

@@ -180,16 +181,16 @@ let remove_mli mlif =
180181
Sys.remove mlif;
181182
ignore (Sys.command ("touch " ^ mlif))
182183

183-
let fixup mlif mlf =
184-
open_modules !modules_to_open mlf;
184+
let fixup ~dir mlif mlf =
185+
open_modules ~dir !modules_to_open mlf;
185186
redefine_int mlf;
186187
remove_mli mlif
187188

188189
(* Copy over the contents of the ocaml directory *)
189190
let copy_dirs dir ds =
190191
List.iter (fun s -> ignore (Sys.command (Printf.sprintf "cp -r %s %s" s dir))) ds
191192

192-
let compile dir mlif mlf =
193+
let compile ~dir mlif mlf =
193194
let run_command cmd =
194195
if Sys.command cmd <> 0 then
195196
let build_log = read_file (dir ^ "/build.log") in
@@ -283,17 +284,17 @@ let run_exec execn =
283284
| Repl -> run_exec_repl execn
284285
| Forward -> run_exec_forward execn
285286

286-
let compile_and_run dir mlif mlf =
287-
compile dir mlif mlf |> run_exec
287+
let compile_and_run ~dir mlif mlf =
288+
compile ~dir mlif mlf |> run_exec
288289

289290
let extract_and_run ~plugin_name ~opaque_access ident =
290291
let dir = mk_tmp_dir ~plugin_name in
291292
let mlf : string = dir </> "extracted_main.ml" in
292293
let mlif : string = Filename.chop_extension mlf ^ ".mli" in
293294
extract ~opaque_access ~file:mlf ident;
294-
fixup mlif mlf;
295+
fixup ~dir mlif mlf;
295296
copy_dirs dir !extra_dir;
296-
compile_and_run dir mlif mlf
297+
compile_and_run ~dir mlif mlf
297298
;;
298299

299300
let mk_ref s = CAst.make @@ CRef (qualid_of_string s, None)

0 commit comments

Comments
 (0)