Skip to content

Commit 1395e36

Browse files
kristoff3ralice-i-cecilemockersf
authored
Fix is_plugin_added::<Self>() being true during build (#13817)
# 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 <alice.i.cecile@gmail.com> Co-authored-by: François Mockers <mockersf@gmail.com> Co-authored-by: François Mockers <francois.mockers@vleue.com>
1 parent 01971f2 commit 1395e36

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

crates/bevy_app/src/app.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,12 +439,7 @@ impl App {
439439
plugin: Box<dyn Plugin>,
440440
) -> Result<&mut Self, AppError> {
441441
debug!("added plugin: {}", plugin.name());
442-
if plugin.is_unique()
443-
&& !self
444-
.main_mut()
445-
.plugin_names
446-
.insert(plugin.name().to_string())
447-
{
442+
if plugin.is_unique() && self.main_mut().plugin_names.contains(plugin.name()) {
448443
Err(AppError::DuplicatePlugin {
449444
plugin_name: plugin.name().to_string(),
450445
})?;
@@ -459,6 +454,9 @@ impl App {
459454

460455
self.main_mut().plugin_build_depth += 1;
461456
let result = catch_unwind(AssertUnwindSafe(|| plugin.build(self)));
457+
self.main_mut()
458+
.plugin_names
459+
.insert(plugin.name().to_string());
462460
self.main_mut().plugin_build_depth -= 1;
463461

464462
if let Err(payload) = result {
@@ -1275,6 +1273,21 @@ mod tests {
12751273
.init_resource::<TestResource>();
12761274
}
12771275

1276+
#[test]
1277+
/// Plugin should not be considered inserted while it's being built
1278+
///
1279+
/// bug: <https://github.com/bevyengine/bevy/issues/13815>
1280+
fn plugin_should_not_be_added_during_build_time() {
1281+
pub struct Foo;
1282+
1283+
impl Plugin for Foo {
1284+
fn build(&self, app: &mut App) {
1285+
assert!(!app.is_plugin_added::<Self>());
1286+
}
1287+
}
1288+
1289+
App::new().add_plugins(Foo);
1290+
}
12781291
#[test]
12791292
fn events_should_be_updated_once_per_update() {
12801293
#[derive(Event, Clone)]

0 commit comments

Comments
 (0)