Development workflow: quickly test code modifications

As I’m writing more and more Motoko, it would be nice to have a playground to quickly test code. The quickest way I’ve found to test a function is to run this after each modification:

dfx deploy && dfx canister call my_can myFunc

Even with minimal code, this takes roughly 30 seconds before the output of myFunc is reported.

Is there a faster way to test changes to my code?

3 Likes

If you’ve only made changes to a single canister you can run:
dfx build my_can && dfx canister install my_can -m upgrade && dfx canister call my_can myFunc

You can check the options for any of these commands with the --help flag too, eg:
dfx canister install --help

1 Like

This might help:

2 Likes

We are going to release a web-based playground for Motoko to allow you deploy canisters online soon :slight_smile:

The live edits in the SDK website is a lightweight version of the playground, if you want to explore small Motoko code snippets.

6 Likes

Excellent

Something like Remix would reach Solidity devs;

https://remix.ethereum.org/

2 Likes

@Ori For some reason, when I try to build just the one canister I’m working on, it fails with

import error, canister alias "credits" not defined

even though I do have a canister named credits in the project that can be built successfully with

dfx build credits

Everything builds and runs successfully when I use dfx build or dfx deploy, so I’m not sure why building just a single canister fails when it is importing another canister.

Have you tried running dfx deploy once first, then using the commands I suggested for any further single canister changes?

Yup. The first command, dfx build serve fails, complaining that credits is not found. serve is one canister that imports the credits canister which is in the same project and both exist in the canister_ids.json file:

"credits": {
  "local": "rwlgt-iiaaa-aaaaa-aaaaa-ca
},
"serve": {
  "local": "ryjl3-tyaaa-aaaaa-aaaba-cai"
},

My output:

% dfx build serve
Building canisters...
The build step failed for canister 'ryjl3-tyaaa-aaaaa-aaaba-cai' with an embedded error: The command '"/Users/mikem/.cache/dfinity/versions/0.6.16/moc" "/Users/mikem/OneDrive/Projects/Web/videate/credits/src/serve/main.mo" "-o" "/Users/mikem/OneDrive/Projects/Web/videate/credits/.dfx/local/canisters/serve/serve.did" "--idl" "--actor-idl" "/Users/mikem/OneDrive/Projects/Web/videate/credits/.dfx/local/canisters/idl/" "--actor-alias" "serve" "ryjl3-tyaaa-aaaaa-aaaba-cai" "--package" "base" "/Users/mikem/.cache/dfinity/versions/0.6.16/base"' failed with exit status 'exit code: 1'.
Stdout:

Stderr:
/Users/mikem/OneDrive/Projects/Web/videate/credits/src/serve/main.mo:19.1-19.34: import error, canister alias "credits" not defined

serve uses import Credits "canister:credits"; to import the credits canister and the credits code is parallel to the serve code. Both are able to be deployed with dfx deploy commands; building works just fine in that case.

1 Like

Ah, you’ll need to add the credits canister as a dependency of the serve canister in your dfx.json, like this:

"serve": {
    "main": "src/serve/main.mo",
    "type": "motoko",
    "dependencies": [
        "credits"
    ]
},

Then the above steps should work, so…

First run only:
dfx deploy

Subsequent runs for single canister changes:
dfx build serve && dfx canister install serve -m upgrade && dfx canister call serve myFunc

1 Like

Oh interesting. Odd that the deploy command works without the dependency, but the build command doesn’t. I thought deploy just used build under the hood. Something else is going on?

dfx deploy will be doing the same as dfx build --all, which will work too, but to build an individual canister it needs the dependencies listed.

1 Like