Skip to content

Commit 2cbad6d

Browse files
committed
bug fix
1 parent 0a89fe4 commit 2cbad6d

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,23 @@ impl MotokoBuilder {
4444
}
4545

4646
// 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>()?;
4951
#[context("Failed recursive dependency detection at {}.", file.display())]
5052
fn get_imports_recursive (
5153
cache: &dyn Cache,
5254
file: &Path,
5355
imports: &mut ImportsTracker,
5456
pool: &CanisterPool,
57+
top: Option<&CanisterInfo>, // hackish
5558
) -> 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+
};
5764
if imports.nodes.contains_key(&parent) {
5865
return Ok(());
5966
}
@@ -71,13 +78,13 @@ fn get_imports(cache: &dyn Cache, info: &MotokoCanisterInfo, imports: &mut Impor
7178
let child = MotokoImport::try_from(line).context("Failed to create MotokoImport.")?;
7279
match &child {
7380
MotokoImport::Relative(path) => {
74-
get_imports_recursive(cache, path.as_path(), imports, pool)?;
81+
get_imports_recursive(cache, path.as_path(), imports, pool, None)?;
7582
}
7683
MotokoImport::Canister(canister_name) => { // duplicate code
7784
if let Some(canister) = pool.get_first_canister_with_name(canister_name.as_str()) {
7885
let main_file = canister.get_info().get_main_file();
7986
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)?;
8188
}
8289
}
8390
}
@@ -91,7 +98,7 @@ fn get_imports(cache: &dyn Cache, info: &MotokoCanisterInfo, imports: &mut Impor
9198
Ok(())
9299
}
93100

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))?;
95102

96103
Ok(())
97104
}
@@ -103,8 +110,7 @@ impl CanisterBuilder for MotokoBuilder {
103110
pool: &CanisterPool,
104111
info: &CanisterInfo,
105112
) -> 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)?;
108114

109115
let graph = &pool.imports.borrow().graph;
110116
match petgraph::algo::toposort(&pool.imports.borrow().graph, None) {
@@ -177,7 +183,7 @@ impl CanisterBuilder for MotokoBuilder {
177183
std::fs::create_dir_all(idl_dir_path)
178184
.with_context(|| format!("Failed to create {}.", idl_dir_path.to_string_lossy()))?;
179185

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)?;
181187

182188
// If the management canister is being imported, emit the candid file.
183189
if pool.imports.borrow().nodes.contains_key(&MotokoImport::Ic("aaaaa-aa".to_string()))

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,12 @@ impl CanisterPool {
572572
}
573573
}
574574

575+
println!("GRAPH2: {:?}", self.imports.borrow().graph);
576+
// FIXME: Error on indirect dependencies.
575577
Ok(self.imports.borrow().graph.filter_map(
576578
|_node_index, node_weight| {
577579
match node_weight {
580+
// FIXME: The "tops" of the digraph are `Relative()`, not `Canister()`
578581
// TODO: `get_first_canister_with_name` is a hack
579582
MotokoImport::Canister(name) => Some(self.get_first_canister_with_name(&name).unwrap().canister_id()),
580583
_ => None,
@@ -709,6 +712,7 @@ impl CanisterPool {
709712

710713
trace!(log, "Building dependencies graph.");
711714
let graph = self.build_dependencies_graph(build_config.canisters_to_build.clone())?; // TODO: Can `clone` be eliminated?
715+
println!("GRAPH: {:?}", graph);
712716
let nodes = petgraph::algo::toposort(&graph, None).map_err(|cycle| {
713717
let message = match graph.node_weight(cycle.node_id()) {
714718
Some(canister_id) => match self.get_canister_info(canister_id) {
@@ -724,6 +728,7 @@ impl CanisterPool {
724728
.rev() // Reverse the order, as we have a dependency graph, we want to reverse indices.
725729
.map(|idx| *graph.node_weight(*idx).unwrap())
726730
.collect();
731+
println!("ORDER: {:?}", order.iter().map(|c| c.to_text()).collect::<Vec<_>>());
727732

728733
// let canisters_to_build = Bfs::new(graph, start);
729734
// let canisters_to_build = self.canisters_to_build(build_config); // FIXME

0 commit comments

Comments
 (0)