Dfx deploy failure when importing a pulled canister

I have a canister that is importing the Ledger canister via import ic:ryjl3-tyaaa-aaaaa-aaaba-cai. When I run dfx deploy, I get an error:

~/dfx-deploy-failure-example/src/dfx-deploy-failure-example-backend/store/main.mo:1.1-1.47: import error [M0009], file "~/dfx-deploy-failure-example/.dfx/local/canisters/idl/ryjl3-tyaaa-aaaaa-aaaba-cai.did" does not exist

I would expect ryjl3-tyaaa-aaaaa-aaaba-cai.did to be created before the store canister is built but that doesn’t seem to be the case.

Here is a small example repo:

Any help here would be appreciated!

Additional context:
moc version: Motoko compiler 1.1.0 (source q8nbql1z-ylg40zah-wmfljwd6-j6bq1lwd)

dfx version: dfx 0.31.0

Doing some testing, it appears this is related to the name of the canister. Changing the name from store to anything alphabetically earlier than icp_ledger (i.e. foo) fixes the problem.

Even still, I would expect the dependencies arg in dfx.json to control the order that the canisters are built.

There’s a bad dependency in the repo I think… Anyways, after removing the superfluous mops.toml dependencies I can reproduce the issue, thanks a lot for the repo!

Turns out dfx ignores what you declare as dependencies for Motoko canisters in dfx.json, and instead asks moc, the Motoko compiler, what canisters your imports depend on. I could probably fix it, but we put minimal effort into dfx now in favor of icp-cli.

What you can change in your project setup to make moc report the ledger as a dependency is to change your import statement from import Ledger "ic:ryjl3-tyaaa-aaaaa-aaaba-cai"; to import Ledger "canister:icp_ledger";. With this change the project deploys just fine in my local setup, no matter what name I give the ICP ledger

Hey, thanks for the repo, this helps a lot to understand the issue.

This usually happens because moc resolves dependencies itself instead of relying on dfx.json, which can mess up the build order.

A simple fix is to use the canister alias instead of the raw canister id:

import Ledger "canister:icp_ledger";

This makes the dependency explicit and fixes the deploy issue in most cases.

Ah this makes sense. Thank you for your help @vivienne! Thank you as well @taimoor_dev!