@@ -16,6 +16,7 @@ use dfx_core::config::cache::Cache;
16
16
use dfx_core:: config:: model:: dfinity:: { MetadataVisibility , Profile } ;
17
17
use dfx_core:: fs:: metadata;
18
18
use fn_error_context:: context;
19
+ use petgraph:: visit:: Bfs ;
19
20
use slog:: { info, o, trace, warn, Logger } ;
20
21
use std:: collections:: BTreeMap ;
21
22
use std:: convert:: TryFrom ;
@@ -45,7 +46,7 @@ impl MotokoBuilder {
45
46
// TODO: Rename this function.
46
47
#[ context( "Failed to find imports for canister at '{}'." , info. get_main_path( ) . display( ) ) ]
47
48
fn get_imports ( cache : & dyn Cache , info : & MotokoCanisterInfo , imports : & mut ImportsTracker ) -> DfxResult < ( ) > {
48
- #[ context( "Failed recursive dependency detection at {}." , file. display( ) ) ] // FIXME
49
+ #[ context( "Failed recursive dependency detection at {}." , file. display( ) ) ]
49
50
fn get_imports_recursive (
50
51
cache : & dyn Cache ,
51
52
file : & Path ,
@@ -94,7 +95,7 @@ impl CanisterBuilder for MotokoBuilder {
94
95
info : & CanisterInfo ,
95
96
) -> DfxResult < Vec < CanisterId > > {
96
97
let motoko_info = info. as_info :: < MotokoCanisterInfo > ( ) ?;
97
- get_imports ( self . cache . as_ref ( ) , & motoko_info, & mut * pool. imports . borrow_mut ( ) ) ?; // TODO: slow operation
98
+ get_imports ( self . cache . as_ref ( ) , & motoko_info, & mut * pool. imports . borrow_mut ( ) ) ?;
98
99
99
100
Ok ( pool. imports . borrow ( ) . nodes
100
101
. iter ( )
@@ -145,7 +146,7 @@ impl CanisterBuilder for MotokoBuilder {
145
146
std:: fs:: create_dir_all ( idl_dir_path)
146
147
. with_context ( || format ! ( "Failed to create {}." , idl_dir_path. to_string_lossy( ) ) ) ?;
147
148
148
- get_imports ( cache. as_ref ( ) , & motoko_info, & mut * pool. imports . borrow_mut ( ) ) ?; // TODO: repeated slow operation
149
+ get_imports ( cache. as_ref ( ) , & motoko_info, & mut * pool. imports . borrow_mut ( ) ) ?;
149
150
150
151
// If the management canister is being imported, emit the candid file.
151
152
if pool. imports . borrow ( ) . nodes . contains_key ( & MotokoImport :: Ic ( "aaaaa-aa" . to_string ( ) ) )
@@ -172,11 +173,12 @@ impl CanisterBuilder for MotokoBuilder {
172
173
// Check that one of the dependencies is newer than the target:
173
174
if let Ok ( wasm_file_metadata) = metadata ( output_wasm_path) {
174
175
let wasm_file_time = wasm_file_metadata. modified ( ) ?;
175
- let imports = pool. imports . borrow ( ) ;
176
- let mut import_iter = imports. nodes . iter ( ) ;
176
+ let mut imports = pool. imports . borrow_mut ( ) ;
177
+ let start = imports. graph . add_node ( MotokoImport :: Relative ( motoko_info. get_main_path ( ) . to_path_buf ( ) ) ) ; // Start with oput canister.
178
+ let mut import_iter = Bfs :: new ( & imports. graph , start) ;
177
179
loop {
178
- if let Some ( import) = import_iter. next ( ) {
179
- let imported_file = match import . 0 {
180
+ if let Some ( import) = import_iter. next ( & imports . graph ) {
181
+ let imported_file = match & imports . graph [ import ] {
180
182
MotokoImport :: Canister ( canister_name) => {
181
183
if let Some ( canister) = pool. get_first_canister_with_name ( canister_name. as_str ( ) ) {
182
184
let main_file = canister. get_info ( ) . get_main_file ( ) ;
@@ -214,7 +216,7 @@ impl CanisterBuilder for MotokoBuilder {
214
216
} else {
215
217
Path :: new ( path. as_str ( ) ) . to_owned ( )
216
218
} ;
217
- let path2 = pre_path. to_string_lossy ( ) + ".mo" ; // TODO: Is `lossy` OK?
219
+ let path2 = pre_path. to_str ( ) . unwrap ( ) . to_owned ( ) + ".mo" ;
218
220
let path2 = path2. to_string ( ) ;
219
221
let path2 = Path :: new ( & path2) ;
220
222
if path2. exists ( ) { // TODO: Is it correct order of two variants?
@@ -240,7 +242,7 @@ impl CanisterBuilder for MotokoBuilder {
240
242
} ;
241
243
} ;
242
244
} else {
243
- // trace!(log, "Canister {} already compiled", canister_info.get_name()); // TODO
245
+ // println!( "Canister {} already compiled. ", canister_info.get_name()); // TODO
244
246
return Ok ( BuildOutput { // duplicate code
245
247
canister_id : canister_info
246
248
. get_canister_id ( )
0 commit comments