mjdi docsv0.10.0latest
Compile-Time Validation
mjdi's promise is that an invalid dependency graph fails to compile. This
Source: mjdi/docs/compile-time-validation.md
mjdi's promise is that an invalid dependency graph fails to compile. This page is the precise contract: which checks run, exactly when they run, what is guaranteed to be rejected, and — just as important — the detection boundaries that no compile-time system can cross.
When validation runs
Validation is organized in two layers, by design:
- Registration-time guards (defense in depth).
register,register_value,register_factory, andregister_aliasrun compile -time assertions as each call is elaborated. They remain as early signals, but they are not the authoritative layer: verified compiler quirks (findings 30/31/38 in Technical investigation) make some comptime conditions inert inside the chained registration evaluation pattern, so registration guards alone cannot be trusted for every case. - Container-construction validation (authoritative). Constructing
Container[registry](or aScope[registry]) elaboratesvalidate_registry[registry](), which walks every registered provider and checks each one against the full chain. This runs correctly for all checks and still executes before any application code — the constructor body of the container is itself compile-time elaborated.
The practical rule: if the graph is invalid, Container[type_of(reg)]()
does not compile. Applications that want an even earlier signal can call
validate_registry explicitly (exported from the package root), as
examples/layered_app/app.mojo does — useful when composing registries
long before the container site.
The checks
Missing providers
Every Inject[D] field of every class-provided service must have a
registered provider for D. The diagnostic names both sides:
mjdi: no provider is registered for dependency 'Database' required by
'UserService'. Register it with Registry().register[Database]() (or a
value/factory/alias provider) before creating a Container.
This check is what makes the graph a compile-time artifact: delete a
registration and every consumer that depends on it fails to compile, by
name. Fixture: tests/compile_fail/cf_missing_provider.mojo.
Duplicate registrations
A class, value, or factory provider whose service type is already provided by the chain is rejected:
mjdi: duplicate provider registration for 'ServiceA'. Each service type may
only be registered once per registry.
Aliases are exempt by design — aliasing an already-registered type is the
documented override mechanism (last registration wins;
Providers → Aliases).
Fixture: tests/compile_fail/cf_duplicate_registration.mojo.
Lifetime relationships
A singleton service must not depend on a scoped provider:
mjdi: invalid lifetime relationship: singleton provider 'SessionCache'
depends on scoped provider 'Session'. A singleton instance cannot depend on
a per-scope instance because the scoped instance would outlive its scope.
Make the dependency singleton or transient instead.
This is the only cross-lifetime rule; the full matrix is in
Scopes and lifetimes.
Fixture: tests/compile_fail/cf_singleton_depends_on_scoped.mojo.
Alias validity
Every alias is checked against the whole chain:
- the target must be registered
(
cf_alias_missing_target.mojo), - the target must be a class provider — aliases to value or factory
providers are rejected (
cf_alias_to_value_provider.mojo,cf_alias_cycle.mojo— binding rules are what make alias cycles impossible by construction), - the aliased type must conform to
Injectable(cf_not_injectable_class_provider.mojocovers the class-provider side of the Injectable requirement), - the alias chain must terminate; a depth-limited walk (budget 32) reports a cycle path if one were ever expressible.
Copyable requirements
Providers that hand out copies of a canonical instance or stored value
require the type to be Copyable, at registration:
- singleton class provider →
cf_value_not_copyable.mojoasserts the "requires the provided type to conform to Copyable" diagnostic, - scoped class provider → same check,
- value provider → same check,
- cached factory scopes (
register_factory/use_providerwithprovider_scope.singletonorprovider_scope.scoped) → same check, pinned bycf_cached_factory_not_copyable.mojo.
Injectable requirement
A class provider whose type does not conform to Injectable is rejected
when the container is constructed
(cf_not_injectable_class_provider.mojo), and the construction dispatch
carries its own assertion with the same message.
Resolution-site checks
Two checks fire where the resolution is written, both compile-time:
- Resolving an unregistered type — the diagnostic names the type and
instructs: register it with
Registry().register[...](or a value/factory/alias provider) before resolving it. set_valueon a non-value-provider — "mjdi: 'T' is not registered as a value provider, so set_value does not apply" (cf_set_value_non_value_provider.mojo), and its companion for unregistered types.
Linear-service checks
Linear services (annotated @__annotation(mjdi_linear), conforming to
LinearService — see Linear services) add four
compile-time rules:
- The linear overload of
registerrequires the annotation — aLinearServiceconformer without@__annotation(mjdi_linear)is rejected with a dedicated diagnostic (cf_linear_missing_annotation.mojo). - Duplicate linear registration — the same relaxed-bound chain query
that powers duplicate detection for every provider kind also sees linear
providers, so a type cannot be registered twice, and a linear
registration blocks later regular registrations of the same type
(
cf_linear_duplicate.mojo). - Visitor-overload misuse — the linear consumption path
(
container.resolve[T](/docs/0.10.0/visitor)) is guarded by a trait bound plus two asserts: theT: LinearServicebound keeps non-linear types out at overload resolution (compiler-level,cf_linear_resolve_non_linear.mojo), while the asserts reject aLinearServiceconformer without the annotation (cf_linear_resolve_unannotated.mojo) and an unregistered linear provider (cf_linear_resolve_unregistered.mojo). - Every regular path is closed by the type system —
register,register_value,register_factory,register_alias,resolve[T], andInject[T]fields are all bounded byMovable & Deinitable(and the class-provider/resolve overloads carryMovable & Deinitabletrait bounds, which linear types do not satisfy; the compiler rejects those calls before mjdi diagnostics apply (diagnostics entry 24 documents the behavior).
The linear service's own Inject dependencies are validated by the
standard graph walk: LinearProvider.validate_with dispatches into the
same missing-provider, cycle, and lifetime checks as a class provider.
Cycles
Two layers cover circular dependencies:
- The Mojo compiler rejects recursive struct definitions. A struct
whose field type eventually contains itself cannot be laid out in
memory, so
struct A(Injectable): var b: Inject[B]withstruct B: var a: Inject[A]fails at type-definition time with "attempt to resolve a recursive reference". Fixtures:cf_circular_dependency.mojo,cf_self_cycle.mojo. For struct-level graphs — the only kind expressible today — the compiler is the detector. - mjdi's own cycle walk (
find_cycle_path) traversesInjectfields through the registry chain and renders the full path (A -> B -> C -> A) in the diagnostic. It runs as part of validation on every container construction; on Mojo 1.2.0 it is defense in depth for graphs that future compiler features (indirection, interior references) could make expressible.
The renderable path also powers the alias-cycle diagnostic.
The compile-fail guarantee, verified
Every row below is pinned by a fixture that scripts/run_tests.sh compiles
expecting failure and asserts both rejection and the diagnostic text:
| Fixture | Asserted message fragment |
|---|---|
cf_missing_provider.mojo | no provider is registered |
cf_duplicate_registration.mojo | duplicate provider registration |
cf_circular_dependency.mojo | recursive reference |
cf_self_cycle.mojo | recursive reference |
cf_singleton_depends_on_scoped.mojo | invalid lifetime relationship |
cf_alias_missing_target.mojo | targets 'Unregistered', which is not registered |
cf_alias_cycle.mojo | cannot bind to |
cf_value_not_copyable.mojo | requires the provided type to conform to Copyable |
cf_not_injectable_class_provider.mojo | does not conform to the Injectable trait |
cf_set_value_non_value_provider.mojo | not registered as a value provider |
cf_alias_to_value_provider.mojo | cannot bind to |
cf_cached_factory_not_copyable.mojo | requires the provided type to conform to Copyable |
cf_linear_duplicate.mojo | duplicate provider registration |
cf_linear_missing_annotation.mojo | does not carry the 'mjdi:linear' annotation |
cf_linear_resolve_non_linear.mojo | the visitor overload rejects a non-linear type (LinearService / no viable overload) |
cf_linear_resolve_unannotated.mojo | to carry the 'mjdi:linear' annotation |
cf_linear_resolve_unregistered.mojo | no provider is registered for the linear service |
cf_lifecycle_missing_pre_destruct.mojo | trait conformance: the required pre_destruct / InjectableLifecycle hook is missing |
18 of the 54 checks in the suite are these compile-fail fixtures; the three crash-expected fixtures cover the runtime remainder.
What is runtime by design
Some conditions depend on runtime values no compiler can see. mjdi reports these at the point where the information exists:
| Condition | Mechanism |
|---|---|
Value provider whose value was never set_value-d | Runtime raise on resolution. |
| A factory that raises | Runtime raise, propagated from the factory. |
Inject.value() read on a never-populated marker | Runtime abort with a diagnostic. |
| Scoped provider (class or factory) resolved outside a scope | Runtime raise (the specialization is compile-time selected; the context check is answered at runtime). |
These are not validation gaps — they are the boundary between structure (compile time) and values (runtime), drawn deliberately per the product requirements and documented in Architecture → Validation.
Detection boundaries
Honest limits of the compile-time layer:
- Factory-mediated cycles. A factory function is opaque at compile time; if it resolves a service whose graph leads back to the factory's product, no static walk can see it, and the resolution recurses at runtime. Documented behavior, consistent with the PRD's runtime fallback (Known limitations §4).
- Value-provider timing. Whether a value has been set is runtime state by definition.
- Cross-container reasoning. Each container/scope validates its own registry; nothing tracks instances across containers (there are no global mutable registries to track).
Reproducing a rejection
To see any of these diagnostics without the full suite:
mojo build -I . tests/compile_fail/cf_missing_provider.mojo -o /tmp/out
# fails, printing the constraint diagnostic
Or point the pattern at your own code — the fastest diagnosis loop in mjdi is: delete the registration, rebuild, read the consumer/dependency pair in the message, restore the registration.