@@ -45,12 +45,13 @@ impl MotokoBuilder {
45
45
46
46
// TODO: Rename this function.
47
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 ) -> DfxResult < ( ) > {
48
+ fn get_imports ( cache : & dyn Cache , info : & MotokoCanisterInfo , imports : & mut ImportsTracker , pool : & CanisterPool ) -> DfxResult < ( ) > {
49
49
#[ context( "Failed recursive dependency detection at {}." , file. display( ) ) ]
50
50
fn get_imports_recursive (
51
51
cache : & dyn Cache ,
52
52
file : & Path ,
53
53
imports : & mut ImportsTracker ,
54
+ pool : & CanisterPool ,
54
55
) -> DfxResult {
55
56
let parent = MotokoImport :: Relative ( file. to_path_buf ( ) ) ;
56
57
if imports. nodes . contains_key ( & parent) {
@@ -67,10 +68,17 @@ fn get_imports(cache: &dyn Cache, info: &MotokoCanisterInfo, imports: &mut Impor
67
68
68
69
for line in output. lines ( ) {
69
70
let child = MotokoImport :: try_from ( line) . context ( "Failed to create MotokoImport." ) ?;
70
- // TODO: The code seems screwed: Why recompile onluy on `Relative`?
71
71
match & child {
72
72
MotokoImport :: Relative ( path) => {
73
- get_imports_recursive ( cache, path. as_path ( ) , imports) ?;
73
+ get_imports_recursive ( cache, path. as_path ( ) , imports, pool) ?;
74
+ }
75
+ MotokoImport :: Canister ( canister_name) => { // duplicate code
76
+ if let Some ( canister) = pool. get_first_canister_with_name ( canister_name. as_str ( ) ) {
77
+ let main_file = canister. get_info ( ) . get_main_file ( ) ;
78
+ if let Some ( main_file) = main_file {
79
+ get_imports_recursive ( cache, Path :: new ( main_file) , imports, pool) ?;
80
+ }
81
+ }
74
82
}
75
83
_ => { }
76
84
}
@@ -82,7 +90,7 @@ fn get_imports(cache: &dyn Cache, info: &MotokoCanisterInfo, imports: &mut Impor
82
90
Ok ( ( ) )
83
91
}
84
92
85
- get_imports_recursive ( cache, info. get_main_path ( ) , imports) ?;
93
+ get_imports_recursive ( cache, info. get_main_path ( ) , imports, pool ) ?;
86
94
87
95
Ok ( ( ) )
88
96
}
@@ -95,7 +103,7 @@ impl CanisterBuilder for MotokoBuilder {
95
103
info : & CanisterInfo ,
96
104
) -> DfxResult < Vec < CanisterId > > {
97
105
let motoko_info = info. as_info :: < MotokoCanisterInfo > ( ) ?;
98
- get_imports ( self . cache . as_ref ( ) , & motoko_info, & mut * pool. imports . borrow_mut ( ) ) ?;
106
+ get_imports ( self . cache . as_ref ( ) , & motoko_info, & mut * pool. imports . borrow_mut ( ) , pool ) ?;
99
107
100
108
Ok ( pool. imports . borrow ( ) . nodes
101
109
. iter ( )
@@ -146,7 +154,7 @@ impl CanisterBuilder for MotokoBuilder {
146
154
std:: fs:: create_dir_all ( idl_dir_path)
147
155
. with_context ( || format ! ( "Failed to create {}." , idl_dir_path. to_string_lossy( ) ) ) ?;
148
156
149
- get_imports ( cache. as_ref ( ) , & motoko_info, & mut * pool. imports . borrow_mut ( ) ) ?;
157
+ get_imports ( cache. as_ref ( ) , & motoko_info, & mut * pool. imports . borrow_mut ( ) , pool ) ?;
150
158
151
159
// If the management canister is being imported, emit the candid file.
152
160
if pool. imports . borrow ( ) . nodes . contains_key ( & MotokoImport :: Ic ( "aaaaa-aa" . to_string ( ) ) )
@@ -179,7 +187,7 @@ impl CanisterBuilder for MotokoBuilder {
179
187
loop {
180
188
if let Some ( import) = import_iter. next ( & imports. graph ) {
181
189
let imported_file = match & imports. graph [ import] {
182
- MotokoImport :: Canister ( canister_name) => {
190
+ MotokoImport :: Canister ( canister_name) => { // duplicate code
183
191
if let Some ( canister) = pool. get_first_canister_with_name ( canister_name. as_str ( ) ) {
184
192
let main_file = canister. get_info ( ) . get_main_file ( ) ;
185
193
if let Some ( main_file) = main_file {
0 commit comments