Make sure #[track_caller] in EntityCommands::insert_if_neq works as intended#23986
Conversation
There was a problem hiding this comment.
Pull request overview
Updates EntityCommands::insert_if_neq to correctly preserve #[track_caller] information through the deferred command closure, ensuring get_changed_by reports the user callsite as intended (fixing #23962).
Changes:
- Capture
MaybeLocation::caller()outside the queued closure and thread it throughEntityWorldMut::insert_with_caller. - Switch insertion in
insert_if_neqtoinsert_with_caller(..., InsertMode::Replace, caller, RelationshipHookMode::Run). - Add a unit test intended to validate caller tracking for
insert_if_neq.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
alice-i-cecile
left a comment
There was a problem hiding this comment.
Code looks correct, but test needs fixing.
| command_queue.apply(&mut world); | ||
| world.clear_trackers(); | ||
|
|
||
| macro_rules! insert_if_neq_with_expected_caller { |
There was a problem hiding this comment.
Oh, does using a macro let you get the same Location for both places? That's a really neat trick!
| let entity = Commands::new(&mut command_queue, &world) | ||
| .spawn(P(41u8)) | ||
| .id(); | ||
| command_queue.apply(&mut world); | ||
| world.clear_trackers(); |
There was a problem hiding this comment.
This might be simpler if the spawn didn't go through Commands. ... Ah, although I see the previous test does the same thing, so maybe it's better to be consistent.
| let entity = Commands::new(&mut command_queue, &world) | |
| .spawn(P(41u8)) | |
| .id(); | |
| command_queue.apply(&mut world); | |
| world.clear_trackers(); | |
| let entity = world.spawn(P(41u8)).id(); |
intended
Objective
Fixes #23962
Solution
As suggested in the issue: "go back to insert_with_caller so that we can call MaybeLocation::caller() outside of the closure and then pass it through explicitly".
Testing
Added a unit test and made sure it fails without the fix.