Skip to content

Commit ab71851

Browse files
committed
bug fix
1 parent 891496d commit ab71851

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/dfx/src/lib/builders/motoko.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ fn get_imports(cache: &dyn Cache, info: &MotokoCanisterInfo, imports: &mut Impor
5353
imports: &mut ImportsTracker,
5454
pool: &CanisterPool,
5555
) -> DfxResult {
56-
println!("CanisterInfo: {:#?}", file);
5756
let parent = MotokoImport::Relative(file.to_path_buf());
5857
if imports.nodes.contains_key(&parent) {
5958
return Ok(());
6059
}
61-
imports.nodes.insert(parent.clone(), ());
60+
let parent_node_index = *imports.nodes.entry(parent.clone()).or_insert_with(|| imports.graph.add_node(parent.clone()));
61+
imports.nodes.insert(parent.clone(), parent_node_index);
6262
if let MotokoImport::Relative(path) = &parent {
6363
println!("INSERTED: {}", path.display()); // FIXME
6464
}
@@ -86,18 +86,21 @@ fn get_imports(cache: &dyn Cache, info: &MotokoCanisterInfo, imports: &mut Impor
8686
}
8787
_ => {}
8888
}
89-
let parent_node = imports.graph.add_node(parent.clone());
90-
if let MotokoImport::Relative(child) = &child {
91-
println!("INSERTED CHILD: {}", child.display()); // FIXME
92-
}
93-
let child_node = imports.graph.add_node(child);
94-
imports.graph.add_edge(parent_node, child_node, ());
89+
let parent_node_index = *imports.nodes.entry(parent.clone()).or_insert_with(|| imports.graph.add_node(parent.clone()));
90+
let child_node_index = *imports.nodes.entry(child.clone()).or_insert_with(|| imports.graph.add_node(child.clone()));
91+
// if let MotokoImport::Relative(parent) = &parent {
92+
// println!("INSERTED PARENT: {} ({:?})", parent.display(), parent_node); // FIXME
93+
// }
94+
// let child_node = imports.graph.add_node(child.clone());
95+
// if let MotokoImport::Relative(child) = &child {
96+
// println!("INSERTED CHILD: {} ({:?})", child.display(), child_node); // FIXME
97+
// }
98+
imports.graph.add_edge(parent_node_index, child_node_index, ());
9599
}
96100

97101
Ok(())
98102
}
99103

100-
println!("CanisterInfo2: {:#?}", info.get_main_path());
101104
get_imports_recursive(cache, info.get_main_path(), imports, pool)?;
102105

103106
Ok(())
@@ -136,7 +139,6 @@ impl CanisterBuilder for MotokoBuilder {
136139
config: &BuildConfig,
137140
) -> DfxResult<BuildOutput> {
138141
let motoko_info = canister_info.as_info::<MotokoCanisterInfo>()?;
139-
println!("motoko_info: {}", motoko_info.get_main_path().display());
140142
let profile = config.profile;
141143
let input_path = motoko_info.get_main_path();
142144
let output_wasm_path = motoko_info.get_output_wasm_path();
@@ -193,10 +195,20 @@ impl CanisterBuilder for MotokoBuilder {
193195
if let Ok(wasm_file_metadata) = metadata(output_wasm_path) {
194196
let wasm_file_time = wasm_file_metadata.modified()?;
195197
let mut imports = pool.imports.borrow_mut();
196-
let start = imports.graph.add_node(MotokoImport::Relative(motoko_info.get_main_path().to_path_buf())); // Start with oput canister.
198+
println!("START: {}", motoko_info.get_main_path().to_path_buf().display()); // FIXME
199+
// let start = imports.graph.add_node(MotokoImport::Relative(motoko_info.get_main_path().to_path_buf())); // Start with oput canister.
200+
let start = if let Some(node_index) = imports.nodes.get(&MotokoImport::Relative(motoko_info.get_main_path().to_path_buf())) {
201+
*node_index
202+
} else {
203+
let node = MotokoImport::Relative(motoko_info.get_main_path().to_path_buf());
204+
let node_index = imports.graph.add_node(node.clone());
205+
imports.nodes.insert(node, node_index);
206+
node_index
207+
};
197208
let mut import_iter = Bfs::new(&imports.graph, start);
198209
loop {
199210
if let Some(import) = import_iter.next(&imports.graph) {
211+
println!("NodeIndex {:?}", import);
200212
let imported_file = match &imports.graph[import] {
201213
MotokoImport::Canister(canister_name) => { // duplicate code
202214
if let Some(canister) = pool.get_first_canister_with_name(canister_name.as_str()) {

src/dfx/src/lib/models/canister.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ pub enum MotokoImport {
446446

447447
/// The graph of Motoko imports (TODO: Motoko-specific code not here)
448448
pub struct ImportsTracker {
449-
pub nodes: HashMap<MotokoImport, ()>,
449+
pub nodes: HashMap<MotokoImport, NodeIndex>,
450450
pub graph: DiGraph<MotokoImport, ()>,
451451
}
452452

@@ -483,7 +483,6 @@ impl CanisterPool {
483483
_ => None,
484484
};
485485
let info = CanisterInfo::load(pool_helper.config, canister_name, canister_id)?;
486-
// println!("CanisterInfo: {:#?}", info);
487486
let builder = pool_helper.builder_pool.get(&info);
488487
pool_helper
489488
.canisters_map

0 commit comments

Comments
 (0)