Source file BuildValidation.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
type render_summary = {
diag_file : string option;
diag_error_code : string option;
diag_hints : string list;
diag_line : int option;
diag_col : int option;
}
let range_into_problem_location ~(source : BuildCore.Io.file_object) range :
MlFront_Thunk.BuildWriters.Standard.problem_location list
BuildCore.Alacarte_xpromise_apparatus.Promise.t =
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
let* read_result = BuildCore.Io.read_all source in
match read_result with
| `Error _ | `ExceededSizeLimit _ -> return []
| `Content source_code ->
return
[
MlFront_Thunk.BuildWriters.Standard.
{
origin = Some (BuildCore.Io.file_origin source);
source = source_code;
range;
};
]
let find_first_duplicate compare projection consider =
let sorted =
List.sort
(fun (p_c1, _c1) (p_c2, _c2) -> compare p_c1 p_c2)
(List.map (fun c -> (projection c, c)) consider)
in
let rec aux = function
| [] -> None
| [ _single ] -> None
| (p_x, x) :: ((p_y, y) :: _ as rest) ->
if compare p_x p_y = 0 then Some (x, y) else aux rest
in
aux sorted
let verify_nonoverlapping_subpaths_for_getobject ~config ~fail ~initiator
~source (commands : MlFront_Thunk.ThunkAst.precommand_instance list) =
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
let rec aux acc (cmds : MlFront_Thunk.ThunkAst.precommand_instance list) =
match cmds with
| [] -> acc
| {
precommand = _range, GetObject { command_output; _ };
precommand_canonical_id = _;
}
:: rest -> begin
match command_output with
| range, OutputDir { dir; strip = _ } -> aux ((range, dir) :: acc) rest
| range, OutputFile file -> aux ((range, file) :: acc) rest
end
| _other_command :: rest -> aux acc rest
in
let getobject_destination_terms :
(Fmlib_parse.Position.range * MlFront_Thunk.ThunkCommand.evalable_term)
list =
aux [] commands
in
let getobject_destinations :
( (Fmlib_parse.Position.range * MlFront_Core.FilePath.t) list,
Fmlib_parse.Position.range
* string
* MlFront_Thunk.ThunkCommand.evalable_term )
result =
let ( let* ) = Result.bind in
List.fold_right
(fun (range, term) acc ->
let* acc = acc in
match BuildTask.eval_term_as_filepath ~config ~initiator term with
| Error e -> Error (range, e, term)
| Ok fp -> Ok ((range, fp) :: acc))
getobject_destination_terms (Ok [])
in
match getobject_destinations with
| Error (range, msg, term) ->
let* err = range_into_problem_location ~source range in
fail ~error_code:"6e3d1e38"
~cant_do:
(Printf.sprintf "evaluate the expression `%s`"
(MlFront_Thunk.ThunkCommand.evalable_term_to_shell term))
~because:msg ~error_locations:err ~recommendations:[] ()
| Ok getobject_destinations ->
match
MlFront_Core.FilePath.find_first_subpath_opt ~strict:() snd
getobject_destinations
with
| None -> return (Ok ())
| Some ((parentrange, parent), (childrange, child)) ->
let* error_locations1 = range_into_problem_location ~source parentrange in
let* error_locations2 = range_into_problem_location ~source childrange in
fail ~error_code:"6e3d1e38"
~cant_do:
(Format.asprintf "get the object `%a`" MlFront_Core.FilePath.pp child)
~because:
(Format.asprintf "the output overlaps with another output `%a`"
MlFront_Core.FilePath.pp parent)
~error_locations:(error_locations1 @ error_locations2)
~recommendations:
[
"Use the `install-object` command instead of `get-object` if you \
truly mean to squish multiple outputs into the same directory \
trees.";
]
()
let verify_unique_paths_for_objectcommands ~config ~fail ~initiator ~source
(commands : MlFront_Thunk.ThunkAst.precommand_instance list) =
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
let rec aux acc (cmds : MlFront_Thunk.ThunkAst.precommand_instance list) =
match cmds with
| [] -> acc
| {
precommand = _range, GetObject { command_output; _ };
precommand_canonical_id = _;
}
:: rest -> begin
match command_output with
| range, OutputDir { dir; strip = _ } ->
aux ((range, `Get, dir) :: acc) rest
| range, OutputFile file -> aux ((range, `Get, file) :: acc) rest
end
| {
precommand = _range, InstallObject { command_output; _ };
precommand_canonical_id = _;
}
:: rest -> begin
match command_output with
| range, OutputDir { dir; strip = _ } ->
aux ((range, `Install, dir) :: acc) rest
| range, OutputFile file -> aux ((range, `Install, file) :: acc) rest
end
| {
precommand = _range, GetBundle { command_output; _ };
precommand_canonical_id = _;
}
:: rest -> begin
match command_output with
| range, OutputDir { dir; strip = _ } ->
aux ((range, `GetBundle, dir) :: acc) rest
| range, OutputFile file -> aux ((range, `GetBundle, file) :: acc) rest
end
| {
precommand = _range, GetAsset { command_output; _ };
precommand_canonical_id = _;
}
:: rest -> begin
match command_output with
| range, OutputDir { dir; strip = _ } ->
aux ((range, `GetAsset, dir) :: acc) rest
| range, OutputFile file -> aux ((range, `GetAsset, file) :: acc) rest
end
| {
precommand = _range, (PipeObject _ | EnterObject _);
precommand_canonical_id = _;
}
:: rest ->
aux acc rest
in
let object_destination_terms :
(Fmlib_parse.Position.range
* _
* MlFront_Thunk.ThunkCommand.evalable_term)
list =
aux [] commands
in
let object_destination_terms =
List.sort_uniq
(fun (i1, (_range1, typ1, _term1)) (i2, (_range2, typ2, _term2)) ->
match (typ1, typ2) with
| `Install, `Install -> 0
| _ ->
i2 - i1)
(List.mapi (fun i v -> (i, v)) object_destination_terms)
|> List.map snd
in
let object_destinations :
( (Fmlib_parse.Position.range * _ * MlFront_Core.FilePath.t) list,
Fmlib_parse.Position.range
* string
* MlFront_Thunk.ThunkCommand.evalable_term )
result =
let ( let* ) = Result.bind in
List.fold_right
(fun (range, typ, term) acc ->
let* acc = acc in
match BuildTask.eval_term_as_filepath ~config ~initiator term with
| Error e -> Error (range, e, term)
| Ok fp -> Ok ((range, typ, fp) :: acc))
object_destination_terms (Ok [])
in
match object_destinations with
| Error (range, msg, term) ->
let* err = range_into_problem_location ~source range in
fail ~error_code:"ed248cd8"
~cant_do:
(Printf.sprintf "evaluate the expression `%s`"
(MlFront_Thunk.ThunkCommand.evalable_term_to_shell term))
~because:msg ~error_locations:err ~recommendations:[] ()
| Ok object_destinations ->
match
find_first_duplicate MlFront_Core.FilePath.compare
(fun (_, _, fp) -> fp)
object_destinations
with
| None -> return (Ok ())
| Some ((onerange, onetyp, _one), (anotherrange, _anothertyp, another)) ->
let* error_locations1 = range_into_problem_location ~source onerange in
let* error_locations2 =
range_into_problem_location ~source anotherrange
in
fail ~error_code:"e0067628"
~cant_do:
(match onetyp with
| `Get -> "get object"
| `Install -> "install object"
| `GetAsset -> "get asset"
| `GetBundle -> "get bundle"
| `Resume -> "resume object")
~because:
(Format.asprintf "two commands share the same output path `%a`"
MlFront_Core.FilePath.pp another)
~error_locations:(error_locations1 @ error_locations2)
~recommendations:[] ()
let verify_unique_pipes_for_objectcommands ~fail ~source
(commands : MlFront_Thunk.ThunkAst.precommand_instance list) =
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
let rec aux acc (cmds : MlFront_Thunk.ThunkAst.precommand_instance list) =
match cmds with
| [] -> acc
| {
precommand_canonical_id = _;
precommand = _range, PipeObject { pipe = range, pipename; _ };
}
:: rest ->
aux ((range, `Pipe, pipename) :: acc) rest
| {
precommand_canonical_id = _;
precommand =
( _range,
( GetObject _ | InstallObject _ | EnterObject _ | GetBundle _
| GetAsset _ ) );
}
:: rest ->
aux acc rest
in
let pipe_terms : (Fmlib_parse.Position.range * _ * string) list =
aux [] commands
in
match
find_first_duplicate String.compare
(fun (_, _typ, pipeline) -> pipeline)
pipe_terms
with
| None -> return (Ok ())
| Some ((onerange, onetyp, _one), (anotherrange, _anothertyp, another)) ->
let* error_locations1 = range_into_problem_location ~source onerange in
let* error_locations2 =
range_into_problem_location ~source anotherrange
in
fail ~error_code:"cc40063b"
~cant_do:(match onetyp with `Pipe -> "pipe object")
~because:
(Format.asprintf "two commands share the same pipe `%s`" another)
~error_locations:(error_locations1 @ error_locations2)
~recommendations:[] ()
let render_trace ?downgrade_errors_into_warnings ~cant_do
~(observer_result :
(module MlFront_Thunk.ThunkParsers.Results.OBSERVER_RESULT)) ~source_file
trace =
let module ResultObserver = (val observer_result) in
let rootcause_origin, rootcause_ranges, rootcause_source, rootcause_code =
let ranges0 = Fmlib_parse.Position.[ (start, start) ] in
let ( let* ) = BuildCore.Alacarte_xpromise_apparatus.Promise.bind in
let rec aux ((origin, ranges, (source : string option), _item) as acc) =
function
| [] -> BuildCore.Alacarte_xpromise_apparatus.Promise.return acc
| first_item :: rest_items ->
let open MlFront_Thunk.BuildWriters.Standard in
let origin', range', source' =
match problem_location_backtrace_item first_item with
| [] -> (origin, ranges, source)
| { origin; range; source } :: rest
when List.for_all
(fun problem ->
String.equal (problem_source problem) source)
rest ->
(origin, range :: List.map problem_range rest, Some source)
| { origin; range; source } :: _rest ->
(origin, [ range ], Some source)
in
aux
(origin', range', source', Some (code_backtrace_item first_item))
rest_items
in
let promise =
let* source0_result = BuildCore.Io.read_all source_file in
let source0 =
match source0_result with
| `Error _ | `ExceededSizeLimit _ -> None
| `Content source_code -> Some source_code
in
aux (None, ranges0, source0, None) trace
in
BuildInstance.Launcher.run_isolated_promise promise
in
let marker_message =
let errors =
List.filter_map
(fun item ->
match item with
| MlFront_Thunk.BuildWriters.Standard.InfoBacktraceItem' _ -> None
| MlFront_Thunk.BuildWriters.Standard.ErrorBacktraceItem'
{
error_code = _;
cant_do;
because;
error_locations = _;
recommendations = _;
} ->
Some
(Printf.sprintf "Could not %s%s." cant_do
(if because = "" then "" else " because " ^ because)))
trace
in
match errors with
| [] -> "Could not recover the error."
| _ -> String.concat "\n" errors
in
let summary =
let maybe_first_range =
match rootcause_ranges with
| [] -> None
| first_range :: _rest -> Some first_range
in
let rootcause_line, rootcause_col =
match maybe_first_range with
| None -> (None, None)
| Some (start, _end_) ->
( Some (1 + Fmlib_parse.Position.line start),
Some (1 + Fmlib_parse.Position.column start) )
in
{
diag_file = rootcause_origin;
diag_error_code = rootcause_code;
diag_hints = [];
diag_line = rootcause_line;
diag_col = rootcause_col;
}
in
let r =
ResultObserver.(
create_report ?code:rootcause_code
~is_error:(downgrade_errors_into_warnings = None)
()
|> with_message (Printf.sprintf "Could not %s." cant_do))
in
let r =
List.fold_left
(fun r range ->
ResultObserver.add_marker ~marker_message ~origin:rootcause_origin
~range r)
r rootcause_ranges
in
let r, summary =
List.fold_left
(fun (acc_r, acc_summary) item ->
match item with
| MlFront_Thunk.BuildWriters.Standard.InfoBacktraceItem'
{ info_code = _; message; info_locations = _ } ->
( ResultObserver.add_hint message acc_r,
{
acc_summary with
diag_hints = message :: acc_summary.diag_hints;
} )
| MlFront_Thunk.BuildWriters.Standard.ErrorBacktraceItem'
{
error_code = _;
cant_do = _;
because = _;
error_locations = _;
recommendations;
} ->
List.fold_left
(fun (acc2_r, acc2_summary) recommendation ->
( ResultObserver.add_hint recommendation acc2_r,
{
acc2_summary with
diag_hints = recommendation :: acc2_summary.diag_hints;
} ))
(acc_r, acc_summary) recommendations)
(r, summary) trace
in
let rendered =
ResultObserver.render ~origin:rootcause_origin
~source:
(Option.value ~default:"<source code is too big>" rootcause_source)
r
in
(rendered, summary)
let validate_precommands ?downgrade_errors_into_warnings ~config ~source
(commands : MlFront_Thunk.ThunkAst.precommand_instance list) =
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
let fail ~error_code ~cant_do ~because ~error_locations ~recommendations () =
let observer_result = BuildConfig.observer_result config in
let traceitem : MlFront_Thunk.BuildWriters.Standard.backtrace_item =
MlFront_Thunk.BuildWriters.Standard.ErrorBacktraceItem'
{ error_code; cant_do; because; error_locations; recommendations }
in
let rendered, _summary =
render_trace ?downgrade_errors_into_warnings ~cant_do ~observer_result
~source_file:source [ traceitem ]
in
return (Error rendered)
in
let mock_initiator =
BuildTask.CommandInitiated
{
cid_command = "01";
cid_thunk_for_output = "02";
basedir = None;
request_slot = None;
}
in
let* verify_result =
verify_nonoverlapping_subpaths_for_getobject ~config ~fail
~initiator:mock_initiator ~source commands
in
match verify_result with
| Error _ as e -> return e
| Ok () -> (
let* verify_result =
verify_unique_paths_for_objectcommands ~config ~fail
~initiator:mock_initiator ~source commands
in
match verify_result with
| Error _ as e -> return e
| Ok () -> verify_unique_pipes_for_objectcommands ~fail ~source commands)