bevyengine / bevyengine/bevy

`Scene::write_to_world_with` multiple times with previously existing entities leads to inconsistent relationships.

Open
#18,418 0 comments 1 reaction 0 assignees View on GitHub
A-ECS A-Scenes C-Bug S-Needs-Design
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## Bevy version

f353cc33

## What you did

```
let mut app = App::empty();
app.init_resource::();
app.register_type::();
app.register_type::();

let mut scene = Scene {
world: World::new(),
};
let a = scene.world.spawn_empty().id();
let b = scene.world.spawn(ChildOf { parent: a }).id();
let c = scene.world.spawn(ChildOf { parent: b }).id();

let type_registry = app.world().resource::().clone();

let mut entity_map = Default::default();
scene
.write_to_world_with(app.world_mut(), &mut entity_map, &type_registry)
.unwrap();

let b = entity_map[&b];
let c = entity_map[&c];

let d = app.world_mut().spawn(ChildOf { parent: b }).id();

scene
.write_to_world_with(app.world_mut(), &mut entity_map, &type_registry)
.unwrap();

assert_eq!(app.world().entity(d).get::().unwrap().parent, b);

assert_eq!(
&app.world()
.entity(b)
.get::()
.unwrap()
.iter()
.cloned()
.collect::>(),
&[c, d]
);
```

## What went wrong

The relationships are inconsistent! We have a ChildOf component on `d`, but the `Children` of `b` does not contain `d`!

## Additional information

This is a result of #17858.

Contributor guide

Open the contributing guide

Research direction

Start at Scene::write_to_world_with and reproduce the issue with the code shown, focusing on how repeated writes handle existing entities and ChildOf/Children relationships. Trace the relationship updates during the second write. Done means the existing entity d remains linked to b and b's Children contains both c and d after the repeated write.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.