简体   繁体   中英

When does msbuild determine list of files to build, using new 'implicit' csproj format?

Visual Studio 2019, with the new 'implicit' C# project format where you don't include files in the csproj.

There is a pre-build job which generates two.cs files, but apparently when msbuild invokes csc (after the pre-build event), it doesn't include these two files in the list of files to compile. I've even tried introducing a 30 second delay. So it looks like msbuild fixes the file list before running the pre-build event.

Obviously, after the first time this is not a problem, but it's not great on a build server or new dev box.

Is there a clean solution to this?

You will need to add these files to the Compile items manually if it is not already in there:

<Target Name="GenerateStuff" BeforeTargets="BeforeBuild">
  <Exec Command="generate foo.cs" />
  <ItemGroup>
    <Compile Include="foo.cs" Exclude="@(Compile)" />
  </ItemGroup>
</Target>

This notation also supports wildcats ( Include="gen*.cs" )

Found a solution:

Add InitialTargets attribute to project element:

<Project Sdk="Microsoft.NET.Sdk" InitialTargets="Generation">

Change PreBuild to this

<Target Name="Generation">
  <Exec Command="whatever" />
</Target>

(Note that if you have Visual Studio open, this works almost too well, as the files will reappear automatically even without building!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM