Skip to content

Commit

Permalink
bevyengine#4231: panic when App::run() is called from Plugin::build()
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Mar 17, 2022
1 parent c1a2378 commit a35b026
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ pub struct App {
/// A container of [`Stage`]s set to be run in a linear order.
pub schedule: Schedule,
sub_apps: HashMap<Box<dyn AppLabel>, SubApp>,
/// A private marker to prevent incorrect calls to `App::run()` from `Plugin::build()`
/// <https://github.com/bevyengine/bevy/issues/4231>
is_from_plugin_build: bool,
}

/// Each [`SubApp`] has its own [`Schedule`] and [`World`], enabling a separation of concerns.
Expand Down Expand Up @@ -99,6 +102,7 @@ impl App {
schedule: Default::default(),
runner: Box::new(run_once),
sub_apps: HashMap::default(),
is_from_plugin_build: false,
}
}

Expand Down Expand Up @@ -129,6 +133,9 @@ impl App {
let _bevy_app_run_guard = bevy_app_run_span.enter();

let mut app = std::mem::replace(self, App::empty());
if app.is_from_plugin_build {
panic!("App::Run() is called from a Plugin::Build(), which might result in other initialization code not run.");
}
let runner = std::mem::replace(&mut app.runner, Box::new(run_once));
(runner)(app);
}
Expand Down Expand Up @@ -762,7 +769,9 @@ impl App {
T: Plugin,
{
debug!("added plugin: {}", plugin.name());
self.is_from_plugin_build = true;
plugin.build(self);
self.is_from_plugin_build = false;
self
}

Expand Down

0 comments on commit a35b026

Please sign in to comment.