Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Upgrading from 0.20 to 0.21

This is a checklist for moving an app from crux_core 0.20 to 0.21. Work down it in order: first the dependencies, then what stops your app compiling, then what to check when you regenerate your shells. Everything after that is optional.

The largest change, operations as types, has its own migration guide. You don't have to do it to upgrade: the enum APIs of crux_kv and crux_time are deprecated, but they still work.


1. Update your dependencies

Each capability crate is built against one crux_core minor version, so move them all together:

Crate0.200.21
crux_core0.20.00.21.0
crux_http0.20.00.21.0
crux_kv0.14.00.15.0
crux_time0.18.00.19.0
crux_macros0.10.10.11.0

You only need crux_macros if you depend on it directly; crux_core re-exports its macros.

# Cargo.toml
[dependencies]
crux_core = "0.21"
crux_http = "0.21"
crux_kv = "0.15"
crux_time = "0.19"

Then check the pins around them:

  • facet stays at =0.46.5.
  • facet_generate: crux_core's facet_typegen feature now pulls in 0.22.5 (it was 0.19). Change it only if your own crate depends on facet_generate. facet-generate-attrs stays at 0.18.
  • BoltFFI moves from 0.29.3 to 0.30.1. Move all three pieces at once: the boltffi crate (boltffi = "=0.30.1"), the CLI (cargo install boltffi_cli --version '=0.30.1' --locked) and @boltffi/runtime 0.30.1 in each web shell's package.json. crux_core doesn't depend on BoltFFI, so Cargo won't catch a mismatch for you, and a runtime that doesn't match the crates breaks the web shells.

2. Fix what no longer compiles

The serde-based type generation is gone

Facet type generation has been the documented path since 0.19, and it is now the only one.

  • Replace the typegen feature with facet_typegen, on crux_core and on each capability crate you enable it for (crux_http, crux_kv, crux_time, crux_macros).
  • Replace #[effect(typegen)] with #[effect(facet_typegen)]. The old form is now a compile error that tells you so.
  • Remove any #[derive(Export)], and any use of crux_core::typegen, crux_core::type_generation::serde or Operation::register_types. Operation::register_types_facet keeps its name.
  • Derive Facet on the types that cross the bridge, and generate from a codegen binary using TypeRegistry. Type generation covers the annotations, skipped and opaque types and the codegen binary.

ResolveError has grown

  • ResolveError is now #[non_exhaustive], so an exhaustive match on it needs a _ arm.
  • Resolving a notification through the bridge now fails with ResolveError::Never. In 0.20 the bridge reported NotFound, so update any test that expects that. NotFound now only means an id that was never issued or has already been resolved.
  • Three new variants, NoSuchEffect, WrongEffect and WrongKind, report an id that doesn't belong to the request it claims to answer.

Request ids are no longer a counter

In 0.20 an id was a counter that went up by one for every request. It now carries the effect variant, the operation kind and a sequence, and every notification shares id 0.

  • If a shell or a test makes up an id, or expects ids to be 0, 1, 2…, change it to use the id that arrived with the request. There's nothing in the id for a shell to read. See resolve errors.
  • The field of EffectId is now private. If a hand-written FFI bridge wraps the id that its shell passes back, change EffectId(id) to EffectId::from(id).

An effect enum has at most 256 variants

  • #[effect] rejects an enum with more, because the variant index takes eight bits of the request id. It's a compile error, so you'll know.

Render and HTTP only go through their own Command constructor

RenderOperation now declares itself a notification and HttpRequest a request.

  • If you send either with a different constructor (request_from_shell for a render, say), that's now a build error. It's reported by cargo build, cargo test or cargo clippy --all-targets, not by cargo check. Your own operations aren't affected unless you give them a kind.

Deprecation warnings

  • The enum APIs of crux_kv (KeyValue, KeyValueOperation, …) and crux_time (Time, TimeRequest, …) are deprecated. If you build with warnings as errors, either migrate (see the migration guide) or put #[allow(deprecated)] on the code that uses them for now. The deprecations table names each replacement.

3. Regenerate your shells, and check them

Type generation now emits more than your types: an operation-kind accessor on each effect, an EffectHandler and EffectDispatcher, and, when your effect enum has a Render variant, a Core and a CoreBridge. All of it is additive. A shell that matches on Effect and calls resolve by hand keeps working. A few things can still trip you up:

  • Reserved names. A shared type called OperationKind, EffectHandler, IEffectHandler, EffectSink, IEffectSink, EffectDispatcher, Core, CoreBridge, ICoreBridge or FfiBridge, or an effect variant called OperationKind, is now an error from TypeRegistry::build. Rename it with #[facet(rename = "...")].
  • Two types with the same generated name are now an error. 0.20 kept the first and silently dropped the other. Rename one, or give it a namespace of its own.
  • A Core of your own. The generated package now has a class called Core. On Android, a hand-written Core in the same Kotlin package is a real clash: the two share a fully-qualified name, D8 fails release builds with "Type … Core is defined multiple times", and debug builds put both in the APK and crash on launch. In Swift and TypeScript yours shadows or sits beside the generated one without breaking the build, but renaming it is still clearer. Rename yours (the tutorial uses CoreWrapper), or turn the generated one off with CodeGenerator::without_core().
  • Kotlin. The generated code needs kotlinx-coroutines-core. Add it to the Gradle module that compiles the generated sources. Type generation no longer writes a build.gradle.kts into the generated directory, and it removes the file that an earlier version wrote.
  • TypeScript. CodeGenerator::typescript now fails if pnpm install or tsc fails in the generated package. Before, it carried on, so a generated package that doesn't type-check now fails at typegen instead of in your shell's build.

4. Adopt what's new

None of this is required, but it removes most of the code a shell writes.

  • Per-operation types. Move to crux_kv::store::KeyValue, crux_time::clock::Time and one effect variant per operation, and use #[derive(Operation)] for your own capabilities. The migration guide walks through it, and Building capabilities explains the design.
  • The generated Core. Implement the generated EffectHandler and let Core run the loop your shell used to write by hand. See who drives the loop and the generated Core.
  • The generated BoltFFI bridge. Add .boltffi(BoltFfi::new()...) to your codegen binary, and Core gets a constructor that takes only your handler. See bridging to BoltFFI. Two build changes come with it:
    • TypeScript: the generated package.json now depends on the wasm package, and typegen runs pnpm install, so run boltffi pack wasm before typegen.
    • Swift: the generated package depends on BoltFFI's, so give it a platforms: floor with Config::builder(..).platform(".iOS(.v16)").
  • The operation kind in middleware. resolver.kind() tells effect middleware whether it holds a notification, a request or a stream. See Middleware.
  • HTTP from your own operations. If you have an operation of your own that the shell answers with HttpResult, crux_http can now turn it into the same Response or HttpError a crux_http request gives you. See Handling crux_http rejections.

The one trait per operation kind RFC proposes further changes to how operations are declared, for a later release; nothing in it applies to 0.21.