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 May 18, 2022
1 parent de2b1a4 commit d55da1f
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 @@ -57,6 +57,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 @@ -100,6 +103,7 @@ impl App {
schedule: Default::default(),
runner: Box::new(run_once),
sub_apps: HashMap::default(),
is_from_plugin_build: false,
}
}

Expand All @@ -126,6 +130,9 @@ impl App {
let _bevy_app_run_span = info_span!("bevy_app").entered();

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 @@ -768,7 +775,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 d55da1f

Please sign in to comment.