PreBuild event/target runs twice - during Build and Publish
See original GitHub issueVisual Studio Version: 16.3.1
Summary / Steps to Reproduce / Behavior: I have a .NET Core 3.0 WinForms application and I have added a prebuild event to my project. It gets added to the csproj like this:
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="$(ProjectDir)PreBuildTasks.exe" />
</Target>
It basically launches my prebuild exe, which does a few things, like increment the version in AssemblyInfo. This has always worked fine until I started experimenting with .NET Core and publishing.
For context, I am going to add this right before the Exec Command
:
<Message Importance="high" Text="This is a message" />
When I publish the project (right click project, Publish), this is what I see:
It’s clear the prebuild task is running twice. I spent a while researching a way to resolve this problem. The closest solution I found was this. I added Condition="'$(DeployOnBuild)' != 'true'"
as directed, but unfortunately it did not do anything. The other solution in that thread says the issue is fixed, so what is happening here?
This behavior may be by design, but I decided to open this issue in case it is not, and because I’d like some guidance on the correct way to solve this problem if I am going at this the wrong way. I only want my prebuild task to run on Build, not Publish.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:18 (6 by maintainers)
Top GitHub Comments
Also curious to hear if there are any updates on this. Experienced a similar issue with “dotnet publish” running pre-build tasks twice when executed from a directory containing the .sln file, but not when running it from sub-folders containing their own .csproj files.
I found this to be useful in my .NET 5 project:
<Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition="'$(DeployOnBuild)' != 'true'">
Note the use of $(DeployOnBuild). This value is empty on build, and true on publish. Now my build events only run during build!Edit: I just realized the OP mentions this but didn’t work for them. Maybe it’s a framework dependent thing?