Source file load_path.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
open Local_store
exception Parse_error of string
(** Splits input line into tokens, with spaces acting as separators.
Leading/trailing whitespace is removed to avoid empty tokens. Backslashing
works inside tokens, namely '\n', '\r', ' ' and '\' itself should be
escaped with '\'. *)
let split_and_unescape ~buffer line =
let len = String.length line in
let[@inline] end_current_token current_token tokens =
let token = Buffer.contents current_token in
Buffer.clear current_token;
if String.equal token "" then tokens else token :: tokens
in
let rec loop i current_token tokens =
if i >= len
then (List.rev (end_current_token current_token tokens))
else (
match String.unsafe_get line i with
| '\\' when i + 1 < len ->
(match String.unsafe_get line (i + 1) with
| '\\' ->
Buffer.add_char current_token '\\';
loop (i + 2) current_token tokens
| ' ' ->
Buffer.add_char current_token ' ';
loop (i + 2) current_token tokens
| 'n' ->
Buffer.add_char current_token '\n';
loop (i + 2) current_token tokens
| 'r' ->
Buffer.add_char current_token '\r';
loop (i + 2) current_token tokens
| c -> raise (
Parse_error (Printf.sprintf "Invalid escape sequence: \\%c" c)))
| '\\' -> raise (Parse_error "Trailing backslash")
| ' ' ->
let tokens = end_current_token current_token tokens in
loop (i + 1) current_token tokens
| c ->
Buffer.add_char current_token c;
loop (i + 1) current_token tokens)
in
loop 0 buffer []
;;
module Dune_manifests_reader : sig
[@@@warning "-32"]
module Path : sig
module For_testing : sig
val root_override : string option ref
end
module Load_root_relative : sig
type t
val of_string : string -> t
end
module Cwd_relative : sig
type t
val to_string : t -> string
end
end
type t
val create : unit -> t
val iter_manifest
: t
-> f:(filename:string -> location:Path.Cwd_relative.t ->
cmx_guaranteed:bool -> unit)
-> manifest_path:Path.Load_root_relative.t
-> unit
end = struct
module Path : sig
module For_testing : sig
val root_override : string option ref
end
module Cwd_relative : sig
type t
val to_string : t -> string
end
module Load_root_relative : sig
type t
val of_string : string -> t
val to_cwd_relative : t -> Cwd_relative.t
module Hash_set : sig
type elem := t
type t
val create : unit -> t
val mem : t -> elem -> bool
val add : t -> elem -> unit
end
end
end = struct
module For_testing = struct
let root_override = ref None
end
let root =
let from_env_var = lazy (
let var_name = "MANIFEST_FILES_ROOT" in
match Sys.getenv_opt var_name with
| None -> failwith (var_name ^ " not set")
| Some path -> path)
in
fun () ->
match !For_testing.root_override with
| None -> Lazy.force from_env_var
| Some root -> root
module Cwd_relative = struct
type t = string
let to_string path = path
end
module Load_root_relative = struct
type t = string
let of_string path = path
let to_cwd_relative path =
Filename.concat (root ()) path
;;
module Hash_set = struct
type t = unit Misc.Stdlib.String.Tbl.t
let create () = Misc.Stdlib.String.Tbl.create 42
let mem t path =
Misc.Stdlib.String.Tbl.mem t path
let add t path =
Misc.Stdlib.String.Tbl.add t path ()
end
end
end
type t = { visited : Path.Load_root_relative.Hash_set.t }
let create () = { visited = Path.Load_root_relative.Hash_set.create () }
let visit { visited } ~f path =
if Path.Load_root_relative.Hash_set.mem visited path
then ()
else (
Path.Load_root_relative.Hash_set.add visited path;
f (Path.Load_root_relative.to_cwd_relative path))
;;
let iter_lines ~f path =
In_channel.with_open_text
(Path.Cwd_relative.to_string path)
(fun ic ->
let rec loop () =
try
let line = String.trim (input_line ic) in
f line;
loop ()
with
| End_of_file -> ()
in
loop ())
;;
module Entry = struct
type t =
| File of
{ filename : string
; location : Path.Load_root_relative.t
; cmx_guaranteed : bool
}
| Manifest of Path.Load_root_relative.t
end
let parse_line ~buffer line =
match split_and_unescape ~buffer line with
| [ "file"; filename; location ] ->
let location = Path.Load_root_relative.of_string location in
Some (Entry.File { filename; location; cmx_guaranteed = false })
| [ "file_x"; filename; location ] ->
let location = Path.Load_root_relative.of_string location in
Some (Entry.File { filename; location; cmx_guaranteed = true })
| [ "manifest"; _; location ] ->
let location = Path.Load_root_relative.of_string location in
Some (Entry.Manifest location)
| [] -> None
| _ -> raise (Parse_error ("Cannot parse manifest file line: " ^ line))
;;
let rec iter_manifest t ~f ~manifest_path =
let buffer = Buffer.create 16 in
visit t manifest_path ~f:(fun manifest_path ->
iter_lines manifest_path ~f:(fun line ->
match parse_line ~buffer line with
| None -> ()
| Some (Entry.File { filename; location; cmx_guaranteed }) ->
visit t location ~f:(fun location ->
f ~filename ~location ~cmx_guaranteed)
| Some (Manifest manifest_path) -> iter_manifest t ~f ~manifest_path))
;;
end
module For_testing = struct
exception Parse_error = Parse_error
let split_and_unescape = split_and_unescape
let set_manifest_files_root path =
Dune_manifests_reader.Path.For_testing.root_override := path
end
type visibility = Visible of { cmx_guaranteed : bool } | Hidden
let visibility_eq a b =
match a, b with
| ( Visible { cmx_guaranteed = cmx_guaranteed_a },
Visible { cmx_guaranteed = cmx_guaranteed_b } ) ->
Bool.equal cmx_guaranteed_a cmx_guaranteed_b
| Hidden, Hidden -> true
| Visible _, Hidden | Hidden, Visible _ -> false
module Dir : sig
type entry = {
basename : string;
path : string
}
type t
val path : t -> string
val files : t -> entry list
val basenames : t -> string list
val visibility : t -> visibility
val create : visibility -> string -> t
val find : t -> string -> string option
val find_normalized : t -> string -> string option
val check : visibility -> t -> bool
end = struct
type entry = {
basename : string;
path : string
}
type t = {
path : string;
files : entry list;
visibility : visibility;
}
let path t = t.path
let files t = t.files
let basenames t = List.map (fun { basename; _ } -> basename) t.files
let visibility t = t.visibility
let find t fn =
List.find_map (fun { basename; path } ->
if String.equal basename fn then
Some path
else
None) t.files
let find_normalized t fn =
let fn = Misc.normalized_unit_filename fn in
let search { basename; path } =
if Misc.normalized_unit_filename basename = fn then
Some path
else
None
in
List.find_map search t.files
let check visibility t =
visibility_eq visibility t.visibility && Directory_content_cache.check t.path
let create visibility path =
let files = Array.to_list (Directory_content_cache.read path)
|> List.map (
fun basename -> { basename; path = Filename.concat path basename }) in
{ path; files; visibility }
end
(** Stores cached paths to files *)
module Path_cache : sig
val reset : unit -> unit
val prepend_add : Dir.t -> unit
val add : Dir.t -> unit
val prepend_add_single : hidden:bool -> cmx_guaranteed:bool ->
string -> string -> unit
val find : uncap:bool -> string -> string * visibility
end = struct
module STbl = Misc.Stdlib.String.Tbl
type visible_registry = Clflags.visible_include STbl.t
type hidden_registry = string STbl.t
let visible_files : visible_registry ref = s_table STbl.create 42
let visible_files_uncap : visible_registry ref = s_table STbl.create 42
let hidden_files : hidden_registry ref = s_table STbl.create 42
let hidden_files_uncap : hidden_registry ref = s_table STbl.create 42
let reset () =
STbl.clear !hidden_files;
STbl.clear !hidden_files_uncap;
STbl.clear !visible_files;
STbl.clear !visible_files_uncap
let prepend_add_single ~hidden ~cmx_guaranteed base fn =
if hidden then begin
STbl.replace !hidden_files base fn;
STbl.replace !hidden_files_uncap (Misc.normalized_unit_filename base) fn
end else begin
STbl.replace !visible_files base { Clflags.path = fn; cmx_guaranteed };
STbl.replace !visible_files_uncap (String.uncapitalize_ascii base)
{ Clflags.path = fn; cmx_guaranteed }
end
let prepend_add dir =
let hidden, cmx_guaranteed =
match Dir.visibility dir with
| Hidden -> true, false
| Visible { cmx_guaranteed } -> false, cmx_guaranteed
in
List.iter
(fun ({ basename; path } : Dir.entry) ->
prepend_add_single ~hidden ~cmx_guaranteed basename path)
(Dir.files dir)
let add dir =
let update base fn visible_files hidden_files =
match Dir.visibility dir with
| Hidden ->
if not (STbl.mem !hidden_files base) then
STbl.replace !hidden_files base fn
| Visible { cmx_guaranteed } ->
if not (STbl.mem !visible_files base) then
STbl.replace !visible_files base { Clflags.path = fn; cmx_guaranteed }
in
List.iter
(fun ({ basename = base; path = fn } : Dir.entry) ->
update base fn visible_files hidden_files;
let ubase = Misc.normalized_unit_filename base in
update ubase fn visible_files_uncap hidden_files_uncap)
(Dir.files dir)
let find fn visible_files hidden_files =
try
let { Clflags.path; cmx_guaranteed } = STbl.find !visible_files fn in
(path, Visible { cmx_guaranteed })
with
| Not_found -> (STbl.find !hidden_files fn, Hidden)
let find ~uncap fn =
if uncap then
find (String.uncapitalize_ascii fn) visible_files_uncap hidden_files_uncap
else
find fn visible_files hidden_files
end
type auto_include_callback =
(Dir.t -> string -> string option) -> string -> string
let visible_dirs = s_ref []
let visible_basenames = s_ref []
let hidden_dirs = s_ref []
let hidden_basenames = s_ref []
let no_auto_include _ _ = raise Not_found
let auto_include_callback = ref no_auto_include
let reset () =
assert (not Config.merlin || Local_store.is_bound ());
Path_cache.reset ();
hidden_dirs := [];
hidden_basenames := [];
visible_dirs := [];
visible_basenames := [];
auto_include_callback := no_auto_include
;;
type dirs_and_files =
{ dirs : Dir.t list
; basenames : string list
}
let get_visible () =
{ dirs = List.rev !visible_dirs; basenames = !visible_basenames }
let get_path_list () =
Misc.rev_map_end Dir.path !visible_dirs (List.rev_map Dir.path !hidden_dirs)
type paths =
{ visible : Clflags.visible_include list;
hidden : string list }
let get_paths () =
let visible_dir_to_include dir : Clflags.visible_include =
let cmx_guaranteed =
match Dir.visibility dir with
| Hidden -> Misc.fatal_error "Load_path.get_paths"
| Visible { cmx_guaranteed } -> cmx_guaranteed
in
{ path = Dir.path dir; cmx_guaranteed }
in
{ visible = List.rev_map visible_dir_to_include !visible_dirs;
hidden = List.rev_map Dir.path !hidden_dirs }
let () = ignore Path_cache.prepend_add_single;;
let init ~auto_include ~visible ~hidden =
assert (not Config.merlin || Local_store.is_bound ());
let get_new_dirs ~get_visibility ~get_path new_entries old_dirs =
let create_dir entry = Dir.create (get_visibility entry) (get_path entry) in
let rec create_dirs ~acc = function
| [] -> Some acc
| new_entry :: new_rest -> create_dirs ~acc:(create_dir new_entry :: acc) new_rest
in
let rec process_entries ~acc new_entries old_dirs =
match new_entries, old_dirs with
| [], [] -> None
| new_entry :: new_rest, [] -> create_dirs ~acc (new_entry :: new_rest)
| [], _ :: _ -> Some acc
| new_entry :: new_rest, old_dir :: old_rest ->
let visibility = get_visibility new_entry in
if String.equal (get_path new_entry) (Dir.path old_dir)
&& Dir.check visibility old_dir
then process_entries ~acc:(old_dir :: acc) new_rest old_rest
else create_dirs ~acc (new_entry :: new_rest)
in
process_entries ~acc:[] new_entries old_dirs
in
let new_visible =
get_new_dirs visible (List.rev !visible_dirs)
~get_visibility:(fun ({ path = _; cmx_guaranteed } : Clflags.visible_include) ->
Visible { cmx_guaranteed })
~get_path:(fun ({ path; cmx_guaranteed = _ } : Clflags.visible_include) -> path)
in
let new_hidden =
get_new_dirs hidden (List.rev !hidden_dirs)
~get_visibility:(Fun.const Hidden) ~get_path:Fun.id
in
let update =
match new_visible, new_hidden with
| None, None -> None
| Some v, None -> Some (v, !hidden_dirs)
| None, Some h -> Some (!visible_dirs, h)
| Some v, Some h -> Some (v, h)
in
match update with
| None -> ()
| Some (new_visible, new_hidden) ->
reset ();
visible_dirs := new_visible;
hidden_dirs := new_hidden;
List.iter Path_cache.prepend_add new_hidden;
List.iter Path_cache.prepend_add new_visible;
auto_include_callback := auto_include
let remove_dir dir =
assert (not Config.merlin || Local_store.is_bound ());
let visible = List.filter (fun d -> Dir.path d <> dir) !visible_dirs in
let hidden = List.filter (fun d -> Dir.path d <> dir) !hidden_dirs in
if List.compare_lengths visible !visible_dirs <> 0
|| List.compare_lengths hidden !hidden_dirs <> 0 then begin
reset ();
visible_dirs := visible;
hidden_dirs := hidden;
List.iter Path_cache.prepend_add hidden;
List.iter Path_cache.prepend_add visible
end
let add (dir : Dir.t) =
assert (not Config.merlin || Local_store.is_bound ());
Path_cache.add dir;
match Dir.visibility dir with
| Hidden -> hidden_dirs := dir :: !hidden_dirs
| Visible _ -> visible_dirs := dir :: !visible_dirs
let append_dir = add
let add_dir visibility dir = add (Dir.create visibility dir)
let prepend_dir (dir : Dir.t) =
assert (not Config.merlin || Local_store.is_bound ());
Path_cache.prepend_add dir;
match Dir.visibility dir with
| Hidden -> hidden_dirs := !hidden_dirs @ [dir]
| Visible _ -> visible_dirs := !visible_dirs @ [dir]
let is_basename fn = Filename.basename fn = fn
let find fn =
assert (not Config.merlin || Local_store.is_bound ());
try
if is_basename fn && not !Sys.interactive then
fst (Path_cache.find ~uncap:false fn)
else
Misc.find_in_path (get_path_list ()) fn
with Not_found ->
!auto_include_callback Dir.find fn
let search_dirs dirs fn =
List.find_map
(fun dir ->
Option.map
(fun path -> (path, Dir.visibility dir))
(Dir.find_normalized dir fn))
dirs
let find_normalized_with_visibility fn =
assert (not Config.merlin || Local_store.is_bound ());
try
if is_basename fn && not !Sys.interactive then
Path_cache.find ~uncap:true fn
else
match search_dirs (List.rev !visible_dirs) fn with
| Some result -> result
| None ->
match search_dirs (List.rev !hidden_dirs) fn with
| Some result -> result
| None -> raise Not_found
with Not_found ->
let fn_uncap = String.uncapitalize_ascii fn in
(!auto_include_callback Dir.find_normalized fn_uncap,
Visible { cmx_guaranteed = false })
let find_normalized fn = fst (find_normalized_with_visibility fn)
let reset () = reset ()