From 0b6cad97355144d0ad30e4bce699fd1be6e2c512 Mon Sep 17 00:00:00 2001 From: andriyDev Date: Tue, 14 Oct 2025 15:54:48 -0700 Subject: [PATCH] Allow DiagnosticsBuffer to be applied even if DiagnosticsStore is missing. --- crates/bevy_diagnostic/src/diagnostic.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/bevy_diagnostic/src/diagnostic.rs b/crates/bevy_diagnostic/src/diagnostic.rs index 937da5b258502..17943ae60d308 100644 --- a/crates/bevy_diagnostic/src/diagnostic.rs +++ b/crates/bevy_diagnostic/src/diagnostic.rs @@ -381,7 +381,17 @@ impl SystemBuffer for DiagnosticsBuffer { _system_meta: &bevy_ecs::system::SystemMeta, world: &mut bevy_ecs::world::World, ) { - let mut diagnostics = world.resource_mut::(); + let Some(mut diagnostics) = world.get_resource_mut::() else { + // `SystemBuffer::apply` is called even if the system never runs. If a user uses + // `If`, this buffer will be applied even if we are missing + // `DiagnosticsStore`. So be permissive to allow these cases. See + // https://github.com/bevyengine/bevy/issues/21549 for more. + + // Clear the buffer since we have nowhere to put those metrics and we don't want them to + // grow without bound. + self.0.clear(); + return; + }; for (path, measurement) in self.0.drain() { if let Some(diagnostic) = diagnostics.get_mut(&path) { diagnostic.add_measurement(measurement);