Skip to content

Commit 4ce0f66

Browse files
committed
misc
1 parent fceda1d commit 4ce0f66

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### feat: rebuild only necessary canisters
66

7+
Cache `get_imports()` results.
8+
79
Read only those `--print-deps` dependencies that are necessary to read.
810

911
Don't compile canisters for which all dependencies are elder than the `.wasm` file.

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ impl MotokoBuilder {
4545

4646
// TODO: Rename this function.
4747
#[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<()> {
4949
#[context("Failed recursive dependency detection at {}.", file.display())]
5050
fn get_imports_recursive (
5151
cache: &dyn Cache,
5252
file: &Path,
5353
imports: &mut ImportsTracker,
54+
pool: &CanisterPool,
5455
) -> DfxResult {
5556
let parent = MotokoImport::Relative(file.to_path_buf());
5657
if imports.nodes.contains_key(&parent) {
@@ -67,10 +68,17 @@ fn get_imports(cache: &dyn Cache, info: &MotokoCanisterInfo, imports: &mut Impor
6768

6869
for line in output.lines() {
6970
let child = MotokoImport::try_from(line).context("Failed to create MotokoImport.")?;
70-
// TODO: The code seems screwed: Why recompile onluy on `Relative`?
7171
match &child {
7272
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+
}
7482
}
7583
_ => {}
7684
}
@@ -82,7 +90,7 @@ fn get_imports(cache: &dyn Cache, info: &MotokoCanisterInfo, imports: &mut Impor
8290
Ok(())
8391
}
8492

85-
get_imports_recursive(cache, info.get_main_path(), imports)?;
93+
get_imports_recursive(cache, info.get_main_path(), imports, pool)?;
8694

8795
Ok(())
8896
}
@@ -95,7 +103,7 @@ impl CanisterBuilder for MotokoBuilder {
95103
info: &CanisterInfo,
96104
) -> DfxResult<Vec<CanisterId>> {
97105
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)?;
99107

100108
Ok(pool.imports.borrow().nodes
101109
.iter()
@@ -146,7 +154,7 @@ impl CanisterBuilder for MotokoBuilder {
146154
std::fs::create_dir_all(idl_dir_path)
147155
.with_context(|| format!("Failed to create {}.", idl_dir_path.to_string_lossy()))?;
148156

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

151159
// If the management canister is being imported, emit the candid file.
152160
if pool.imports.borrow().nodes.contains_key(&MotokoImport::Ic("aaaaa-aa".to_string()))
@@ -179,7 +187,7 @@ impl CanisterBuilder for MotokoBuilder {
179187
loop {
180188
if let Some(import) = import_iter.next(&imports.graph) {
181189
let imported_file = match &imports.graph[import] {
182-
MotokoImport::Canister(canister_name) => {
190+
MotokoImport::Canister(canister_name) => { // duplicate code
183191
if let Some(canister) = pool.get_first_canister_with_name(canister_name.as_str()) {
184192
let main_file = canister.get_info().get_main_file();
185193
if let Some(main_file) = main_file {

0 commit comments

Comments
 (0)