Writing Smart Contracts on ICP in Go – A First Step

Hey @Lorimer. I only ever used TinyGo to produce canisters (Go-based canisters compiled to wasm), not to run the TinyGo compiler itself inside one. I did poke at the “compile in a canister” idea a few times though, and bounced off it for a handful of reasons. @miadey already touched on the big two, so let me expand and add a few more:

  • Instruction limits. This is the first wall. Single-round execution is capped, and while Deterministic Time Slicing lets update messages stretch across many rounds, into the tens of billions of instructions, a lot of compiler internals (LLVM opt passes especially) aren’t naturally chunkable across await points. So DTS doesn’t bail you out as cleanly as it does for, say, a big loop over a map.

  • File system. Like miadey said. Compilers love spraying intermediate artifacts everywhere: object files, caches, temp dirs, lockfiles. stable-fs is a great starting point (and probably necessary), but most toolchains assume a real OS underneath, not a single-process no-fork/exec world. Patching that assumption out of a real compiler is its own project.

  • Multi-stage bootstraps. Zig is the textbook case: the bootstrap pipeline is zig1.wasm → C → zig1 → zig2.c → zig2 → final stage3 (LLVM-backed). Each stage assumes a host with a C compiler, a working FS, and the ability to spawn processes. Replicating that inside a canister is a very different problem from “just run the compiler.” Ironically Zig ships a wasm artifact as part of its own bootstrap, which kinda hints at the shape of the right answer, but the consumer of that wasm is still a normal host OS, not a canister.

  • WASI surface. Most of the gnarlier bits a compiler wants (sockets, proper FS semantics, process stuff) live in wasip2 territory, and we’re not there yet on the canister side.

  • Determinism. Whatever you run has to produce byte-identical output across replicas. Most compilers basically are deterministic, but anything sneaking in via randomized hashmap iteration in codegen or parallel work-stealing can ruin your day.

So TL;DR: I think your current approach (TinyGo → wasm-unknown + Rust glue) is genuinely the pragmatic path. Doing the whole compile inside a canister is interesting but I’d treat it as a research project rather than a path to shipping something.

Happy to dig into any of the above if useful.