question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PreBuild event/target runs twice - during Build and Publish

See original GitHub issue

Visual 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: 2019-10-01_1-25-35

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:open
  • Created 4 years ago
  • Reactions:6
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
daalcantcommented, Mar 13, 2020

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.

0reactions
zrsio4commented, Aug 4, 2022

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is the pre-build step running twice?
Ever time my project is built, the pre-build event command-line is being executed twice by Visual Studio.
Read more >
AfterBuild task runs twice during "Archive for publishing"
I need to run a task that performs obfuscation on the output assembly before it gets "archived". I've followed the indication in the...
Read more >
Exam AZ-204 topic 2 question 27 discussion
A. Add the path to the static content generation tool to WEBSITE_RUN_FROM_PACKAGE setting in the host.json file. · B. Add a PreBuild target...
Read more >
Visual Studio add pre-build event that always runs (C# ...
I am working around this by always using "rebuild" instead of "build" on my project, but this is tedious and slow. I need...
Read more >
Event Targets Are Always Retargeted
Event targets are always retargeted regardless of the timing that an event handler was added. Where: This change applies to Lightning Out and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found