重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
including-generated-files by dotnet/skills
npx skills add https://github.com/dotnet/skills --skill including-generated-files构建过程中生成的文件通常会被构建过程忽略。这会导致一些令人困惑的结果,例如:
这种情况的发生是由于 MSBuild 的构建阶段工作方式所致。
对于构建过程中生成的代码文件,我们需要在生成文件的目标中,将它们添加到 Compile 和 FileWrites 项组中:
<ItemGroup>
<Compile Include="$(GeneratedFilePath)" />
<FileWrites Include="$(GeneratedFilePath)" />
</ItemGroup>
生成文件的目标应挂接到 CoreCompile 和 BeforeCompile 目标之前 - BeforeTargets="CoreCompile;BeforeCompile"
详细解释请参阅 MSBuild 如何构建项目。
MSBuild 读取您的项目,导入所有内容,创建属性,展开目标之外的项的 glob,并设置构建过程。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
MSBuild 使用提供的属性和项运行目标与任务以执行构建。
关键要点: 执行期间生成的文件在评估期间并不存在,因此无法被找到。这尤其影响默认通过 glob 匹配的文件,例如源文件(.cs)。
当文件在构建过程中生成时,需要手动将它们添加到构建过程中。具体方法取决于所生成文件的类型。
$(IntermediateOutputPath) 作为生成文件的位置始终使用 $(IntermediateOutputPath) 作为生成文件的基础目录。不要硬编码 obj\ 或手动构造中间路径(例如 obj\$(Configuration)\$(TargetFramework)\)。在某些构建配置中(例如共享输出目录、CI 环境),中间输出路径可能会被重定向到不同的位置。使用 $(IntermediateOutputPath) 可以确保您的目标无论实际路径如何都能正常工作。
FileWrites每个生成的文件都应添加到 FileWrites 项组中。这确保了 MSBuild 的 Clean 目标能够正确删除您生成的文件。如果没有这个步骤,生成的文件将在多次构建之间累积为过时的产物。
<ItemGroup>
<FileWrites Include="$(IntermediateOutputPath)my-generated-file.xyz" />
</ItemGroup>
对于需要复制到输出的生成文件(配置文件、数据文件等),请在 BeforeBuild 之前将它们添加到 Content 或 None 项中:
<Target Name="IncludeGeneratedFiles" BeforeTargets="BeforeBuild">
<!-- 您生成文件的逻辑放在这里 -->
<ItemGroup>
<None Include="$(IntermediateOutputPath)my-generated-file.xyz" CopyToOutputDirectory="PreserveNewest"/>
<!-- 使用通配符捕获特定类型的所有文件 -->
<None Include="$(IntermediateOutputPath)generated\*.xyz" CopyToOutputDirectory="PreserveNewest"/>
<!-- 注册生成的文件以便正确清理 -->
<FileWrites Include="$(IntermediateOutputPath)my-generated-file.xyz" />
<FileWrites Include="$(IntermediateOutputPath)generated\*.xyz" />
</ItemGroup>
</Target>
如果您正在生成需要编译的 .cs 文件,请使用 BeforeTargets="CoreCompile;BeforeCompile"。这是添加 Compile 项的正确时机——它运行得足够晚,文件生成已经发生,但又早于编译器运行。对于某些场景,使用 BeforeBuild 太早,并且可能无法与所有 SDK 功能可靠地协同工作。
<Target Name="IncludeGeneratedSourceFiles" BeforeTargets="CoreCompile;BeforeCompile">
<PropertyGroup>
<GeneratedCodeDir>$(IntermediateOutputPath)Generated\</GeneratedCodeDir>
<GeneratedFilePath>$(GeneratedCodeDir)MyGeneratedFile.cs</GeneratedFilePath>
</PropertyGroup>
<MakeDir Directories="$(GeneratedCodeDir)" />
<!-- 您生成 .cs 文件的逻辑放在这里 -->
<ItemGroup>
<Compile Include="$(GeneratedFilePath)" />
<FileWrites Include="$(GeneratedFilePath)" />
</ItemGroup>
</Target>
注意:同时指定 CoreCompile 和 BeforeCompile 可以确保目标在无论哪个目标先运行之前运行,从而无论构建中有何自定义项,都能提供稳健的顺序保证。
根据所生成文件的类型选择 BeforeTargets 的值:
BeforeTargets="BeforeBuild" — 对于添加到 None 或 Content 的非代码文件。对于复制到输出的场景,运行得足够早。BeforeTargets="CoreCompile;BeforeCompile" — 对于添加到 Compile 的生成源文件。确保文件在编译器运行之前被包含。BeforeTargets="AssignTargetPaths" — 在 None 和 Content 项(以及其他项)被转换为新项之前的“最后一站”。如果 BeforeBuild 太早,可作为备选方案。通配符的行为取决于 glob 何时发生:
| Glob 位置 | 捕获的文件 |
|---|---|
| 目标之外 | 仅捕获评估阶段(构建开始前)可见的文件 |
| 目标内部 | 捕获目标运行时可见的文件(如果时机正确,可以捕获生成的文件) |
这就是为什么解决方案将 <ItemGroup> 放在 <Target> 内部的原因——glob 在执行期间运行,此时生成的文件已经存在。
每周安装次数
47
代码仓库
GitHub 星标数
703
首次出现
2026年3月10日
安全审计
已安装于
kimi-cli45
gemini-cli45
amp45
cline45
github-copilot45
codex45
Files generated during the build are generally ignored by the build process. This leads to confusing results such as:
This happens because of how MSBuild's build phases work.
For code files generated during the build - we need to add those to Compile and FileWrites item groups within the target generating the file(s):
<ItemGroup>
<Compile Include="$(GeneratedFilePath)" />
<FileWrites Include="$(GeneratedFilePath)" />
</ItemGroup>
The target generating the file(s) should be hooked before CoreCompile and BeforeCompile targets - BeforeTargets="CoreCompile;BeforeCompile"
For detailed explanation, see How MSBuild Builds Projects.
MSBuild reads your project, imports everything, creates Properties, expands globs for Items outside of Targets , and sets up the build process.
MSBuild runs Targets & Tasks with the provided Properties & Items to perform the build.
Key Takeaway: Files generated during execution don't exist during evaluation, therefore they aren't found. This particularly affects files that are globbed by default, such as source files (.cs).
When files are generated during the build, manually add them into the build process. The approach depends on the type of file being generated.
$(IntermediateOutputPath) for Generated File LocationAlways use $(IntermediateOutputPath) as the base directory for generated files. Do not hardcode obj\ or construct the intermediary path manually (e.g., obj\$(Configuration)\$(TargetFramework)\). The intermediate output path can be redirected to a different location in some build configurations (e.g., shared output directories, CI environments). Using $(IntermediateOutputPath) ensures your target works correctly regardless of the actual path.
FileWritesEvery generated file should be added to the FileWrites item group. This ensures that MSBuild's Clean target properly removes your generated files. Without this, generated files will accumulate as stale artifacts across builds.
<ItemGroup>
<FileWrites Include="$(IntermediateOutputPath)my-generated-file.xyz" />
</ItemGroup>
For generated files that need to be copied to output (config files, data files, etc.), add them to Content or None items before BeforeBuild:
<Target Name="IncludeGeneratedFiles" BeforeTargets="BeforeBuild">
<!-- Your logic that generates files goes here -->
<ItemGroup>
<None Include="$(IntermediateOutputPath)my-generated-file.xyz" CopyToOutputDirectory="PreserveNewest"/>
<!-- Capture all files of a certain type with a glob -->
<None Include="$(IntermediateOutputPath)generated\*.xyz" CopyToOutputDirectory="PreserveNewest"/>
<!-- Register generated files for proper cleanup -->
<FileWrites Include="$(IntermediateOutputPath)my-generated-file.xyz" />
<FileWrites Include="$(IntermediateOutputPath)generated\*.xyz" />
</ItemGroup>
</Target>
If you're generating .cs files that need to be compiled, use BeforeTargets="CoreCompile;BeforeCompile". This is the correct timing for adding Compile items — it runs late enough that the file generation has occurred, but before the compiler runs. Using BeforeBuild is too early for some scenarios and may not work reliably with all SDK features.
<Target Name="IncludeGeneratedSourceFiles" BeforeTargets="CoreCompile;BeforeCompile">
<PropertyGroup>
<GeneratedCodeDir>$(IntermediateOutputPath)Generated\</GeneratedCodeDir>
<GeneratedFilePath>$(GeneratedCodeDir)MyGeneratedFile.cs</GeneratedFilePath>
</PropertyGroup>
<MakeDir Directories="$(GeneratedCodeDir)" />
<!-- Your logic that generates the .cs file goes here -->
<ItemGroup>
<Compile Include="$(GeneratedFilePath)" />
<FileWrites Include="$(GeneratedFilePath)" />
</ItemGroup>
</Target>
Note: Specifying both CoreCompile and BeforeCompile ensures the target runs before whichever target comes first, providing robust ordering regardless of customizations in the build.
Choose the BeforeTargets value based on the type of file being generated:
BeforeTargets="BeforeBuild" — For non-code files added to None or Content. Runs early enough for copy-to-output scenarios.BeforeTargets="CoreCompile;BeforeCompile" — For generated source files added to Compile. Ensures the file is included before the compiler runs.BeforeTargets="AssignTargetPaths" — The "final stop" before None and Content items (among others) are transformed into new items. Use as a fallback if BeforeBuild is too early.Globs behave according to when the glob took place:
| Glob Location | Files Captured |
|---|---|
| Outside of a target | Only files visible during Evaluation phase (before build starts) |
| Inside of a target | Files visible when the target runs (can capture generated files if timed correctly) |
This is why the solution places the <ItemGroup> inside a <Target> - the glob runs during execution when the generated files exist.
Weekly Installs
47
Repository
GitHub Stars
703
First Seen
Mar 10, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
kimi-cli45
gemini-cli45
amp45
cline45
github-copilot45
codex45
Azure Pipelines 本地验证与构建管理指南 - VS Code 开发工作流优化
544 周安装
Azure Event Hub Python 客户端库 - 实时数据流处理与云消息队列解决方案
1 周安装
Azure Event Grid .NET SDK - 事件驱动架构与云服务集成开发工具
1 周安装
PDForge API (PDF Noodle) - 从HTML/模板生成PDF和PNG的API服务
62 周安装
Azure Data Tables Python 技能包 - 高效管理 Azure 表存储数据
1 周安装
Azure Cosmos Java SDK 技能包 - 高效开发云原生数据库应用
1 周安装
Azure Cosmos DB Python SDK 技能包 - 云端NoSQL数据库开发与集成指南
1 周安装