-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Provide MSB4191 documentation content for transfer to msbuild-api-docs #13054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/fix-item-condition-metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+172
−0
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3358b81
Initial plan
Copilot e68a8ed
Add documentation for item metadata condition limitation (MSB4191)
Copilot ce66be7
Revert documentation changes - to be moved to msbuild-api-docs repo
Copilot 71d5e19
Add documentation content for transfer to msbuild-api-docs
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| # Common MSBuild Gotchas and Limitations | ||
|
|
||
| This document describes common pitfalls, limitations, and unexpected behaviors in MSBuild that developers frequently encounter. | ||
|
|
||
| ## Item Metadata in Conditions Outside of Targets | ||
|
|
||
| ### The Issue | ||
|
|
||
| When using `ItemGroup` elements with conditions that reference item metadata (using the `%(ItemType.MetadataName)` syntax), these conditions only work inside `Target` elements, not at the project level (outside of targets). | ||
|
|
||
| ### Error Message | ||
|
|
||
| If you try to use item metadata in a condition outside of a target, you'll get an error like: | ||
|
|
||
| ``` | ||
| error MSB4191: The reference to custom metadata "X" at position 1 is not allowed in this condition "'%(Content.X)' == 'abc'". | ||
| ``` | ||
|
|
||
| ### Example | ||
|
|
||
| This **does NOT work** at the project level: | ||
|
|
||
| ```xml | ||
| <Project> | ||
| <ItemGroup> | ||
| <Content Include="file1.txt"> | ||
| <X>abc</X> | ||
| </Content> | ||
| <Content Include="file2.txt"> | ||
| <X>def</X> | ||
| </Content> | ||
| </ItemGroup> | ||
|
|
||
| <!-- This will FAIL with MSB4191 error --> | ||
| <ItemGroup> | ||
| <FilteredContent Include="@(Content)" Condition="'%(Content.X)' == 'abc'" /> | ||
| </ItemGroup> | ||
| </Project> | ||
| ``` | ||
|
|
||
| This **DOES work** inside a target: | ||
|
|
||
| ```xml | ||
| <Project> | ||
| <ItemGroup> | ||
| <Content Include="file1.txt"> | ||
| <X>abc</X> | ||
| </Content> | ||
| <Content Include="file2.txt"> | ||
| <X>def</X> | ||
| </Content> | ||
| </ItemGroup> | ||
|
|
||
| <Target Name="FilterItems"> | ||
| <!-- This works inside a target --> | ||
| <ItemGroup> | ||
| <FilteredContent Include="@(Content)" Condition="'%(Content.X)' == 'abc'" /> | ||
| </ItemGroup> | ||
| <Message Text="FilteredContent: @(FilteredContent)" /> | ||
| </Target> | ||
| </Project> | ||
| ``` | ||
|
|
||
| ### Why This Happens | ||
|
|
||
| The `%(ItemType.MetadataName)` syntax implies **batching** - evaluating the condition once for each distinct value of the metadata. Batching only happens inside `Target` elements during target execution, not during project evaluation (which processes everything outside of targets). | ||
|
|
||
| Outside of targets, MSBuild evaluates each element as a single entity. The batching infrastructure required to split items into buckets based on metadata values is only available during target execution. | ||
|
|
||
| ### Workarounds | ||
|
|
||
| #### 1. Move the Item Filtering to a Target | ||
|
|
||
| The simplest solution is to move your item filtering logic into a target: | ||
|
|
||
| ```xml | ||
| <Target Name="FilterItems" BeforeTargets="Build"> | ||
| <ItemGroup> | ||
| <FilteredContent Include="@(Content)" Condition="'%(Content.X)' == 'abc'" /> | ||
| </ItemGroup> | ||
| </Target> | ||
| ``` | ||
|
|
||
| #### 2. Use `WithMetadataValue` Item Function | ||
|
|
||
| For project-level filtering, use the `->WithMetadataValue()` item function instead: | ||
|
|
||
| ```xml | ||
| <ItemGroup> | ||
| <!-- This works at project level --> | ||
| <FilteredContent Include="@(Content->WithMetadataValue('X', 'abc'))" /> | ||
| </ItemGroup> | ||
| ``` | ||
|
|
||
| The `WithMetadataValue` function is specifically designed for filtering items by metadata at the project level without requiring batching. | ||
|
|
||
| #### 3. Use Other Item Functions | ||
|
|
||
| MSBuild provides several [item functions](https://learn.microsoft.com/visualstudio/msbuild/item-functions) that can be used at the project level: | ||
|
|
||
| ```xml | ||
| <ItemGroup> | ||
| <!-- Filter using metadata value --> | ||
| <FilteredContent Include="@(Content->WithMetadataValue('X', 'abc'))" /> | ||
|
|
||
| <!-- Check if metadata exists --> | ||
| <ItemsWithMetadata Include="@(Content->HasMetadata('X'))" /> | ||
|
|
||
| <!-- Transform based on metadata --> | ||
| <TransformedContent Include="@(Content->'%(X)')" /> | ||
| </ItemGroup> | ||
| ``` | ||
|
|
||
| ### Related Information | ||
|
|
||
| - [MSBuild Batching](https://learn.microsoft.com/visualstudio/msbuild/msbuild-batching) | ||
| - [Item Functions](https://learn.microsoft.com/visualstudio/msbuild/item-functions) | ||
| - [MSBuild Items](https://learn.microsoft.com/visualstudio/msbuild/msbuild-items) | ||
|
|
||
| ### Further Reading | ||
|
|
||
| For more context on why this limitation exists, see: | ||
| - [MSBuild Architecture Overview](../Contributions/MSBuild-overview.md) - explains the difference between evaluation and execution phases | ||
| - Issue [#3520](https://github.com/dotnet/msbuild/issues/3520) - original issue tracking this limitation | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue number referenced here (#3520) does not match the issue number mentioned in the PR description (#3479). The link should point to the correct issue that this documentation is addressing.