Can't upgrade my Motoko canister

When I run dfx canister install --all -m upgrade, I am greeted with this error:

Upgrading code for canister service, with canister_id rrkah-fqaaa-aaaaa-aaaaq-cai
The invocation to the wallet call forward method failed with the error: An error happened during the call: 5: Canister rrkah-fqaaa-aaaaa-aaaaq-cai trapped explicitly: Odd offset

I see that the Motoko compiler generates the trap here.

Looking through that code, I suspect the root cause is due to the fact I changed one of my stable variables (which is an object) to have a mut field instead of a default immutable field. But the error isn’t helpful. I’m hoping with dfx 0.8.5, upgrade messages will be more helpful now that stable interfaces are checked.

Can anyone confirm my suspicions?

1 Like

Yes, your suspicion is right on: Because the type of the stable variables changed the decoder falls over the data it finds, and the upgrade check will (hopefully) warn you about this change.

As a strategy here, you may want to move that data to a new var with a different name in your post upgrade and refactor your code to use that new var. Maybe even with just. _v2 appended to the end so you can keep track of what version of the var you are on. I need to add some upgrade patterns to

1 Like

Yeah, that would work too.

I’m hoping the Motoko team loosens the conditions for stable compatibility in the near future, such that it’s OK to remove fields from a stable variable record. Right now, you can’t remove stuff. (So you’d have _v2, then _v3, etc…)

1 Like