AGENT SKILL

MSBuild 项目项管理

item-management

处理具体的 MSBuild ItemGroup 和项表达式(item-expression)问题。用于:Include、Remove、Update、项标识(item identity)与元数据(metadata)、变换(transforms)、筛选(filtering)、批处理(batching)、重复或重叠项,以及验证这些操作是否正确的审查。当核心缺陷是项标识、元数据、批处理、重复声明或 glob/Remove/Update 语义时,生成的项(generated items)仍在范围内。对于生成的工件未出现在编译或输出中、目标时序/路径错误,或没有更广泛项语义缺陷的 FileWrites 清理跟踪,请使用 including-generated-files。项操作可能是损坏的、被怀疑的或已经正确的。排除仅属性相关的问题、没有项问题的常规增量构建、没有具体项关注点的广泛审查,以及非 MSBuild 系统。

License 许可
MIT
所属仓库
microsoft/testfx
最近更新
2026-11-14
Stars
★ 1.1k
Forks
315

01 前言 Front Matter

name
item-management
license
MIT
description
Own concrete MSBuild ItemGroup and item-expression questions. USE FOR: Include, Remove, Update, item identity and metadata, transforms, filtering, batching, duplicate or overlapping items, and reviews that verify whether those operations are correct. Generated items stay in scope when the central defect is item identity, metadata, batching, duplicate declarations, or glob/Remove/Update semantics. For a generated artifact missing from compilation or output, wrong target timing/path, or FileWrites clean tracking without a broader item-semantics defect, use including-generated-files. The item operation may be broken, suspected, or already correct. Exclude property-only issues, general incrementality with no item question, broad reviews with no concrete item concern, and non-MSBuild systems.

03 SKILL.md 正文

MSBuild Item Management Patterns

Canonical patterns for working with item groups, from Microsoft.Common.CurrentVersion.targets.

Include / Remove / Update — Three Operations

Operation Purpose When to use
Include Add new items to the group Creating items with identity + metadata
Remove Remove items matching a pattern Excluding files or clearing a group
Update Modify metadata on existing items Adding/changing metadata without re-adding

Include — Add Items

<ItemGroup>
  <Compile Include="Generated\*.cs">
    <AutoGen>true</AutoGen>
  </Compile>
</ItemGroup>

Remove — Subtract Items

<ItemGroup>
  <!-- Remove specific items -->
  <Reference Remove="$(AdditionalExplicitAssemblyReferences)" />

  <!-- Set subtraction: prior minus current -->
  <_CleanOrphanFileWrites Include="@(_CleanPriorFileWrites)"
      Exclude="@(_CleanCurrentFileWrites)" />

  <!-- Clear an entire group -->
  <_Temporary Remove="@(_Temporary)" />
</ItemGroup>

Update — Modify Existing Items

<ItemGroup>
  <EmbeddedResource Update="@(EmbeddedResource)"
      Condition="'%(NuGetPackageId)' == 'Microsoft.CodeAnalysis.Collections'">
    <GenerateSource>true</GenerateSource>
    <ClassName>Microsoft.CodeAnalysis.Collections.SR</ClassName>
  </EmbeddedResource>
</ItemGroup>

Update does not add items — it only modifies items already in the group.

Item Batching — %(Metadata)

When %(Metadata) appears in target attributes or task parameters, MSBuild batches execution per unique metadata value.

Target-level batching (Outputs)

<Target Name="GenerateSatelliteAssemblies"
    Inputs="$(MSBuildAllProjects);@(_SatelliteAssemblyResourceInputs)"
    Outputs="$(IntermediateOutputPath)%(Culture)\$(TargetName).resources.dll">
  <!-- Runs once per unique Culture value -->
</Target>

Task-level batching

<Copy SourceFiles="@(_SourceItems)"
    DestinationFiles="@(_SourceItems->'$(OutDir)%(TargetPath)')">
</Copy>

Per-item filtering with Condition

<ItemGroup>
  <_ResxOutput Include="@(EmbeddedResource->'%(OutputResource)')"
      Condition="'%(EmbeddedResource.WithCulture)' == 'false'" />
</ItemGroup>

Batching rules

  • %(Metadata) in Condition or Outputs → target batches per unique value.
  • %(Metadata) in task parameters → task batches per unique value.
  • Do not mix %() from different item groups in the same expression — this causes a cross-product (see Common Pitfalls).

Item Transforms — @(Item->'expression')

Transforms create new item lists by applying an expression to each item:

<!-- Transform file paths to destinations -->
<Copy SourceFiles="@(IntermediateAssembly)"
    DestinationFiles="@(IntermediateAssembly->'$(OutDir)%(Filename)%(Extension)')"/>

<!-- Transform with separator for display -->
<Message Text="Files: @(Compile->'%(Filename)', ', ')" />

