mjdi

Search documentation

Search the mjdi v0.10.0 documentation

mjdi docsv0.10.0latest

Diagnostics Reference

Every mjdi diagnostic begins with mjdi: and is assembled at compile

Source: mjdi/docs/diagnostics.md

Every mjdi diagnostic begins with mjdi: and is assembled at compile time from reflected type names, with module qualification stripped — so messages read like your source code (UserService, not examples.app.UserService). This page catalogs every diagnostic in mjdi 0.9.0 (30 catalog entries): the exact message shape, what triggers it, and how to fix it.

The machine-readable quick-fix catalog (P1-10)

Every diagnostic in this page also exists in diagnostics/catalog.json — the generated, machine-readable catalog with pinned stable IDs (MJDI-E### compile time, MJDI-R### runtime, MJDI-A### abort, MJDI-D### debug), severities, message patterns, likely causes, quick fixes, documentation links, and example bad/corrected snippets. The catalog is generated from the library sources by scripts/gen_diagnostics.py (CI verifies it is current with --check, that every message builder in the whole library has an entry, and that every builder embeds its pinned ID). Editors and CI consume it through diagnostics/schema.json; see IDE integration, and see the validation output contract for the P0-06 ID policy (IDs are pinned, never renumbered, never reused; the same condition reported from two code paths shares one ID).

Every diagnostic embeds its stable ID in the message text ([diagnostic MJDI-XXX]), so both compile-time output and runtime errors join the catalog by ID directly.

Diagnostics come in four phases:

  • Compile-time — the program does not compile; the message appears in the compiler output attached to the failing expression.
  • Runtime raise — a Mojo Error propagates from resolve; catch it or let it terminate with a stack trace.
  • Runtime abort — the process stops immediately (used only for the unresolved-value() misuse case, where continuing would be unsound).
  • Debug (opt-in) — printed at destructor time by the debug_tracking=True specializations (P0-03); cannot raise or be caught, verified by a golden-output check.

The catalog

1. Missing provider for a declared dependency — compile time

Trigger: a class-provided service declares Inject[D], and no provider for D is registered.

mjdi: no provider is registered for dependency 'D' required by 'Consumer'.
Register it with Registry().register[D]() (or a value/factory/alias
provider) before creating a Container.

Fix: register D — as a class, value, factory, or alias provider — in the registry the container is built from. See Compile-time validation.

2. Resolving an unregistered type — compile time

Trigger: resolve[T]() (or a scope's) where T has no provider.

mjdi: no provider is registered for 'T'. Register it with
Registry().register[...](or a value/factory/alias provider) before
resolving it.

Fix: register T, or resolve a type that is registered.

3. Duplicate registration — compile time

Trigger: register / register_value / register_factory for a type the chain already provides (aliases are exempt by design).

mjdi: duplicate provider registration for 'T'. Each service type may only
be registered once per registry.

Fix: remove one of the registrations. To "override" a binding deliberately, use register_alias (last registration wins).

4. Circular dependency — compile time

Trigger (primary): a struct-level cycle; the Mojo compiler rejects the recursive struct definition before mjdi is even involved.

attempt to resolve a recursive reference to ...

Trigger (defense in depth): mjdi's graph walk, should a cycle become expressible through future compiler features:

mjdi: circular dependency detected involving 'T'. Dependency path:
A -> B -> C -> A. Break the cycle by restructuring the dependency graph,
using a factory provider, or moving the shared state behind a value or
factory provider.

Fix: break the cycle — extract the shared piece behind a value or factory provider, or restructure so dependencies point one way. See Compile-time validation → Cycles.

5. Singleton depends on scoped — compile time

Trigger: a singleton class provider declares Inject[D] where D is registered scoped.

mjdi: invalid lifetime relationship: singleton provider 'T' depends on
scoped provider 'D'. 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.

Fix: make D singleton or transient, or make T scoped.

6. Alias target not registered — compile time

mjdi: alias for 'From' targets 'To', which is not registered in this
registry.

Fix: register To (as a class provider) before register_alias.

7. Alias target is not a class provider — compile time

Trigger: register_alias[From, To] where To is a value or factory provider. (The registry and descriptor layers emit two phrasings of the same rule; the fixture asserts on the shared fragment.)

mjdi: alias 'From' cannot bind to 'To' because the target is not a class
provider. Aliases bind to class providers only, which makes alias cycles
impossible by construction.
mjdi: alias target 'To' is not a class provider. Aliases bind to class
providers only, ...

Fix: alias to a class-provided type. See Providers → Aliases.

8. Alias source not Injectable — compile time

mjdi: alias source 'From' does not conform to the Injectable trait.

Fix: conform From to Injectable (aliases construct the requested type through its own contract).

9. Circular alias — compile time (impossible by construction)

mjdi: circular alias detected involving 'From'. Alias path: ... . An alias
must eventually resolve through a non-alias provider.

Trigger: defense in depth. Because aliases bind to class providers only, an alias chain has length one; the depth-limited walk (budget 32) exists should the binding rules ever change.

10. Non-Copyable cached-lifetime provider — compile time

Trigger: registering a type without Copyable as a singleton class, scoped class, or value provider — or registering a non-Copyable product with a cached factory scope (register_factory / use_provider with provider_scope.singleton or provider_scope.scoped).

mjdi: singleton class provider 'T' requires the provided type to conform
to Copyable because consumers receive copies of the canonical instance
(Mojo value semantics). Make the type Copyable or use a transient provider.

(The kind word varies: singleton class, scoped class, value, singleton factory, or scoped factory.)

Fix: conform to Copyable with a copy constructor (Ownership and memory), or use a transient provider. Fixtures: cf_value_not_copyable.mojo, cf_cached_factory_not_copyable.mojo.

11. Class provider for a non-Injectable type — compile time

mjdi: class provider 'T' does not conform to the Injectable trait. Conform
the struct to Injectable (or register it as a value or factory provider
instead).

Fix: conform the struct, or change the registration to value/factory. Fixture: cf_not_injectable_class_provider.mojo.

12. set_value on an unregistered type — compile time

mjdi: cannot set a runtime value for 'T' because it is not registered as a
value provider (use Registry().register_value[...]()).

13. set_value on a non-value provider — compile time

mjdi: 'T' is not registered as a value provider, so set_value does not
apply.

Fix (12/13): set_value applies only to register_value[T]-provided types. Fixture: cf_set_value_non_value_provider.mojo.

14. Scoped provider resolved outside a scope — runtime raise

Trigger: app.resolve[T]() where T is registered provider_scope.scoped — as a class provider or a factory product.

mjdi: 'T' is registered with the scoped lifetime and can only be resolved
through a Scope (obtain one with container.create_scope()), not directly
from the container.

Fix: create a scope and resolve there (Scopes and lifetimes). Fixtures: tests/crash_expected/crash_scoped_from_container.mojo (class provider), tests/crash_expected/crash_scoped_factory_from_container.mojo (factory product).

15. Value provider without a value — runtime raise

mjdi: 'T' is registered as a value provider but no value has been
provided. Call container.set_value[T](/docs/0.10.0/value) before resolving it.

Fix: call set_value before the first resolution (or verify with has_value[T]()). Verified by tests/test_container.mojo.

16. Unresolved Inject.value() — runtime abort

Trigger: application code reads value() on a marker the container never populated — almost always a service constructed directly (UserService()) instead of resolved.

mjdi: dependency of type 'T' was never resolved. Inject fields are
populated by the container; construct instances through Container.resolve
(or Injectable.create) instead of calling the struct constructor directly.

Fix: resolve through the container (or call T.create(resolver) inside provider-owned code). Fixture: tests/crash_expected/crash_unresolved_inject.mojo.

17. Factory failure with preserved cause — runtime raise (MJDI-R004)

Trigger: the registered factory raises. The error is wrapped with the service type, the operation being attempted, and the original error text preserved verbatim as the underlying cause; the failure mode is unchanged (a raisable Error). See the runtime error reference for the full message shape. Verified by tests/test_factories.mojo and tests/test_runtime_errors.mojo.

18. mjdi internal assertion errors

Internal comptime asserts exist as unreachable-unless-buggy guards ("provider descriptor did not implement supply_as", "this provider kind has no runtime slot", "linear service cannot be supplied through the value-returning provider path"). If one ever surfaces, it is a mjdi bug, not an application error — please report it with the registry that triggered it.

19. Linear service registered without the annotation — compile time

Trigger: the linear overload of register[T]() where T conforms to LinearService but is not annotated with @__annotation(mjdi_linear).

mjdi: 'T' conforms to the LinearService trait but does not carry the
'mjdi:linear' annotation. Add @__annotation(mjdi_linear) above the struct
(mjdi_linear is exported by mjdi) so the registry recognizes it as a linear
service, then register it with Registry().register[T]() — the overload
selected for LinearService conformers.

Fix: annotate the struct. Fixture: tests/compile_fail/cf_linear_missing_annotation.mojo.

20. Duplicate linear registration — compile time

Trigger: the linear overload of register[T]() twice (or a regular registration following a linear one for the same type).

mjdi: duplicate provider registration for 'T'. Each service type may only
be registered once per registry.

Fix: one registration per type per registry. Fixture: tests/compile_fail/cf_linear_duplicate.mojo.

21. The visitor overload of resolve with a non-linear service — compile time

Trigger: container.resolve[T](/docs/0.10.0/visitor) where T does not conform to LinearService.

The overload's T: LinearService trait bound rejects the call before any mjdi code runs, so this rejection is a compiler overload-resolution error (naming the unsatisfied LinearService requirement or the failed overload candidates), not an mjdi assert — trait bounds are the mechanism by which non-linear types are kept out of the linear path.

Fix: resolve non-linear services with the value-returning resolve[T]. Fixture: tests/compile_fail/cf_linear_resolve_non_linear.mojo.

22. The visitor overload of resolve without the annotation — compile time

Trigger: container.resolve[T](/docs/0.10.0/visitor) where T conforms to LinearService but lacks the mjdi_linear annotation.

mjdi: container.resolve[T](/docs/0.10.0/visitor) requires 'T' to carry the 'mjdi:linear'
annotation. Add @__annotation(mjdi_linear) above the struct (mjdi_linear
is exported by mjdi).

Fix: annotate the struct. Fixture: tests/compile_fail/cf_linear_resolve_unannotated.mojo.

23. The visitor overload of resolve without a registered linear provider — compile time

Trigger: container.resolve[T](/docs/0.10.0/visitor) where no linear provider for T is registered.

mjdi: no provider is registered for the linear service 'T'. Register it
with Registry().register[T]() before consuming it with
container.resolve[T](/docs/0.10.0/visitor).

Fix: register the linear service. Fixture: tests/compile_fail/cf_linear_resolve_unregistered.mojo.

24. Linear service and the regular registration/resolution paths — compiler-level

The regular registration paths (register, register_value, register_factory, use_value, use_provider, register_alias) and the movable-bound introspection queries are generic over T: Movable & Deinitable, and the class-provider/value-returning-resolve registration overloads additionally carry where not conforms_to(T, LinearService) (plain methods, where the compiler proves the clause at each concrete call site). A linear type satisfies neither requirement, so the compiler itself rejects these calls before any mjdi diagnostic applies — typically as a bound/conformance error naming Movable (or Deinitable), or the failed LinearService where-clause. This is deliberate: the type system, not a library assert, closes every cached/copied/by-value path for linear services. Fix: use the linear overload of register[T]() and container.resolve[T](/docs/0.10.0/visitor) (see Linear services).

27. Circular dependency between services — compile time (MJDI-E015)

Trigger: a registered service's Inject fields form a cycle. The compiler output carries [diagnostic MJDI-E015] and the dependency path. Verified by tests/compile_fail/cf_circular_dependency.mojo.

28. Singleton depends on scoped — compile time (MJDI-E016)

Trigger: a singleton provider declares an Inject dependency on a scoped provider. Carries [diagnostic MJDI-E016]. Verified by tests/compile_fail/cf_singleton_depends_on_scoped.mojo.

29. explain_resolution of an unregistered service — compile time (MJDI-E020)

Trigger: explain_resolution[T] was called for a type with no provider. Carries [diagnostic MJDI-E020]. Verified by tests/compile_fail/cf_explain_unregistered.mojo.

30. Unclosed tracked container/scope — debug print (MJDI-D001 / MJDI-D002)

Trigger: a debug_tracking=True container or scope was dropped while cached instances existed and no explicit close ran. Printed at drop time (destructors cannot raise); states which services missed their pre_destruct hooks. See lifecycle safety diagnostics. Verified by the runner's golden-output check.

Reading compiler output

The diagnostics are emitted as constraint-failure messages attached to the failing expression. Practical tips:

  • The constraint text is the whole message; everything after mjdi: is meant for you. The compiler may show additional notes around it (for example, elaboration context) — those are noise unless you are hacking on mjdi itself.
  • Type names in messages are the last path component of the reflected name, with parameters intact. Two types with the same short name in different modules read identically; if that is ambiguous in your code, rename one — or check the required by / targets pairs, which usually disambiguate by context.
  • The message tells you the registration call to add (checks 1 and 2 include a ready-to-paste Registry().register[...]()) or the rule that was violated.

Testing your diagnostics

Adapt the fixture pattern for your own project's guarantees — scripts/run_tests.sh shows the harness: compile the file expecting failure, then grep for the diagnostic fragment. The full methodology is in Testing.

25. Override of an unregistered service — compile time

Trigger: override[T] / override_value[T] / override_factory[T, ...] where T was never registered. ID: MJDI-E008.

mjdi: cannot override the provider for 'T': no provider for it is
registered in this registry. Override replaces an existing registration —
register the service first (Registry().register[T]() or a
value/factory provider), then override it. [diagnostic MJDI-E011]

Fix: register the service first, then override it. See Registry overrides. (The ID is pinned to the catalog entry; the catalog generator guarantees the mapping.)

26. Re-entrant resolution detected — runtime raise

Trigger: a resolution cycle the compile-time graph validation cannot see: custom construction code (a custom create override or a linear constructor) resolved a service that resolves this service again on the same container. ID: MJDI-R003.

mjdi: re-entrant resolution of 'A' detected while it is already being
resolved. Resolution path: A -> B -> A. This is a dependency cycle the
compile-time graph validation cannot see: it is created by custom
construction code (a custom create override or a linear constructor)
resolving services that resolve this service again. [diagnostic
MJDI-R003] Break the cycle by restructuring the custom construction
path, or remove the re-entrant resolve call.

Fix: restructure the custom construction path so it does not re-enter its own service's resolution. See Re-entrancy.

27. Explain of an unregistered service — compile time

Trigger: explain_resolution[registry, T] where T is not registered in the registry.

mjdi: explain_resolution requires 'T' to be registered in this registry.
Register it (or explain a registered service) before asking for an
explanation.

Fix: explain a registered service, or register the service first. See Explain resolution.

28. Double destruction of a linear value — compiler-level

Trigger: calling destroy a second time on a consumed linear value. The compiler rejects the use of the uninitialized value — exactly-once destruction is enforced by the language, not policed at runtime.

See Linear resource patterns.