Dfinity official troubleshooting motoko migrations

From jiayaoqijia/cryptoskill · View interactive version

Dfinity official troubleshooting motoko migrations is an AI agent skill from the GitHub repository jiayaoqijia/cryptoskill. Deep reference for when the Motoko migration chain misbehaves — upgrade compatibility errors you cannot explain, migration files you cannot write, a first migration whose OldActor is non-empty, a project converted from legacy persistence, or a request to delete/remove the migrations directory or rev

How to use this skill

Skill file

Troubleshooting Motoko Migrations

Reference for the failure modes of the mops-managed migration chain (migrations/).

Do not load this for routine work. Writing a migration, deciding implicit vs explicit, or naming a new file is covered by migrating-motoko-actors — that skill's rules are sufficient for the normal path. Come here when:

How the runtime decides what to run

Two consequences drive most of the rules in this skill:

What the checker compares against

mops check validates your chain against the deployed canister's stable signature (its .most snapshot), not against local git history. So:

Diagnostics you may hit

| Diagnostic | What it actually means | Action |

| --------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------- |

| M0250 initialized stable field | A stable actor field has an inline initializer, which the chain (not the actor body) must supply | Reduce it to a typed declaration; move the initializer expression into the pending migration's NewActor |

| M0254 / M0267 initial actor requires field | main.mo declares a stable field that no migration supplies. M0267 is the same problem when the deployed signature cannot account for it either | Add the field to the pending migration's NewActor with a value computed from OldActor or a well-defined default |

| M0170 compatibility error | The new actor is not stable-compatible with the deployed signature | Add (or extend) the pending migration so the transformation is explicit |

| M0255 stable signature downgrade | The build would move the canister from an enhanced chain back to an older persistence model — usually because the chain or the mops migrations config was removed | Restore what was removed; there is no supported downgrade |

| Chain start does not match the baseline | The first migration's OldActor does not describe the state the canister actually holds — the classic case is a converted project where the chain starts from the legacy shape, not {} | See Converted projects; do not rewrite OldActor to {} |

| M0268 migration directory disagrees with deployed history | An already-applied migration was edited, deleted, or reordered, so the chain no longer matches the history recorded by the stable baseline. Surfaces at mops build (which folds the deployed baseline via --stable-baseline) and can pass mops check when check-limit trims that migration out | Restore the applied migration to exactly what was deployed and express further changes as a new migration. Only the oldest migrations may be trimmed; an applied migration in the middle cannot be removed |

| M0278 file does not export a public migration function (warning M0251 on moc 1) | A file in the chain directory has no public func migration, so the chain skips it and its migration never runs | Name the function migration; move helper modules out of the chain directory |

| M0014 effectful actor body (older toolchains) | Initialization work sitting in the actor body | Keep the actor body static — see Static actor body |

| IC0503 missing migration var (runtime, after deploy) | A stable field never received a value — typically an edited or renamed applied migration, or a field added without a migration | Add a new migration that supplies the field; never repair by editing the old file |

| IC0505 invalid custom section (install only, build and check pass) | The chain's stable-type history has outgrown the platform's custom-section limit | Nothing local fixes it — stop adding migrations and report it; see Why chains must stay short |

Write failures on a migration file are not a tooling bug — see the next section.

Frozen migration files

On platforms that freeze deployed migrations, each one is made read-only after a successful deploy and write tools reject it by its mode bits.

Converted (legacy → enhanced) projects

Some enhanced-migration projects were not created that way — they were converted in place from legacy inline-migration persistence, with live data preserved. In such a project:

A project can also inherit such a chain without having been converted itself — forking or remixing a converted project copies the chain as-is, first migration included. The rule is the same either way, and it is readable straight off the chain: a non-empty OldActor in the earliest file means state predates the chain, so that file is correct as written.

Protected configuration

Where a hosting platform owns the mops migrations config — the [canisters.<name>.migrations] section, its chain path, and check-limit — you cannot add, remove, or alter it, and the tooling will reject attempts.

EM is one-way

Once a project uses the migration chain there is no supported path back to legacy persistence. Independent mechanisms enforce this:

  1. The persistence model is decided per project and is sticky — nothing re-evaluates it later.
  2. The compiler rejects a signature downgrade (M0255), and a legacy wasm deployed over an enhanced canister traps at runtime. Signature versions only move forward.
  3. Applied migrations are identified by module name, so history cannot be rewritten by editing or renaming files — and on platforms that freeze deployed migrations, it cannot be rewritten at all.
  4. Fresh installs of a converted project permanently depend on the pre-conversion history.

So if a task asks you to delete the migrations/ directory, restore inline initializers on stable fields, or reintroduce (with migration = ...), say plainly that it is not possible for this project and offer the forward-only equivalent: a new migration that reshapes state toward what the user actually wants.

Why chains must stay short

The one-migration-per-deployed-version rule is not bureaucratic. A long chain eventually becomes undeployable, and the failure is unrecoverable:

Nothing in the toolchain caps chain length, so the one-migration-per-version discipline is the only thing keeping a project away from that cliff: fold changes into the pending migration, keep stable types lean, and never add a migration whose body would just be old.

Static actor body

The actor body must not do work. Stable fields are typed declarations; their values come from the chain.

Older toolchains rejected any effectful actor body outright, and that compiler check has since been relaxed for transient bindings — but the rule stands regardless of whether the compiler complains, because top-level seeding re-runs on every upgrade and would overwrite live state.

Initialization belongs in exactly two places:

When you cannot resolve it

Do not create room to proceed by relaxing protected configuration, deleting or renaming history, or rewriting a frozen file. Those turn a build failure into data loss. If the chain and the deployed signature genuinely disagree in a way none of the above explains, stop and report the mismatch: which fields the checker names, what the latest migration's NewActor declares, and what main.mo declares.

Related skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.