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:
| Crate | 0.20 | 0.21 |
|---|---|---|
crux_core | 0.20.0 | 0.21.0 |
crux_http | 0.20.0 | 0.21.0 |
crux_kv | 0.14.0 | 0.15.0 |
crux_time | 0.18.0 | 0.19.0 |
crux_macros | 0.10.1 | 0.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:
-
facetstays at=0.46.5. -
facet_generate:crux_core'sfacet_typegenfeature now pulls in 0.22.5 (it was 0.19). Change it only if your own crate depends onfacet_generate.facet-generate-attrsstays at 0.18. -
BoltFFI moves from 0.29.3 to 0.30.1. Move all three pieces at once:
the
boltfficrate (boltffi = "=0.30.1"), the CLI (cargo install boltffi_cli --version '=0.30.1' --locked) and@boltffi/runtime0.30.1 in each web shell'spackage.json.crux_coredoesn'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
typegenfeature withfacet_typegen, oncrux_coreand 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 ofcrux_core::typegen,crux_core::type_generation::serdeorOperation::register_types.Operation::register_types_facetkeeps its name. -
Derive
Faceton the types that cross the bridge, and generate from a codegen binary usingTypeRegistry. Type generation covers the annotations, skipped and opaque types and the codegen binary.
ResolveError has grown
-
ResolveErroris now#[non_exhaustive], so an exhaustivematchon it needs a_arm. -
Resolving a notification through the bridge now fails with
ResolveError::Never. In 0.20 the bridge reportedNotFound, so update any test that expects that.NotFoundnow only means an id that was never issued or has already been resolved. -
Three new variants,
NoSuchEffect,WrongEffectandWrongKind, 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
EffectIdis now private. If a hand-written FFI bridge wraps the id that its shell passes back, changeEffectId(id)toEffectId::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_shellfor a render, say), that's now a build error. It's reported bycargo build,cargo testorcargo clippy --all-targets, not bycargo check. Your own operations aren't affected unless you give them a kind.
Deprecation warnings
-
The enum APIs of
crux_kv(KeyValue,KeyValueOperation, …) andcrux_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,ICoreBridgeorFfiBridge, or an effect variant calledOperationKind, is now an error fromTypeRegistry::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
Coreof your own. The generated package now has a class calledCore. On Android, a hand-writtenCorein 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 usesCoreWrapper), or turn the generated one off withCodeGenerator::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 abuild.gradle.ktsinto the generated directory, and it removes the file that an earlier version wrote. -
TypeScript.
CodeGenerator::typescriptnow fails ifpnpm installortscfails 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::Timeand 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 generatedEffectHandlerand letCorerun 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, andCoregets a constructor that takes only your handler. See bridging to BoltFFI. Two build changes come with it:- TypeScript: the generated
package.jsonnow depends on the wasm package, and typegen runspnpm install, so runboltffi pack wasmbefore typegen. - Swift: the generated package depends on BoltFFI's, so give it a
platforms:floor withConfig::builder(..).platform(".iOS(.v16)").
- TypeScript: the generated
-
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_httpcan now turn it into the sameResponseorHttpErroracrux_httprequest 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.