bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Improve error message when `StatesPlugin` is missing

Open TimJentzsch opened this issue 1 year ago • 3 comments

Bevy version

v0.14

[Optional] Relevant system information

cargo 1.79.0 (ffa9cf99a 2024-06-03)

The app crashes before printing the adapter info, but:

  • Linux (Pop!_OS)
  • NVIDIA Corporation TU104 [GeForce RTX 2070 SUPER]

What you did

Use states without loading the StatesPlugin or when loading it too late.

What went wrong

The app crashes and doesn't provide a useful error message on what went wrong.

I get an error like "called Option::unwrap() on a None value":

Stacktrace
    Finished `dev` profile [optimized + debuginfo] target(s) in 8.21s
     Running `target/debug/invasion_by_nightfall`
thread 'main' panicked at /home/tim/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_state-0.14.0/src/app.rs:74:67:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0: rust_begin_unwind
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:652:5
   1: core::panicking::panic_fmt
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/panicking.rs:72:14
   2: core::panicking::panic
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/panicking.rs:146:5
   3: core::option::unwrap_failed
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/option.rs:1984:5
   4: core::option::Option<T>::unwrap
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/option.rs:932:21
   5: <bevy_app::sub_app::SubApp as bevy_state::app::AppExtStates>::init_state
             at /home/tim/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_state-0.14.0/src/app.rs:74:67
   6: <bevy_app::app::App as bevy_state::app::AppExtStates>::init_state
             at /home/tim/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_state-0.14.0/src/app.rs:186:9
   7: <invasion_by_nightfall::core::CorePlugin as bevy_app::plugin::Plugin>::build
             at ./src/core/mod.rs:26:9
   8: bevy_app::app::App::add_boxed_plugin::{{closure}}
             at /home/tim/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_app-0.14.0/src/app.rs:456:55
   9: core::ops::function::FnOnce::call_once
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ops/function.rs:250:5
  10: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/panic/unwind_safe.rs:272:9
  11: std::panicking::try::do_call
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:559:40
  12: std::panicking::try
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:523:19
  13: std::panic::catch_unwind
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panic.rs:149:14
  14: bevy_app::app::App::add_boxed_plugin
             at /home/tim/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_app-0.14.0/src/app.rs:456:22
  15: <P as bevy_app::plugin::sealed::Plugins<bevy_app::plugin::sealed::PluginMarker>>::add_to_app
             at /home/tim/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_app-0.14.0/src/plugin.rs:158:17
  16: <(S0,S1,S2,S3) as bevy_app::plugin::sealed::Plugins<(bevy_app::plugin::sealed::PluginsTupleMarker,P0,P1,P2,P3)>>::add_to_app
             at /home/tim/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_app-0.14.0/src/plugin.rs:184:23
  17: bevy_app::app::App::add_plugins
             at /home/tim/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_app-0.14.0/src/app.rs:552:9
  18: invasion_by_nightfall::main
             at ./src/main.rs:14:5
  19: core::ops::function::FnOnce::call_once
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Instead, I would expect an error message explaining that the StatesPlugin needs to be added before handling states.

TimJentzsch avatar Jul 05 '24 13:07 TimJentzsch

I ran into this panic as well. In my case, the problem was not adding StatesPlugin to my app. (at a glance, looks like the same issue for you)

I agree that the error message is quite bad, and we probably shouldn't be crashing, but logging an error/warning or something.

rparrett avatar Jul 05 '24 13:07 rparrett

I ran into this panic as well. In my case, the problem was not adding StatesPlugin to my app. (at a glance, looks like the same issue for you)

I agree that the error message is quite bad, and we probably shouldn't be crashing, but logging an error/warning or something.

Ah, you are correct. Actually, I already added DefaultPlugins which contains StatesPlugin, but apparently in the wrong plugin of my app, causing it to get loaded too late. Kinda surprising to me, I thought plugins were assembled before the rest.

In any case, I will adjust the issue to reflect that this is more of a docs/error message problem.

TimJentzsch avatar Jul 05 '24 13:07 TimJentzsch

I ran into this error message in 0.14. I was calling init_state before add_plugins (with the DefaultPlugins). That worked fine prior to 0.14. The fix was to move init_state to after the add_plugins call

oliver-dew avatar Jul 09 '24 19:07 oliver-dew

Closing as resolved due to #14160.

alice-i-cecile avatar Jul 15 '24 13:07 alice-i-cecile