I have a Visual Studio 2019 solution and the projects it contains have been converted from .NET Framework 4.8 to NET Core 8. In VS2019, building the projects and the solution worked without any problems.
Now I have the Visual Studio 2022 NET Core 8 solution with several NET Core projects and all the following steps have already been carried out in VS2019, but everything always worked there. One of these projects contains two partial classes A and B (these are always present and were written manually). During the build process, two further files are created ( which contains also partial classes A and B ) in the PreBuildEvent via an external generator application (called via Exec Command). The generated files are located in the same directory as the existing classes. The automatically generated files contains partial classes A and B with methods that are used in manuell created partial classes A and B. The files are also automatically recognized by Visual Studio and displayed in the Solution Explorer.
When the solution or the corresponding project is built for the first time, the files are generated correctly. However, errors occur during compilation (error CS0103), which indicate that some of the functions used are not available. These functions are implemented in the automatically generated partial class.
If I run the build process again directly, everything works.
If I delete the generated files manually, the problem can be reproduced.
If I use <EnableDefaultCompileItems>false</EnableDefaultCompileItems> and add the following to <Target BeforeTargets="PreBuildEvent">
<ItemGroup>
<Compile Include="*.cs" />
</ItemGroup>
then the files are added and the build works. Once. If I delete the files manually, the target step is no longer executed.
I have tried a few things and some things do work, but only for the project. When I build the solution, I always get this error the first time. As soon as the files are available the second time it works.
Of the many events that happen in the build pipeline, it looks like the
PreBuildEventruns a little too late for your scenario. The files need to be generated in an earlier build step, otherwise the generated files themselves are not included in the compile process. To achieve that you can generate them using theBeforeBuildevent instead.Note: This was inspired by this Answer to "What is the difference between a PreBuildEvent, BeforeBuild target and BeforeCompile target in MSBuild?" which offers a lot more detail, and by the first comment below it.