@@ -7,6 +7,7 @@ use crate::config::{Config, OneOrMore};
7
7
use crate :: helpers;
8
8
use crate :: project_context:: ProjectContext ;
9
9
use ahash:: AHashSet ;
10
+ use anyhow:: anyhow;
10
11
use log:: debug;
11
12
use rayon:: prelude:: * ;
12
13
use std:: path:: { Path , PathBuf } ;
@@ -51,11 +52,13 @@ pub fn generate_asts(
51
52
package. to_owned ( ) ,
52
53
& source_file. implementation . path . to_owned ( ) ,
53
54
build_state,
54
- ) ;
55
+ )
56
+ . map_err ( |e| e. to_string ( ) ) ;
55
57
56
58
let iast_result = match source_file. interface . as_ref ( ) . map ( |i| i. path . to_owned ( ) ) {
57
59
Some ( interface_file_path) => {
58
60
generate_ast ( package. to_owned ( ) , & interface_file_path. to_owned ( ) , build_state)
61
+ . map_err ( |e| e. to_string ( ) )
59
62
. map ( Some )
60
63
}
61
64
_ => Ok ( None ) ,
@@ -237,16 +240,15 @@ pub fn parser_args(
237
240
package_config : & Config ,
238
241
filename : & Path ,
239
242
contents : & str ,
240
- ) -> ( PathBuf , Vec < String > ) {
243
+ ) -> anyhow :: Result < ( PathBuf , Vec < String > ) > {
241
244
let root_config = project_context. get_root_config ( ) ;
242
245
let file = & filename;
243
246
let ast_path = helpers:: get_ast_path ( file) ;
244
- let node_modules_path = project_context. get_root_path ( ) . join ( "node_modules" ) ;
245
247
let ppx_flags = config:: flatten_ppx_flags (
246
- node_modules_path. as_path ( ) ,
248
+ project_context,
249
+ package_config,
247
250
& filter_ppx_flags ( & package_config. ppx_flags , contents) ,
248
- & package_config. name ,
249
- ) ;
251
+ ) ?;
250
252
let jsx_args = root_config. get_jsx_args ( ) ;
251
253
let jsx_module_args = root_config. get_jsx_module_args ( ) ;
252
254
let jsx_mode_args = root_config. get_jsx_mode_args ( ) ;
@@ -255,7 +257,7 @@ pub fn parser_args(
255
257
256
258
let file = PathBuf :: from ( ".." ) . join ( ".." ) . join ( file) ;
257
259
258
- (
260
+ Ok ( (
259
261
ast_path. to_owned ( ) ,
260
262
[
261
263
ppx_flags,
@@ -273,20 +275,20 @@ pub fn parser_args(
273
275
] ,
274
276
]
275
277
. concat ( ) ,
276
- )
278
+ ) )
277
279
}
278
280
279
281
fn generate_ast (
280
282
package : Package ,
281
283
filename : & Path ,
282
284
build_state : & BuildState ,
283
- ) -> Result < ( PathBuf , Option < helpers:: StdErr > ) , String > {
285
+ ) -> anyhow :: Result < ( PathBuf , Option < helpers:: StdErr > ) > {
284
286
let file_path = PathBuf :: from ( & package. path ) . join ( filename) ;
285
287
let contents = helpers:: read_file ( & file_path) . expect ( "Error reading file" ) ;
286
288
287
289
let build_path_abs = package. get_build_path ( ) ;
288
290
let ( ast_path, parser_args) =
289
- parser_args ( & build_state. project_context , & package. config , filename, & contents) ;
291
+ parser_args ( & build_state. project_context , & package. config , filename, & contents) ? ;
290
292
291
293
// generate the dir of the ast_path (it mirrors the source file dir)
292
294
let ast_parent_path = package. get_build_path ( ) . join ( ast_path. parent ( ) . unwrap ( ) ) ;
@@ -298,7 +300,13 @@ fn generate_ast(
298
300
. current_dir ( & build_path_abs)
299
301
. args ( parser_args)
300
302
. output ( )
301
- . expect ( "Error converting .res to .ast" ) ,
303
+ . map_err ( |e| {
304
+ anyhow ! (
305
+ "Error running bsc for parsing {}: {}" ,
306
+ filename. to_string_lossy( ) ,
307
+ e
308
+ )
309
+ } ) ?,
302
310
) {
303
311
Some ( res_to_ast) => {
304
312
let stderr = String :: from_utf8_lossy ( & res_to_ast. stderr ) . to_string ( ) ;
@@ -307,7 +315,7 @@ fn generate_ast(
307
315
if res_to_ast. status . success ( ) {
308
316
Ok ( ( ast_path, Some ( stderr. to_string ( ) ) ) )
309
317
} else {
310
- Err ( format ! ( "Error in {}:\n {}" , package. name, stderr) )
318
+ Err ( anyhow ! ( "Error in {}:\n {}" , package. name, stderr) )
311
319
}
312
320
} else {
313
321
Ok ( ( ast_path, None ) )
@@ -316,7 +324,7 @@ fn generate_ast(
316
324
_ => {
317
325
log:: info!( "Parsing file {}..." , filename. display( ) ) ;
318
326
319
- Err ( format ! (
327
+ Err ( anyhow ! (
320
328
"Could not find canonicalize_string_path for file {} in package {}" ,
321
329
filename. display( ) ,
322
330
package. name
0 commit comments