{"record":{"id":"783a4a001538cdcb","repo":"bevyengine/bevy","slug":"component-already-has-an-on-insert-hook","errorCode":null,"errorMessage":"Component already has an on_insert hook","messagePattern":"Component already has an on_insert hook","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bevy_ecs/src/lifecycle.rs","lineNumber":207,"sourceCode":"            .expect(\"Component already has an on_add hook\")\n    }\n\n    /// Register a [`ComponentHook`] that will be run when this component is added (with `.insert`)\n    /// or replaced.\n    ///\n    /// An `on_insert` hook always runs after any `on_add` hooks (if the entity didn't already have the component).\n    ///\n    /// # Warning\n    ///\n    /// The hook won't run if the component is already present and is only mutated, such as in a system via a query.\n    /// As a result, this needs to be combined with immutable components to serve as a mechanism for reliably updating indexes and other caches.\n    ///\n    /// # Panics\n    ///\n    /// Will panic if the component already has an `on_insert` hook\n    pub fn on_insert(&mut self, hook: ComponentHook) -> &mut Self {\n        self.try_on_insert(hook)\n            .expect(\"Component already has an on_insert hook\")\n    }\n\n    /// Register a [`ComponentHook`] that will be run when this component is about to be dropped,\n    /// such as being replaced (with `.insert`) or removed.\n    ///\n    /// If this component is inserted onto an entity that already has it, this hook will run before the value is replaced,\n    /// allowing access to the previous data just before it is dropped.\n    /// This hook does *not* run if the entity did not already have this component.\n    ///\n    /// An `on_discard` hook always runs before any `on_remove` hooks (if the component is being removed from the entity).\n    ///\n    /// # Warning\n    ///\n    /// The hook won't run if the component is already present and is only mutated, such as in a system via a query.\n    /// As a result, this needs to be combined with immutable components to serve as a mechanism for reliably updating indexes and other caches.\n    ///\n    /// # Panics\n    ///","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_ecs/src/lifecycle.rs#L189-L225","documentation":"Panic in ComponentLifecycleHook registration (on_insert): the try_on_insert call found an existing on_insert hook in the component's lifecycle slot. The expect guard prevents a second registration from overwriting the first, which would hide a real bug in hook wiring.","triggerScenarios":"Thrown at crates/bevy_ecs/src/lifecycle.rs:207 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Merge the new behavior into the already-registered on_insert hook closure","Audit plugins/extension traits for duplicate .on_insert(...) calls on the same component","Use try_on_insert and handle the None case explicitly when conflict is expected"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}