extension-points
它能做什么
这项技能解释并追踪 MSBuild 如何找到扩展点,例如自定义钩子以及 NuGet 包添加的构建文件。适合需要弄清自定义构建步骤为何缺失或执行顺序错误的构建工程师。
适用场景
- ✓当自定义构建钩子或导入没有运行时。
- ✓当 NuGet 包的构建文件似乎缺失或被替换时。
- ✓当你想知道某项构建自定义是从哪里加载的时候。
技能链接
https://github.com/dotnet/skills/blob/main/plugins/dotnet-msbuild/skills/extension-points/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的 NuGet 包的构建目标在我的项目中没有运行。请追踪 MSBuild 从哪里加载它们,以及原因。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会沿着标准构建文件的导入顺序查找,检查钩子属性和包的布局,并说明哪个文件被加载、哪个被跳过。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
MSBuild Extension Points
How the MSBuild pipeline provides hooks for SDKs, NuGet packages, repos, and users to inject custom logic.
CustomBefore / CustomAfter Hooks
Every major .targets file defines import hooks:
<PropertyGroup>
<CustomBeforeMicrosoftCommonTargets Condition="'$(CustomBeforeMicrosoftCommonTargets)' == ''">
$(MSBuildExtensionsPath)\v$(MSBuildToolsVersion)\Custom.Before.Microsoft.Common.targets
</CustomBeforeMicrosoftCommonTargets>
</PropertyGroup>
<Import Project="$(CustomBeforeMicrosoftCommonTargets)"
Condition="'$(CustomBeforeMicrosoftCommonTargets)' != '' and Exists('$(CustomBeforeMicrosoftCommonTargets)')"/>
<!-- ... core targets ... -->
<Import Project="$(CustomAfterMicrosoftCommonTargets)"
Condition="'$(CustomAfterMicrosoftCommonTargets)' != '' and Exists('$(CustomAfterMicrosoftCommonTargets)')"/>
Rules
- Default path includes version (
v$(MSBuildToolsVersion)) for side-by-side installations. - Always check
Exists(). The file may not be present on every machine. - Append to the property (don't overwrite) to chain multiple hooks:
<PropertyGroup>
<CustomBeforeMicrosoftCommonTargets>
$(CustomBeforeMicrosoftCommonTargets);$(MSBuildThisFileDirectory)MyExtension.targets
</CustomBeforeMicrosoftCommonTargets>
</PropertyGroup>
Wildcard Import Directories
MSBuild imports all files in extension directories, sorted alphabetically:
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Imports\Microsoft.Common.props\ImportBefore\*"
Condition="'$(ImportByWildcardBeforeMicrosoftCommonProps)' == 'true'
and Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Imports\Microsoft.Common.props\ImportBefore')" />
Key paths
| Property | Resolves to | Scope |
|---|---|---|
$(MSBuildUserExtensionsPath) |
%APPDATA%\Microsoft\MSBuild |
Per-user |
$(MSBuildExtensionsPath) |
MSBuild install directory | Machine-wide |
$(MSBuildProjectExtensionsPath) |
obj/ directory |
Per-project (NuGet) |
Name files with numeric prefixes for ordering: 01-first.props, 02-second.props.
Import Gating — Control Properties
Every wildcard import is gated by a boolean property:
<PropertyGroup>
<ImportByWildcardBeforeMicrosoftCommonProps
Condition="'$(ImportByWildcardBeforeMicrosoftCommonProps)' == ''">true</ImportByWildcardBeforeMicrosoftCommonProps>
<ImportDirectoryBuildProps
Condition="'$(ImportDirectoryBuildProps)' == ''">true</ImportDirectoryBuildProps>
</PropertyGroup>
Available control properties
| Property | What it disables |
|---|---|
ImportDirectoryBuildProps |
Directory.Build.props auto-discovery |
ImportDirectoryBuildTargets |
Directory.Build.targets auto-discovery |
ImportProjectExtensionProps |
NuGet-generated *.props in obj/ |
ImportProjectExtensionTargets |
NuGet-generated *.targets in obj/ |
ImportByWildcardBefore* |
Machine-level ImportBefore extensions |
ImportByWildcardAfter* |
Machine-level ImportAfter extensions |
NuGet Package Build Extension Layout
NuGet packages inject build logic via build/ or buildTransitive/ folders:
MyPackage/
build/
MyPackage.props ← imported via *.props wildcard
MyPackage.targets ← imported via *.targets wildcard
buildTransitive/
MyPackage.props ← imported by transitive consumers
MyPackage.targets
Rules
- File names must match the package ID exactly.
build/affects direct consumers only.buildTransitive/affects the entire dependency chain.- Props are imported early (before the project), targets are imported late (after the project).
Forwarding chain: buildTransitive/ → build/ → shared
Forward buildTransitive/*.props and buildTransitive/*.targets through their sibling build/*.props / build/*.targets files (chain buildTransitive → build → shared) instead of importing buildMultiTargeting/ directly. This keeps build/ as the single source of truth with a clear ownership chain, so transitive consumers stay in sync with direct consumers instead of the two layouts drifting apart.
When build/ is packed per-TFM (build/<tfm>/, via TfmSpecificPackageFile, a per-TFM <PackagePath>, or SDK conventions) while buildMultiTargeting/ is not, a buildTransitive/<tfm>/ forwarder must include the TFM segment — dropping it resolves to a non-existent package-root build/MyPackage.props and fails tra
常见问题
什么是 CustomBefore 钩子?
它是一个钩子文件,MSBuild 会在主构建目标之前导入它,因此可以添加自定义步骤。
它涵盖 NuGet 构建文件吗?
涵盖。它包括包的构建文件如何被导入和匹配。
它会改动构建吗?
它只解释和追踪导入过程,任何改动都在审阅后进行。
详情
作者的原始描述
Own MSBuild import and hook discovery. USE FOR: CustomBefore/CustomAfter hooks, ordered wildcard and NuGet auto-imports, control properties, build/buildTransitive packed layout, package ID and file-name matching, per-TFM forwarders, and tracing why assets or hooks are missing, broken, or replaced. An import guard remains in scope when it is one defect in a broader hook/import flow. DO NOT USE for a focused safety verdict on whether one specific Import needs Exists or is an intentionally unguarded package contract; use msbuild-antipatterns. NEVER INVOKE when imports and hook placement already w
repo: dotnet/skills
热度
- GitHub 星标
- 5.8k
- 排名
- #14600
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
create-custom-agent
5.8k帮你的智能体创建 VS Code 自定义智能体文件,为每个智能体设置专属工具、指令以及向其他智能体的交接。
create-skill
5.8k帮你的智能体创建新的智能体技能,包括有效的名称、清晰的触发描述,以及符合标准的 SKILL.md 文件。
csharp-scripts
5.8k帮你的智能体通过 .NET 命令行工具以单个文件运行小型 C# 程序,无需创建完整项目。
dotnet-pinvoke
5.8k帮你的智能体在 .NET 中正确调用原生 C 或 C++ 库,涵盖函数签名、字符串处理、内存和安全句柄。