bevy_expected_components
Runtime validation for Bevy component dependencies. Like #[require] but panics instead of auto-inserting.
Performance Warning
This crate adds runtime overhead. Every time a component with #[expects(...)] is inserted, the plugin checks that all expected components exist on the entity.
Enable only in development and test builds:
app.add_plugins;
With this pattern, release builds have zero overhead.
Why Use This?
Bevy's #[require(T)] automatically inserts missing components using Default. This doesn't work when:
- Required components need world access to construct (e.g., handles, entity references)
- You want bugs to surface immediately rather than silently using defaults
- The required component has no sensible default
#[expects(T)] solves this by panicking if expected components are missing, making bugs immediately visible during development.
Installation
[]
= "0.1"
Usage
use *;
use *;
// PhysicsBody expects Transform and Velocity to exist when inserted
;
;
Error Messages
When validation fails, you get a clear panic message:
my_game::PhysicsBody expects my_game::Velocity but it was not found on entity 42v3
The stack trace points to the spawn site, making debugging straightforward.
Comparison with #[require]
| Feature | #[require] |
#[expects] |
|---|---|---|
| Missing component | Auto-inserted via Default |
Panics |
Requires Default |
Yes | No |
| Runtime cost | Archetype lookup | Component check |
| Use case | Convenience bundles | Bug detection |
| When to use | Components with sensible defaults | Components that must be explicitly provided |
Multiple Expectations
You can list multiple components in one attribute or use multiple attributes:
// Single attribute with multiple components
;
// Multiple attributes (equivalent)
;
Qualified Paths
Full paths work too:
;
How It Works
#[derive(ExpectComponents)]generates anExpectComponentstrait implementation- The macro registers the type with
inventoryat compile time ExpectedComponentsPlugininstallson_addhooks for all registered types- When a component is inserted, the hook validates expected components exist
- If any are missing, it panics with a descriptive message
Limitations
Validates insertion only, not removal. If you later remove an expected component from an entity, no error occurs. This keeps the implementation simple and covers the main use case: catching mistakes at spawn time.
If you need removal protection, consider using Bevy's on_remove hooks directly or waiting for archetype invariants.
Future of This Crate
This crate may become unnecessary when Bevy adds native support for non-defaultable required components. Relevant upstream discussions:
- Issue #16194: Require components that can't be defaulted - Proposes
#[require(Component(explicit))]syntax - Issue #18717: Support required components which have no sensible default - Proposes
#[must_provide(Component)] - Archetype invariants - A future Bevy feature that would enforce component relationships at the type level
Until then, this crate provides a simple, opt-in solution for development-time validation.
Bevy Version Compatibility
| bevy | bevy_expected_components |
|---|---|
| 0.19 | 0.2 |
| 0.18 | 0.1 |
AI assistance
This crate was developed with the help of AI coding tools.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.