Skip to content

Make sure #[track_caller] in EntityCommands::insert_if_neq works as intended#23986

Merged
alice-i-cecile merged 3 commits intobevyengine:mainfrom
agluszak:fix-entity-commands-track-caller-23944
Apr 28, 2026
Merged

Make sure #[track_caller] in EntityCommands::insert_if_neq works as intended#23986
alice-i-cecile merged 3 commits intobevyengine:mainfrom
agluszak:fix-entity-commands-track-caller-23944

Conversation

@agluszak
Copy link
Copy Markdown
Contributor

@agluszak agluszak commented Apr 25, 2026

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.

Copilot AI review requested due to automatic review settings April 25, 2026 21:44
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 through EntityWorldMut::insert_with_caller.
  • Switch insertion in insert_if_neq to insert_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.

Comment thread crates/bevy_ecs/src/system/commands/mod.rs
Comment thread crates/bevy_ecs/src/system/commands/mod.rs Outdated
Comment thread crates/bevy_ecs/src/system/commands/mod.rs
Copy link
Copy Markdown
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks correct, but test needs fixing.

@alice-i-cecile alice-i-cecile requested a review from chescock April 25, 2026 21:56
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-ECS Entities, components, systems, and events X-Uncontroversial This work is generally agreed upon D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 25, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in ECS Apr 25, 2026
@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 25, 2026
@agluszak agluszak requested a review from alice-i-cecile April 25, 2026 22:07
@alice-i-cecile alice-i-cecile added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Apr 26, 2026
command_queue.apply(&mut world);
world.clear_trackers();

macro_rules! insert_if_neq_with_expected_caller {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, does using a macro let you get the same Location for both places? That's a really neat trick!

Comment on lines +2762 to +2766
let entity = Commands::new(&mut command_queue, &world)
.spawn(P(41u8))
.id();
command_queue.apply(&mut world);
world.clear_trackers();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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();

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 27, 2026
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Apr 28, 2026
Merged via the queue into bevyengine:main with commit 3968184 Apr 28, 2026
40 checks passed
@github-project-automation github-project-automation Bot moved this from Needs SME Triage to Done in ECS Apr 28, 2026
@agluszak agluszak deleted the fix-entity-commands-track-caller-23944 branch April 28, 2026 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Uncontroversial This work is generally agreed upon

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Make sure #[track_caller] in EntityCommands::insert_if_neq works as intended

4 participants