@@ -8,6 +8,7 @@ use super::packages;
88use crate :: config;
99use crate :: helpers;
1010use ahash:: { AHashMap , AHashSet } ;
11+ use anyhow:: { anyhow, Result } ;
1112use console:: style;
1213use log:: { debug, log_enabled, trace, Level :: Info } ;
1314use rayon:: prelude:: * ;
@@ -225,106 +226,100 @@ pub fn compile(
225226 }
226227 } )
227228 } )
228- . collect :: < Vec <
229- Option < (
230- String ,
231- Result < Option < String > , String > ,
232- Option < Result < Option < String > , String > > ,
233- bool ,
234- bool ,
235- ) > ,
236- > > ( )
229+ . collect :: < Vec < _ > > ( )
237230 . iter ( )
238- . for_each ( |result| if let Some ( ( module_name, result, interface_result, is_clean, is_compiled) ) = result {
239- in_progress_modules. remove ( module_name) ;
231+ . for_each ( |result| {
232+ if let Some ( ( module_name, result, interface_result, is_clean, is_compiled) ) = result {
233+ in_progress_modules. remove ( module_name) ;
240234
241- if * is_compiled {
242- num_compiled_modules += 1 ;
243- }
235+ if * is_compiled {
236+ num_compiled_modules += 1 ;
237+ }
244238
245- files_current_loop_count += 1 ;
246- compiled_modules. insert ( module_name. to_string ( ) ) ;
239+ files_current_loop_count += 1 ;
240+ compiled_modules. insert ( module_name. to_string ( ) ) ;
247241
248- if * is_clean {
249- // actually add it to a list of clean modules
250- clean_modules. insert ( module_name. to_string ( ) ) ;
251- }
242+ if * is_clean {
243+ // actually add it to a list of clean modules
244+ clean_modules. insert ( module_name. to_string ( ) ) ;
245+ }
252246
253- let module_dependents = build_state. get_module ( module_name) . unwrap ( ) . dependents . clone ( ) ;
247+ let module_dependents = build_state. get_module ( module_name) . unwrap ( ) . dependents . clone ( ) ;
254248
255- // if not clean -- compile modules that depend on this module
256- for dep in module_dependents. iter ( ) {
257- // mark the reverse dep as dirty when the source is not clean
258- if !* is_clean {
259- let dep_module = build_state. modules . get_mut ( dep) . unwrap ( ) ;
249+ // if not clean -- compile modules that depend on this module
250+ for dep in module_dependents. iter ( ) {
260251 // mark the reverse dep as dirty when the source is not clean
261- dep_module. compile_dirty = true ;
262- }
263- if !compiled_modules. contains ( dep) {
264- in_progress_modules. insert ( dep. to_string ( ) ) ;
252+ if !* is_clean {
253+ let dep_module = build_state. modules . get_mut ( dep) . unwrap ( ) ;
254+ // mark the reverse dep as dirty when the source is not clean
255+ dep_module. compile_dirty = true ;
256+ }
257+ if !compiled_modules. contains ( dep) {
258+ in_progress_modules. insert ( dep. to_string ( ) ) ;
259+ }
265260 }
266- }
267261
268- let module = build_state. modules . get_mut ( module_name) . unwrap ( ) ;
269- let package = build_state
270- . packages
271- . get ( & module. package_name )
272- . expect ( "Package not found" ) ;
273- match module. source_type {
274- SourceType :: MlMap ( ref mut mlmap) => {
275- module. compile_dirty = false ;
276- mlmap. parse_dirty = false ;
277- }
278- SourceType :: SourceFile ( ref mut source_file) => {
279- match result {
280- Ok ( Some ( err) ) => {
281- source_file. implementation . compile_state = CompileState :: Warning ;
282- logs:: append ( package, err) ;
283- compile_warnings. push_str ( err) ;
284- }
285- Ok ( None ) => {
286- source_file. implementation . compile_state = CompileState :: Success ;
287- }
288- Err ( err) => {
289- source_file. implementation . compile_state = CompileState :: Error ;
290- logs:: append ( package, err) ;
291- compile_errors. push_str ( err) ;
292- }
293- } ;
294- match interface_result {
295- Some ( Ok ( Some ( err) ) ) => {
296- source_file. interface . as_mut ( ) . unwrap ( ) . compile_state =
297- CompileState :: Warning ;
298- logs:: append ( package, err) ;
299- compile_warnings. push_str ( err) ;
300- }
301- Some ( Ok ( None ) ) => {
302- if let Some ( interface) = source_file. interface . as_mut ( ) {
303- interface. compile_state = CompileState :: Success ;
262+ let module = build_state. modules . get_mut ( module_name) . unwrap ( ) ;
263+ let package = build_state
264+ . packages
265+ . get ( & module. package_name )
266+ . expect ( "Package not found" ) ;
267+ match module. source_type {
268+ SourceType :: MlMap ( ref mut mlmap) => {
269+ module. compile_dirty = false ;
270+ mlmap. parse_dirty = false ;
271+ }
272+ SourceType :: SourceFile ( ref mut source_file) => {
273+ match result {
274+ Ok ( Some ( err) ) => {
275+ source_file. implementation . compile_state = CompileState :: Warning ;
276+ logs:: append ( package, err) ;
277+ compile_warnings. push_str ( err) ;
278+ }
279+ Ok ( None ) => {
280+ source_file. implementation . compile_state = CompileState :: Success ;
281+ }
282+ Err ( err) => {
283+ source_file. implementation . compile_state = CompileState :: Error ;
284+ logs:: append ( package, & err. to_string ( ) ) ;
285+ compile_errors. push_str ( & err. to_string ( ) ) ;
286+ }
287+ } ;
288+ match interface_result {
289+ Some ( Ok ( Some ( err) ) ) => {
290+ source_file. interface . as_mut ( ) . unwrap ( ) . compile_state =
291+ CompileState :: Warning ;
292+ logs:: append ( package, & err. to_string ( ) ) ;
293+ compile_warnings. push_str ( & err. to_string ( ) ) ;
294+ }
295+ Some ( Ok ( None ) ) => {
296+ if let Some ( interface) = source_file. interface . as_mut ( ) {
297+ interface. compile_state = CompileState :: Success ;
298+ }
304299 }
305- }
306300
307- Some ( Err ( err) ) => {
308- source_file. interface . as_mut ( ) . unwrap ( ) . compile_state =
309- CompileState :: Error ;
310- logs:: append ( package, err) ;
311- compile_errors. push_str ( err) ;
312- }
313- _ => ( ) ,
314- } ;
315- match ( result, interface_result) {
316- // successfull compilation
317- ( Ok ( None ) , Some ( Ok ( None ) ) ) | ( Ok ( None ) , None ) => {
318- module. compile_dirty = false ;
319- module. last_compiled_cmi = Some ( SystemTime :: now ( ) ) ;
320- module. last_compiled_cmt = Some ( SystemTime :: now ( ) ) ;
321- }
322- // some error or warning
323- ( Err ( _) , _)
324- | ( _, Some ( Err ( _) ) )
325- | ( Ok ( Some ( _) ) , _)
326- | ( _, Some ( Ok ( Some ( _) ) ) ) => {
327- module. compile_dirty = true ;
301+ Some ( Err ( err) ) => {
302+ source_file. interface . as_mut ( ) . unwrap ( ) . compile_state =
303+ CompileState :: Error ;
304+ logs:: append ( package, & err. to_string ( ) ) ;
305+ compile_errors. push_str ( & err. to_string ( ) ) ;
306+ }
307+ _ => ( ) ,
308+ } ;
309+ match ( result, interface_result) {
310+ // successfull compilation
311+ ( Ok ( None ) , Some ( Ok ( None ) ) ) | ( Ok ( None ) , None ) => {
312+ module. compile_dirty = false ;
313+ module. last_compiled_cmi = Some ( SystemTime :: now ( ) ) ;
314+ module. last_compiled_cmt = Some ( SystemTime :: now ( ) ) ;
315+ }
316+ // some error or warning
317+ ( Err ( _) , _)
318+ | ( _, Some ( Err ( _) ) )
319+ | ( Ok ( Some ( _) ) , _)
320+ | ( _, Some ( Ok ( Some ( _) ) ) ) => {
321+ module. compile_dirty = true ;
322+ }
328323 }
329324 }
330325 }
@@ -523,12 +518,16 @@ fn compile_file(
523518 packages : & AHashMap < String , packages:: Package > ,
524519 project_root : & str ,
525520 workspace_root : & Option < String > ,
526- ) -> Result < Option < String > , String > {
521+ ) -> Result < Option < String > > {
527522 let build_path_abs = package. get_build_path ( ) ;
528- let implementation_file_path = match module. source_type {
529- SourceType :: SourceFile ( ref source_file) => & source_file. implementation . path ,
530- _ => panic ! ( "Not a source file" ) ,
531- } ;
523+ let implementation_file_path = match & module. source_type {
524+ SourceType :: SourceFile ( ref source_file) => Ok ( & source_file. implementation . path ) ,
525+ sourcetype => Err ( anyhow ! (
526+ "Tried to compile a file that is not a source file ({}). Path to AST: {}. " ,
527+ sourcetype,
528+ ast_path
529+ ) ) ,
530+ } ?;
532531 let module_name = helpers:: file_path_to_module_name ( implementation_file_path, & package. namespace ) ;
533532 let has_interface = module. get_interface ( ) . is_some ( ) ;
534533 let to_mjs_args = compiler_args (
@@ -553,9 +552,9 @@ fn compile_file(
553552 Ok ( x) if !x. status . success ( ) => {
554553 let stderr = String :: from_utf8_lossy ( & x. stderr ) ;
555554 let stdout = String :: from_utf8_lossy ( & x. stdout ) ;
556- Err ( stderr. to_string ( ) + & stdout)
555+ Err ( anyhow ! ( stderr. to_string( ) + & stdout) )
557556 }
558- Err ( e) => Err ( format ! ( "ERROR, {}, {:?}" , e, ast_path) ) ,
557+ Err ( e) => Err ( anyhow ! ( "Could not compile file. Error: {}. Path to AST: {:?}" , e, ast_path) ) ,
559558 Ok ( x) => {
560559 let err = std:: str:: from_utf8 ( & x. stderr )
561560 . expect ( "stdout should be non-null" )
0 commit comments