Benjol
Benjol

Reputation: 66617

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?

Upvotes: 1

Views: 978

Answers (2)

Martin Ullrich
Martin Ullrich

Reputation: 100761

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

Upvotes: 2

Benjol
Benjol

Reputation: 66617

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

Upvotes: 0

Related Questions