diff --git a/crates/bevy_ecs/src/world/entity_ref.rs b/crates/bevy_ecs/src/world/entity_ref.rs index 618b34cbe80c7..7697d2092c4f6 100644 --- a/crates/bevy_ecs/src/world/entity_ref.rs +++ b/crates/bevy_ecs/src/world/entity_ref.rs @@ -2670,9 +2670,6 @@ impl<'w> EntityWorldMut<'w> { table_row: swapped_location.table_row, }), ); - world - .entities - .mark_spawn_despawn(swapped_entity.index(), caller, change_tick); } } table_row = remove_result.table_row; @@ -2702,14 +2699,18 @@ impl<'w> EntityWorldMut<'w> { table_row, }), ); - world - .entities - .mark_spawn_despawn(moved_entity.index(), caller, change_tick); } world.archetypes[moved_location.archetype_id] .set_entity_table_row(moved_location.archetype_row, table_row); } world.flush(); + + // SAFETY: `self.entity` is a valid entity index + unsafe { + world + .entities + .mark_spawn_despawn(self.entity.index(), caller, change_tick); + } } /// Ensures any commands triggered by the actions of Self are applied, equivalent to [`World::flush`] @@ -6740,4 +6741,28 @@ mod tests { } ); } + + #[test] + fn spawned_after_swap_remove() { + #[derive(Component)] + struct Marker; + + let mut world = World::new(); + let id1 = world.spawn(Marker).id(); + let _id2 = world.spawn(Marker).id(); + let id3 = world.spawn(Marker).id(); + + #[cfg(feature = "track_location")] + let e1_spawned = world.entity(id1).spawned_by(); + + let spawn = world.entity(id3).spawned_by(); + world.entity_mut(id1).despawn(); + #[cfg(feature = "track_location")] + let e1_despawned = world.entities().entity_get_spawned_or_despawned_by(id1); + #[cfg(feature = "track_location")] + assert_ne!(e1_spawned.map(Some), e1_despawned); + + let spawn_after = world.entity(id3).spawned_by(); + assert_eq!(spawn, spawn_after); + } } diff --git a/tools/ci/src/commands/test.rs b/tools/ci/src/commands/test.rs index 68fc9ba67c5dd..ae449f6a8fec2 100644 --- a/tools/ci/src/commands/test.rs +++ b/tools/ci/src/commands/test.rs @@ -20,7 +20,7 @@ impl Prepare for TestCommand { PreparedCommand::new::( cmd!( sh, - "cargo test --workspace --lib --bins --tests {no_fail_fast...} {jobs_ref...} -- {test_threads_ref...}" + "cargo test --workspace --lib --bins --tests --features bevy_ecs/track_location {no_fail_fast...} {jobs_ref...} -- {test_threads_ref...}" ), "Please fix failing tests in output above.", ),