본문으로 건너뛰기
Skillpedia
배포 및 클라우드 (DevOps)5.8k · #14601

including-generated-files

하는 일

이 스킬은 MSBuild가 빌드 실행 전에 파일 목록을 정하기 때문에, 빌드 단계가 만든 파일이 컴파일되거나 복사되지 않는 문제를 고칩니다. 빌드가 만든 코드나 콘텐츠가 사라지는 개발자를 위한 스킬입니다.

사용하는 경우

  • ✓빌드 단계가 만든 소스 파일이 컴파일되지 않을 때.
  • ✓빌드가 만든 파일이 출력 폴더에 없을 때.
  • ✓빌드 중에 만들어진 파일을 프로젝트가 인식하지 못할 때.

스킬 링크

https://github.com/dotnet/skills/blob/main/plugins/dotnet-msbuild/skills/including-generated-files/SKILL.md

복사해서 AI(Claude, ChatGPT, Cursor)에 붙여넣고 지침을 따르도록 요청하세요.

사용 방법

가장 쉬운 방법입니다. 메시지를 복사해 AI에 붙여넣으세요. AI가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

Use this skill to help me: https://github.com/dotnet/skills/blob/main/plugins/dotnet-msbuild/skills/including-generated-files/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
AI가 이런 스킬을 알아서 찾아 쓰길 원하시나요?무료 Skillpedia MCP를 설치하세요. 한 줄이면 작업마다 스킬 15,000+개 중에서 검색합니다.1분 만에 설치 →

AI에게 요청하는 예시

“제 빌드가 빌드 중에 C# 파일을 만드는데 컴파일러가 보지 못합니다. 그 파일이 포함되도록 대상을 고쳐 주세요.”

매주 새 스킬 5개. 읽는 데 1분.

작동 방식

에이전트가 파일을 만드는 대상 안에서 컴파일 목록과 추적되는 출력 목록에 생성된 파일을 추가하고, 그 대상이 컴파일 전에 실행되도록 설정합니다.

기술 내용 (원본 SKILL.md)

작성자의 영어 원문을 GitHub에서 그대로 가져왔습니다.

Including Generated Files Into Your Build

Overview

Files generated during the build are generally ignored by the build process. This leads to confusing results such as:

  • Generated files not being included in the output directory
  • Generated source files not being compiled
  • Globs not capturing files created during the build

This happens because of how MSBuild's build phases work.

Quick Takeaway

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"

Why Generated Files Are Ignored

For detailed explanation, see How MSBuild Builds Projects.

Evaluation Phase

MSBuild reads your project, imports everything, creates Properties, expands globs for Items outside of Targets, and sets up the build process.

Execution Phase

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).

Solution: Manually Add Generated Files

When files are generated during the build, manually add them into the build process. The approach depends on the type of file being generated.

Use $(IntermediateOutputPath) for Generated File Location

Always 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.

Always Add Generated Files to FileWrites

Every 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>

Basic Pattern (Non-Code Files)

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>

For Generated Source Files (Code That Needs Compilation)

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

자주 묻는 질문

왜 생성된 파일이 무시되나요?

MSBuild가 평가 단계에서 파일 패턴을 대상이 파일을 만들기 전에 읽기 때문에, 새 파일이 목록에 없습니다.

파일은 어디에 추가하나요?

그 파일을 만드는 대상 안에 추가하므로, 목록에 제때 들어갑니다.

클린 빌드도 다루나요?

네. 파일을 추적해 두면 정리 작업이 그 파일을 지울 수 있습니다.

상세 정보

작성자의 원본 설명

Own MSBuild generated-artifact integration. USE FOR: a target that creates or should create source, Content, None, or another physical file but the artifact is missing from compilation or output; target timing; evaluation-time glob misses; $(IntermediateOutputPath) placement; and FileWrites clean tracking. The prompt may describe the generated artifact without naming the producing task. DO NOT USE when the primary defect is general Include/Remove/Update semantics, item metadata or batching, duplicate/overlapping declarations, or a generated-item identity relationship; use item-management. Excl

repo: dotnet/skills

인기도

GitHub 스타
5.8k
순위
#14601
라이선스
MIT
업데이트
2026. 11. 2.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