{"record":{"id":"4ce9b3f65b06631f","repo":"bevyengine/bevy","slug":"the-collection-in-relationshiptarget-must-be-priva","errorCode":null,"errorMessage":"The collection in RelationshipTarget must be private to prevent users from directly mutating it, which could invalidate the correctness of relationships.","messagePattern":"The collection in RelationshipTarget must be private to prevent users from directly mutating it, which could invalidate the correctness of relationships\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bevy_ecs/macro_logic/src/component.rs","lineNumber":479,"sourceCode":"        let Some(relationship_target) = &self.relationship_target else {\n            return Ok(None);\n        };\n\n        let Data::Struct(DataStruct {\n            fields,\n            struct_token,\n            ..\n        }) = &ast.data\n        else {\n            return Err(syn::Error::new(\n                ast.span(),\n                \"RelationshipTarget can only be derived for structs.\",\n            ));\n        };\n        let field = relationship_field(fields, \"RelationshipTarget\", struct_token.span())?;\n\n        if field.vis != Visibility::Inherited {\n            return Err(syn::Error::new(field.span(), \"The collection in RelationshipTarget must be private to prevent users from directly mutating it, which could invalidate the correctness of relationships.\"));\n        }\n        let collection = &field.ty;\n        let relationship_member = field.ident.clone().map_or(Member::from(0), Member::Named);\n\n        let members = fields\n            .members()\n            .filter(|member| member != &relationship_member);\n\n        let relationship = &relationship_target.relationship;\n        let struct_name = &ast.ident;\n        let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();\n        let linked_spawn = relationship_target.linked_spawn;\n        let fqdefault = FQDefault.into_token_stream();\n        Ok(Some(quote! {\n            impl #impl_generics #bevy_ecs::relationship::RelationshipTarget for #struct_name #type_generics #where_clause {\n                const LINKED_SPAWN: bool = #linked_spawn;\n                type Relationship = #relationship;\n                type Collection = #collection;","sourceCodeStart":461,"sourceCodeEnd":497,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_ecs/macro_logic/src/component.rs#L461-L497","documentation":"Compile-time error from derive_relationship_target: the field holding the RelationshipTarget's entity collection is not private. Visibility must be inherited because direct user mutation could break relationship invariants that the derive maintains through its hooks.","triggerScenarios":"Thrown at crates/bevy_ecs/macro_logic/src/component.rs:479 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Make the collection field private (no pub/pub(crate))","Expose read-only access via methods on the RelationshipTarget type"],"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"}