From cc4681fc441375853744288e78c6ae81074e1228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20S=C3=B8holm?= Date: Fri, 14 Jun 2024 21:37:03 +0200 Subject: [PATCH] Fix is_plugin_added::() being true during build (#13817) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective Fixes #13815 ## Solution Move insertion of the plugin name to after build is called. ## Testing I added a regression test --------- Co-authored-by: Alice Cecile Co-authored-by: François Mockers Co-authored-by: François Mockers --- crates/bevy_app/src/app.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index 1391250ac9..555e30a992 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -439,12 +439,7 @@ impl App { plugin: Box, ) -> Result<&mut Self, AppError> { debug!("added plugin: {}", plugin.name()); - if plugin.is_unique() - && !self - .main_mut() - .plugin_names - .insert(plugin.name().to_string()) - { + if plugin.is_unique() && self.main_mut().plugin_names.contains(plugin.name()) { Err(AppError::DuplicatePlugin { plugin_name: plugin.name().to_string(), })?; @@ -459,6 +454,9 @@ impl App { self.main_mut().plugin_build_depth += 1; let result = catch_unwind(AssertUnwindSafe(|| plugin.build(self))); + self.main_mut() + .plugin_names + .insert(plugin.name().to_string()); self.main_mut().plugin_build_depth -= 1; if let Err(payload) = result { @@ -1275,6 +1273,21 @@ mod tests { .init_resource::(); } + #[test] + /// Plugin should not be considered inserted while it's being built + /// + /// bug: + fn plugin_should_not_be_added_during_build_time() { + pub struct Foo; + + impl Plugin for Foo { + fn build(&self, app: &mut App) { + assert!(!app.is_plugin_added::()); + } + } + + App::new().add_plugins(Foo); + } #[test] fn events_should_be_updated_once_per_update() { #[derive(Event, Clone)]