{"record":{"id":"61458ac6c8ca1fcf","repo":"bevyengine/bevy","slug":"resource-was-inserted-during-a-call-to-world","errorCode":null,"errorMessage":"Resource `{}` was inserted during a call to World::resource_scope, which may result in unexpected behavior.\\nIn release builds, the value inserted will be overwritten at the end of the scope.","messagePattern":"Resource `(.+?)` was inserted during a call to World::resource_scope, which may result in unexpected behavior\\.\\\\nIn release builds, the value inserted will be overwritten at the end of the scope\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bevy_ecs/src/world/mod.rs","lineNumber":2864,"sourceCode":"\n                // in debug mode, raise a panic if user code re-inserted a resource of this type within the scope.\n                // resource insertion usually indicates a logic error in user code, which is useful to catch at dev time,\n                // however it does not inherently lead to corrupted state, so we avoid introducing an unnecessary crash\n                // for production builds.\n                if entity_mut.contains_id(self.component_id) {\n                    #[cfg(debug_assertions)]\n                    {\n                        // if we're already panicking, log an error instead of panicking, as double-panics result in an abort\n                        #[cfg(feature = \"std\")]\n                        if std::thread::panicking() {\n                            log::error!(\"Resource `{}` was inserted during a call to World::resource_scope, which may result in unexpected behavior.\\n\\\n                                   In release builds, the value inserted will be overwritten at the end of the scope.\",\n                                   DebugName::type_name::<R>());\n                            // return early to maintain consistent behavior with non-panicking calls in debug builds\n                            return;\n                        }\n\n                        panic!(\"Resource `{}` was inserted during a call to World::resource_scope, which may result in unexpected behavior.\\n\\\n                               In release builds, the value inserted will be overwritten at the end of the scope.\",\n                               DebugName::type_name::<R>());\n                    }\n                    #[cfg(not(debug_assertions))]\n                    {\n                        #[cold]\n                        #[inline(never)]\n                        fn warn_reinsert(resource_name: &str) {\n                            warn!(\n                                \"Resource `{resource_name}` was inserted during a call to World::resource_scope: the inserted value will be overwritten.\",\n                            );\n                        }\n\n                        warn_reinsert(&DebugName::type_name::<R>());\n                    }\n                }\n\n                move_as_ptr!(value);","sourceCodeStart":2846,"sourceCodeEnd":2882,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_ecs/src/world/mod.rs#L2846-L2882","documentation":"While World::resource_scope has R removed, inserting R again through the borrowed world (world.insert_resource::<R>(...) or commands/schedules inside the closure) violates the scope's contract. Debug builds panic immediately (or log an error via log::error! if already panicking, to avoid double-panic abort); release builds emit a warn! and silently overwrite your inserted value when the scope re-inserts the original at the end. The resource you receive as Mut<R> is the intended way to change it.","triggerScenarios":"Inside the resource_scope closure, calling world.insert_resource(R) / init_resource::<R>() for the SAME type R, or running a schedule/system that inserts R (e.g. world.run_schedule(SomeLabel) where a system in that schedule does res insertion). Resetting a state machine resource inside its own scope is the classic case.","commonSituations":"Resetting a State/Score/Timer resource by inserting a fresh value instead of mutating the Mut<R> handed to the closure; running another schedule inside the scope whose systems initialize the same resource; generic code that 'ensures resource exists' via insert_resource being reused inside a scope.","solutions":["Mutate the resource in place through the Mut<R> the closure gives you (e.g. *state = State::new(...)) instead of inserting a new one.","If you truly need to replace it, have the closure set a flag/value and perform world.insert_resource AFTER resource_scope returns.","Audit any world.run_schedule / app.update / system run inside the closure for insert_resource of the scoped type and move it out of the scope.","In release builds heed the warn! 'inserted value will be overwritten' — it marks the same bug without the panic."],"exampleFix":"// before\nworld.resource_scope(|world, mut state: Mut<MyState>| {\n    world.insert_resource(MyState::Restarting); // panic (debug) / silently overwritten (release)\n});\n\n// after\nworld.resource_scope(|world, mut state: Mut<MyState>| {\n    *state = MyState::Restarting; // mutate in place\n});","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Inside resource_scope, always mutate the provided Mut<R> instead of inserting R again.","Do not run schedules/systems inside the closure that may insert the scoped resource type.","Watch release-build warn! logs ('inserted value will be overwritten') — they flag the same bug without the debug panic."],"tags":["bevy","ecs","resource","resource-scope","panic","aliasing"],"backgroundTag":"reentrant-resource-mutation","analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}