Custom build step fails with dfx 14

I’ve been using the next custom build.sh script

#!/bin/sh

CANISTER_NAME=$1
ENTRY_POINT=$2

mkdir -p .dfx/local/canisters/${CANISTER_NAME}

$(vessel bin)/moc ${ENTRY_POINT} -o .dfx/local/canisters/${CANISTER_NAME}/${CANISTER_NAME}.wasm \
-c --debug --idl --stable-types --public-metadata candid:service --actor-idl .dfx/local/canisters/idl/ \
--actor-alias ${CANISTER_NAME} $(dfx canister id ${CANISTER_NAME}) $(vessel sources)

ic-wasm .dfx/local/canisters/${CANISTER_NAME}/${CANISTER_NAME}.wasm -o .dfx/local/canisters/${CANISTER_NAME}/${CANISTER_NAME}.wasm shrink

gzip -f -9 .dfx/local/canisters/${CANISTER_NAME}/${CANISTER_NAME}.wasm

And dfx.json config

{
  "canisters": {
    "test": {
      "build": "build.sh test test.mo",
      "candid": ".dfx/local/canisters/test/test.did",
      "type": "custom",
      "wasm": ".dfx/local/canisters/test/test.wasm.gz"
    }
  }
}

It works fine with dfx 13.1 and lower but dfx 14 fails with an error

Failed during wasm installation call: The replica returned a replica error: Replica Error: reject code CanisterError, reject message Wasm module of canister bkyz2-fmaaa-aaaaa-qaaaq-cai is not valid: Failed to decode wasm module: unsupported canister module format, error code None

This custom build step serves 2 main purposes: gzip the WASM and install custom Motoko compiler.

The first one is no longer necessary with dfx 14 but, as far as I know, custom build is still the only option to install a different Motoko compiler.

Would appreciate any insight on how I can fix the error.

custom build is still the only option to install a different Motoko compiler

you could also use DFX_MOC_PATH environment variable, see docs

Perhaps this will help to mitigate the issue?

It can work as an intermediate solution until we can find a valid fix for this. It adds manual steps to the process that, in my opinion, should be automatic. Complicates deployment of the project for other developers (as they would need to know additional variables to specify instead of just printing “dfx deploy” / “dfx build”).

Ultimately, I would really appreciate an ability to specify moc path in dfx.json config instead of using environment variables (including the ability to specify commands for execution - like “vessel bin” which will handle compiler installation on top of returning a path string).

For now I am manually replacing compiler files in the dfx cache directory and trying to make my custom build work.

1 Like