Skip to content

Commit 67b2f63

Browse files
committed
trying to fix a bug
1 parent 3e74af6 commit 67b2f63

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

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

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,41 @@ impl CanisterBuilder for MotokoBuilder {
106106
let motoko_info = info.as_info::<MotokoCanisterInfo>()?;
107107
get_imports(self.cache.as_ref(), &motoko_info, &mut *pool.imports.borrow_mut(), pool)?;
108108

109-
Ok(pool.imports.borrow().nodes
110-
.iter()
111-
.filter_map(|import| {
112-
if let MotokoImport::Canister(name) = import.0 {
113-
pool.get_first_canister_with_name(name.as_str())
114-
} else {
115-
None
116-
}
117-
})
118-
.map(|canister| canister.canister_id())
119-
.collect())
109+
// let iter = Bfs::new(pool.imports.borrow().graph);
110+
match petgraph::algo::toposort(&pool.imports.borrow().graph, None) {
111+
Ok(order) => {
112+
let graph = &pool.imports.borrow().graph;
113+
Ok(order.into_iter().filter_map(|id| match graph.node_weight(id) {
114+
Some(MotokoImport::Canister(name)) => pool.get_first_canister_with_name(name.as_str()), // TODO: a little inefficient
115+
_ => None,
116+
}).map(|canister| canister.canister_id()).collect())
117+
}
118+
Err(err) => {
119+
let graph = &pool.imports.borrow().graph;
120+
let message = match graph.node_weight(err.node_id()) {
121+
Some(canister_id) => match canister_id {
122+
MotokoImport::Canister(name) => name.clone(), // TODO: Can deal without `clone()`?
123+
_ => "<Unknown>".to_string(),
124+
},
125+
None => "<Unknown>".to_string(),
126+
};
127+
return Err(DfxError::new(BuildError::DependencyError(format!(
128+
"Found circular dependency: {}",
129+
message
130+
))));
131+
}
132+
}
133+
// Ok(pool.imports.borrow().nodes
134+
// .iter()
135+
// .filter_map(|import| {
136+
// if let MotokoImport::Canister(name) = import.0 {
137+
// pool.get_first_canister_with_name(name.as_str())
138+
// } else {
139+
// None
140+
// }
141+
// })
142+
// .map(|canister| canister.canister_id())
143+
// .collect())
120144
}
121145

122146
#[context("Failed to build Motoko canister '{}'.", canister_info.get_name())]

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -578,19 +578,19 @@ impl CanisterPool {
578578

579579
// FIXME: Verify after graph creation (and that creation does not stuck in an infinite loop).
580580
// Verify the graph has no cycles.
581-
if let Err(err) = petgraph::algo::toposort(&graph, None) {
582-
let message = match graph.node_weight(err.node_id()) {
583-
Some(canister_id) => match self.get_canister_info(canister_id) {
584-
Some(info) => info.get_name().to_string(),
585-
None => format!("<{}>", canister_id.to_text()),
586-
},
587-
None => "<Unknown>".to_string(),
588-
};
589-
return Err(DfxError::new(BuildError::DependencyError(format!(
590-
"Found circular dependency: {}",
591-
message
592-
))))
593-
}
581+
// if let Err(err) = petgraph::algo::toposort(&graph, None) {
582+
// let message = match graph.node_weight(err.node_id()) {
583+
// Some(canister_id) => match self.get_canister_info(canister_id) {
584+
// Some(info) => info.get_name().to_string(),
585+
// None => format!("<{}>", canister_id.to_text()),
586+
// },
587+
// None => "<Unknown>".to_string(),
588+
// };
589+
// return Err(DfxError::new(BuildError::DependencyError(format!(
590+
// "Found circular dependency: {}",
591+
// message
592+
// ))))
593+
// }
594594

595595
// Traverse, creating the graph of dependencies starting from `real_canisters_to_build` set.
596596
let mut current_canisters_to_build =
@@ -604,8 +604,6 @@ impl CanisterPool {
604604
// println!("self.canisters.len(): {}", self.canisters.len());
605605
for canister in &self.canisters { // a little inefficient
606606
// FIXME: Remove:
607-
println!("current_canisters_to_build={:?} canister={}",
608-
current_canisters_to_build.keys().map(|c| c.to_text()).collect::<Vec<_>>(), canister.canister_id());
609607
if !current_canisters_to_build.contains_key(&canister.canister_id()) {
610608
continue;
611609
}
@@ -614,14 +612,16 @@ impl CanisterPool {
614612
// FIXME: Is `unwrap()` in the next operator correct?
615613
let deps: Vec<CanisterId> = canister.builder.get_dependencies(self, canister_info)?
616614
.into_iter().filter(|d| *d != canister_info.get_canister_id().unwrap()).collect(); // TODO: This is a hack.
617-
println!("deps.len(): {}", deps.len());
615+
// println!("PARENT: {}, DEPS: {:?}", canister.get_info().get_canister_id()?.to_text(), deps.iter().map(|c| c.to_text()).collect::<Vec<_>>()); // FIXME
618616
let node_ix = *id_set.entry(canister_id).or_insert_with(|| graph.add_node(canister_id));
619617
for d in deps {
620618
let dep_ix = *id_set.entry(d).or_insert_with(|| graph.add_node(d));
621619
graph.add_edge(node_ix, dep_ix, ());
622620
current_canisters_to_build2.insert(d, ());
621+
println!("canister_id={} -> d={}", canister_id, d);
623622
}
624623
}
624+
println!("NEXT CYCLE"); // FIXME: Remove.
625625
if current_canisters_to_build2.is_empty() { // passed to the end of the graph
626626
break;
627627
}

0 commit comments

Comments
 (0)