@@ -7,6 +7,7 @@ use crate::config::{Config, OneOrMore};
77use crate :: helpers;
88use crate :: project_context:: ProjectContext ;
99use ahash:: AHashSet ;
10+ use anyhow:: anyhow;
1011use log:: debug;
1112use rayon:: prelude:: * ;
1213use std:: path:: { Path , PathBuf } ;
@@ -51,11 +52,13 @@ pub fn generate_asts(
5152 package. to_owned ( ) ,
5253 & source_file. implementation . path . to_owned ( ) ,
5354 build_state,
54- ) ;
55+ )
56+ . map_err ( |e| e. to_string ( ) ) ;
5557
5658 let iast_result = match source_file. interface . as_ref ( ) . map ( |i| i. path . to_owned ( ) ) {
5759 Some ( interface_file_path) => {
5860 generate_ast ( package. to_owned ( ) , & interface_file_path. to_owned ( ) , build_state)
61+ . map_err ( |e| e. to_string ( ) )
5962 . map ( Some )
6063 }
6164 _ => Ok ( None ) ,
@@ -237,16 +240,15 @@ pub fn parser_args(
237240 package_config : & Config ,
238241 filename : & Path ,
239242 contents : & str ,
240- ) -> ( PathBuf , Vec < String > ) {
243+ ) -> anyhow :: Result < ( PathBuf , Vec < String > ) > {
241244 let root_config = project_context. get_root_config ( ) ;
242245 let file = & filename;
243246 let ast_path = helpers:: get_ast_path ( file) ;
244- let node_modules_path = project_context. get_root_path ( ) . join ( "node_modules" ) ;
245247 let ppx_flags = config:: flatten_ppx_flags (
246- node_modules_path. as_path ( ) ,
248+ project_context,
249+ package_config,
247250 & filter_ppx_flags ( & package_config. ppx_flags , contents) ,
248- & package_config. name ,
249- ) ;
251+ ) ?;
250252 let jsx_args = root_config. get_jsx_args ( ) ;
251253 let jsx_module_args = root_config. get_jsx_module_args ( ) ;
252254 let jsx_mode_args = root_config. get_jsx_mode_args ( ) ;
@@ -255,7 +257,7 @@ pub fn parser_args(
255257
256258 let file = PathBuf :: from ( ".." ) . join ( ".." ) . join ( file) ;
257259
258- (
260+ Ok ( (
259261 ast_path. to_owned ( ) ,
260262 [
261263 ppx_flags,
@@ -273,20 +275,20 @@ pub fn parser_args(
273275 ] ,
274276 ]
275277 . concat ( ) ,
276- )
278+ ) )
277279}
278280
279281fn generate_ast (
280282 package : Package ,
281283 filename : & Path ,
282284 build_state : & BuildState ,
283- ) -> Result < ( PathBuf , Option < helpers:: StdErr > ) , String > {
285+ ) -> anyhow :: Result < ( PathBuf , Option < helpers:: StdErr > ) > {
284286 let file_path = PathBuf :: from ( & package. path ) . join ( filename) ;
285287 let contents = helpers:: read_file ( & file_path) . expect ( "Error reading file" ) ;
286288
287289 let build_path_abs = package. get_build_path ( ) ;
288290 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) ? ;
290292
291293 // generate the dir of the ast_path (it mirrors the source file dir)
292294 let ast_parent_path = package. get_build_path ( ) . join ( ast_path. parent ( ) . unwrap ( ) ) ;
@@ -298,7 +300,13 @@ fn generate_ast(
298300 . current_dir ( & build_path_abs)
299301 . args ( parser_args)
300302 . 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+ } ) ?,
302310 ) {
303311 Some ( res_to_ast) => {
304312 let stderr = String :: from_utf8_lossy ( & res_to_ast. stderr ) . to_string ( ) ;
@@ -307,7 +315,7 @@ fn generate_ast(
307315 if res_to_ast. status . success ( ) {
308316 Ok ( ( ast_path, Some ( stderr. to_string ( ) ) ) )
309317 } else {
310- Err ( format ! ( "Error in {}:\n {}" , package. name, stderr) )
318+ Err ( anyhow ! ( "Error in {}:\n {}" , package. name, stderr) )
311319 }
312320 } else {
313321 Ok ( ( ast_path, None ) )
@@ -316,7 +324,7 @@ fn generate_ast(
316324 _ => {
317325 log:: info!( "Parsing file {}..." , filename. display( ) ) ;
318326
319- Err ( format ! (
327+ Err ( anyhow ! (
320328 "Could not find canonicalize_string_path for file {} in package {}" ,
321329 filename. display( ) ,
322330 package. name
0 commit comments