Exclude Pattern — Set Subtraction on Include

<ItemGroup>
  <Compile Include="**\*.cs" Exclude="Generated\**;Tests\**" />
</ItemGroup>

Exclude only works on Include — it cannot be used with Update or Remove.

Conditional Item Inclusion

<!-- Condition on ItemGroup — all or nothing -->
<ItemGroup Condition="'$(NetCoreBuild)' == 'true'">
  <PackageReference Include="System.IO.Pipelines" />
</ItemGroup>

<!-- Condition on individual items -->
<ItemGroup>
  <PackageReference Include="System.IO.Pipelines"
      Condition="'$(NetCoreBuild)' == 'true'" />
</ItemGroup>

PrivateAssets on Tool/Analyzer Packages

<ItemGroup>
  <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" PrivateAssets="all" />
  <PackageReference Include="StyleCop.Analyzers" PrivateAssets="all" />
</ItemGroup>

Common Pitfalls

Cross-product batching

Referencing %(Metadata) from two different item groups creates O(N×M) executions:

<!-- BAD: Cross-product of @(Source) × @(Config) -->
<Exec Command="process %(Source.Identity) with %(Config.Identity)" />

<!-- GOOD: Reference one group via batching, the other via property -->
<Exec Command="process %(Source.Identity) with $(ConfigFile)" />

Generated files in source tree

Write to $(IntermediateOutputPath) (obj/), not the source directory. Source-tree generation pollutes version control and can cause duplicate compilation via globs.

Missing FileWrites

Every file created during a target must be added to @(FileWrites) for dotnet clean support.

04 同仓库更多 Skills

microsoft
microsoft/testfx

dotnet-breaking-changes

Use before changing .NET/C# code whose callers you do not control: a NuGet library, or a repo with PublicAPI.Shipped/Unshipped.txt, ApiCompat, multiple TargetFrameworks, InternalsVisibleTo, or generated (.g.cs) partials. Green builds miss silent breaks from optional parameters, parameter names, sealing, const values, nullability, and #if. USE FOR: "without breaking existing callers"; adding a public method, overload, optional parameter, or interface member to a shipped library; renaming a public method or an internal helper a friend assembly uses; consolidating duplicated logic; changing a signature, nullability, default, or attribute; adding a target framework; RS0016/RS0017 or ApiCompat errors; "is this PR breaking?". For a rename, extract, or merge, also invoke csharp-refactoring. DO NOT USE FOR: an executable whose callers all live in one project; renaming a local variable or private member, even in a shipped library; formatting; framework/SDK/NuGet upgrades (use dotnet-upgrade).

★ 1.1k
microsoft
microsoft/testfx

TestFx 测试值证据应用

testfx-test-value-evidence

结合 TestFx 工具链、断言、并行安全、打包消费者(packed-consumer)和评审约定,应用项目内置的 test-value-evidence 技能。在实现或评审会改变行为的 TestFx PR 或提交时使用,包括仅修改源代码的变更。

测试工程师测试与质量保障
★ 1.1k
microsoft
microsoft/testfx

TestFx 并行安全预检

testfx-parallel-safety-preflight

在实现或重构 TestFx 测试、生命周期代码、辅助函数、共享状态声明,或这些测试所覆盖的生产代码路径之前,检查并发性与隔离性。在缩小 DoNotParallelize 或 ResourceLock 的使用范围、更改并行化设置或引入异步工作线程之前使用;并针对最终 diff 重复检查。

测试工程师测试与质量保障
★ 1.1k
microsoft
microsoft/testfx

TestFx 本地化校验

testfx-localization-validation

验证生成的 TestFx 本地化目录、资源 ID、占位符和锁定标记。适用于 RESX/XLF 变更、OneLoc PR、本地化构建失败、重复的目标文本或翻译单元,以及孤立的翻译条目。

测试工程师测试与质量保障
★ 1.1k
microsoft
microsoft/testfx

TestFx 验收验证

testfx-acceptance-validation

使用打包后的消费者、包与缓存来源、精确的退出码和产物,以及选中测试与实际执行测试的证据,验证 TestFx 的交付路径及有效的 CI 执行。适用于验收回归、包/MSBuild/启动器/协议变更,或仅有跳过测试的 CI 结果;不适用于无关的纯单元测试或文档变更。

测试工程师测试与质量保障
★ 1.1k
microsoft
microsoft/testfx

会话交付规范管控

session-delivery-hygiene

把控本地会话范围、持久化交付、实时 PR 状态、集成来源以及责任范围内的清理工作。在实施指导变更、修改或推送 PR、诊断包与产物不匹配,或交接实现工作之前使用。

软件开发工程师代码仓库协作管理
★ 1.1k