Skip to content

Track exact source locations on evaluated items - #14731

Open
AlesProkop wants to merge 3 commits into
dotnet:mainfrom
AlesProkop:dev/alesprokop/item-source-location-draft
Open

Track exact source locations on evaluated items#14731
AlesProkop wants to merge 3 commits into
dotnet:mainfrom
AlesProkop:dev/alesprokop/item-source-location-draft

Conversation

@AlesProkop

Copy link
Copy Markdown
Member

Related to https://dev.azure.com/devdiv/DevDiv/_workitems/edit/2905535

Context

Tasks currently receive evaluated items without the exact source XML location, preventing diagnostics such as NU1510 from navigating to the declaring PackageReference. Reconstructing the location later from the item type and evaluated include is ambiguous, so this records the location when the item is created.

Changes Made

  • Added ITaskItem3.Location and the immutable TaskItemLocation value type.
  • Added source line and column storage to ProjectItemInstance.TaskItem, reusing the existing defining-project path for the file.
  • Preserved item locations through evaluation snapshots, target item creation, task outputs, cloning, and node serialization.
  • Preserved locations when copying items into Microsoft.Build.Utilities.TaskItem and TaskItem<T>.

Testing

  • Built the repository in Debug and Release configurations with 0 warnings and 0 errors.
  • Added and ran focused tests:
    • ProjectItemInstance_Tests.LocationTracksOriginatingItemElement — verifies evaluated items and copied utility items retain the originating XML element location.
    • TaskItem_Tests.Serialization — verifies line and column survive node serialization.
    • Result: passed on net11.0 and net472.
  • Measured ProjectInstance creation with 1,000, 10,000, and 100,000 items:
    • Allocation delta: approximately 8 bytes per item.
  • Measured OrchardCore at commit e872f0f0c8e977a927f883d662d9768c86f050c3:
    • 474 projects and 225,872 evaluated items.
    • 30 randomized fresh-process runs per variant.
    • Retained managed-memory delta: 1.66 MB, or 7.34 bytes per item.
    • Elapsed-time delta 95% confidence interval: -155 ms to +12 ms.

Notes

  • This PR exposes source locations; NuGet and potentially Project System still need to consume them for the reported warning.
  • Legacy out-of-proc task parameter serialization is unchanged, so location may be unavailable after crossing that boundary.
  • Location identifies the immediate XML element that produced the item; it is not a full provenance chain.

AlesProkop and others added 3 commits August 18, 2026 12:42
Capture the source line and column when items are created and preserve them through cloning and node serialization.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 5a5ff63d-77fe-442e-892d-53ae50064596
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 076fff95-d760-471e-8afc-423a9701f8a5
@AlesProkop
AlesProkop marked this pull request as ready for review August 20, 2026 09:03
Copilot AI lite review requested due to automatic review settings August 20, 2026 09:03

Copilot AI left a comment

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.

Pull request overview

Adds end-to-end propagation of precise MSBuild item source locations (file/line/column) from evaluation through task execution, enabling downstream tooling (for example NuGet) to report diagnostics that can navigate back to the originating <Item ... /> XML element.

Changes:

  • Introduces ITaskItem3.Location plus a new immutable TaskItemLocation value type to expose item-origin locations to tasks and consumers.
  • Stores and preserves source line/column on ProjectItemInstance task items across cloning, snapshots, task outputs, and node serialization.
  • Updates key item-production paths (evaluation, intrinsic item transforms, task output inference) and adds focused unit tests validating location retention and serialization.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/Utilities/TaskItem.cs Implements ITaskItem3 and copies/publishes source location when wrapping/copying items for tasks.
src/Framework/TaskItemLocation.cs Adds new public TaskItemLocation struct implementing IMSBuildElementLocation.
src/Framework/TaskItem_T.cs Extends TaskItem<T> to implement ITaskItem3 and forward Location from backing items.
src/Framework/ITaskItem3.cs Adds new ITaskItem3 interface exposing TaskItemLocation? Location.
src/Build/Instance/ProjectItemInstance.cs Stores line/column on the backing task item, exposes Location, and serializes the new fields across nodes.
src/Build/Instance/ProjectInstance.cs Captures XML element line/column at evaluation time when instantiating ProjectItemInstance.
src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs Propagates task parameter XML location onto task-emitted output items.
src/Build/BackEnd/Components/RequestBuilder/TaskBuilder.cs Preserves source location when inferring task outputs via ProjectItemInstanceFactory.
src/Build/BackEnd/Components/RequestBuilder/IntrinsicTasks/ItemGroupIntrinsicTask.cs Preserves source location when expanding/translating items in intrinsic ItemGroup handling.
src/Build.UnitTests/InstanceFromRemote/FakeProjectItemElementLink.cs Implements a non-throwing Location for the fake link used by remote-instance tests.
src/Build.UnitTests/Instance/TaskItem_Tests.cs Extends serialization test to assert location survives translation.
src/Build.OM.UnitTests/Instance/ProjectItemInstance_Tests.cs Adds test asserting evaluated items (and copied utility items) retain originating XML location.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants