Add a new buildcheck that encourages MSBuild logic authors to dispose of unused Private Items from their Targets #12887
+1,491
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add BC0303 BuildCheck: Detect undisposed private item lists in targets
Summary
Adds a new BuildCheck analyzer (BC0303) that detects memory waste from undisposed private item lists in MSBuild targets.
Problem
MSBuild targets often create temporary "private" item lists (conventionally prefixed with
_) for internal calculations. These items persist in memory for the duration of the build unless explicitly removed, which can waste memory in large builds.Example of problematic code:
Solution
This PR introduces BC0303 - ItemDisposalCheck, a new built-in BuildCheck analyzer that:
_) created within targets viaIncludeRemoveoperationOutputsorReturnsattributes (part of the target's public contract)Technical Details
Implementation
src/Build/BuildCheck/Checks/ItemDisposalCheck.csExpressionShredder.GetReferencedItemNamesAndMetadata()for robust parsing of Outputs/Returns expressionsWhy ExpressionShredder instead of Regex?
Initially considered regex-based parsing, but switched to MSBuild's built-in
ExpressionShredderto properly handle:@(_Items->'%(Identity);%(FullPath)')@(_Sources);@(_Resources);@(_Content)@(_Files, ';')@(_Duplicates->Distinct())Documentation
documentation/specs/BuildCheck/Codes.mddocumentation/specs/BuildCheck/ItemDisposalCheck.mdTesting
Added 31 comprehensive test cases in
src/BuildCheck.UnitTests/ItemDisposalCheck_Tests.cs:Positive tests (should fire):
Negative tests (should NOT fire):
_prefix)OutputsorReturnsEdge cases:
All 31 tests pass ✅
Related Work
This analyzer was created using the msbuild-buildcheck-creator agent, following established patterns in the BuildCheck infrastructure.