@@ -44,16 +44,23 @@ impl MotokoBuilder {
44
44
}
45
45
46
46
// TODO: Rename this function.
47
- #[ context( "Failed to find imports for canister at '{}'." , info. get_main_path( ) . display( ) ) ]
48
- fn get_imports ( cache : & dyn Cache , info : & MotokoCanisterInfo , imports : & mut ImportsTracker , pool : & CanisterPool ) -> DfxResult < ( ) > {
47
+ // TODO: Is `unwrap()` in the next line correct?
48
+ #[ context( "Failed to find imports for canister at '{}'." , info. as_info:: <MotokoCanisterInfo >( ) . unwrap( ) . get_main_path( ) . display( ) ) ]
49
+ fn get_imports ( cache : & dyn Cache , info : & CanisterInfo , imports : & mut ImportsTracker , pool : & CanisterPool ) -> DfxResult < ( ) > {
50
+ let motoko_info = info. as_info :: < MotokoCanisterInfo > ( ) ?;
49
51
#[ context( "Failed recursive dependency detection at {}." , file. display( ) ) ]
50
52
fn get_imports_recursive (
51
53
cache : & dyn Cache ,
52
54
file : & Path ,
53
55
imports : & mut ImportsTracker ,
54
56
pool : & CanisterPool ,
57
+ top : Option < & CanisterInfo > , // hackish
55
58
) -> DfxResult {
56
- let parent = MotokoImport :: Relative ( file. to_path_buf ( ) ) ;
59
+ let parent = if let Some ( top) = top {
60
+ MotokoImport :: Canister ( top. get_name ( ) . to_string ( ) ) // a little inefficient
61
+ } else {
62
+ MotokoImport :: Relative ( file. to_path_buf ( ) )
63
+ } ;
57
64
if imports. nodes . contains_key ( & parent) {
58
65
return Ok ( ( ) ) ;
59
66
}
@@ -71,13 +78,13 @@ fn get_imports(cache: &dyn Cache, info: &MotokoCanisterInfo, imports: &mut Impor
71
78
let child = MotokoImport :: try_from ( line) . context ( "Failed to create MotokoImport." ) ?;
72
79
match & child {
73
80
MotokoImport :: Relative ( path) => {
74
- get_imports_recursive ( cache, path. as_path ( ) , imports, pool) ?;
81
+ get_imports_recursive ( cache, path. as_path ( ) , imports, pool, None ) ?;
75
82
}
76
83
MotokoImport :: Canister ( canister_name) => { // duplicate code
77
84
if let Some ( canister) = pool. get_first_canister_with_name ( canister_name. as_str ( ) ) {
78
85
let main_file = canister. get_info ( ) . get_main_file ( ) ;
79
86
if let Some ( main_file) = main_file {
80
- get_imports_recursive ( cache, Path :: new ( main_file) , imports, pool) ?;
87
+ get_imports_recursive ( cache, Path :: new ( main_file) , imports, pool, None ) ?;
81
88
}
82
89
}
83
90
}
@@ -91,7 +98,7 @@ fn get_imports(cache: &dyn Cache, info: &MotokoCanisterInfo, imports: &mut Impor
91
98
Ok ( ( ) )
92
99
}
93
100
94
- get_imports_recursive ( cache, info . get_main_path ( ) , imports, pool) ?;
101
+ get_imports_recursive ( cache, motoko_info . get_main_path ( ) , imports, pool, Some ( info ) ) ?;
95
102
96
103
Ok ( ( ) )
97
104
}
@@ -103,8 +110,7 @@ impl CanisterBuilder for MotokoBuilder {
103
110
pool : & CanisterPool ,
104
111
info : & CanisterInfo ,
105
112
) -> DfxResult < Vec < CanisterId > > {
106
- let motoko_info = info. as_info :: < MotokoCanisterInfo > ( ) ?;
107
- get_imports ( self . cache . as_ref ( ) , & motoko_info, & mut * pool. imports . borrow_mut ( ) , pool) ?;
113
+ get_imports ( self . cache . as_ref ( ) , info, & mut * pool. imports . borrow_mut ( ) , pool) ?;
108
114
109
115
let graph = & pool. imports . borrow ( ) . graph ;
110
116
match petgraph:: algo:: toposort ( & pool. imports . borrow ( ) . graph , None ) {
@@ -177,7 +183,7 @@ impl CanisterBuilder for MotokoBuilder {
177
183
std:: fs:: create_dir_all ( idl_dir_path)
178
184
. with_context ( || format ! ( "Failed to create {}." , idl_dir_path. to_string_lossy( ) ) ) ?;
179
185
180
- get_imports ( cache. as_ref ( ) , & motoko_info , & mut * pool. imports . borrow_mut ( ) , pool) ?;
186
+ get_imports ( cache. as_ref ( ) , canister_info , & mut * pool. imports . borrow_mut ( ) , pool) ?;
181
187
182
188
// If the management canister is being imported, emit the candid file.
183
189
if pool. imports . borrow ( ) . nodes . contains_key ( & MotokoImport :: Ic ( "aaaaa-aa" . to_string ( ) ) )
0 commit comments