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.

Move definitions of BeforeBuild and AfterBuild targets to .props instead of .targets files

See original GitHub issue

The BeforeBuild and AfterBuild targets are currently defined in Microsoft.Common.CurrentVersion.targets. I presume that the intention was for you to override them in your project files after the .targets import near the bottom of the file.

However, if you’re using the new Sdk attribute on the Project element, it’s not possible to put a target definition after the default .targets import. This can lead to targets that people put in their project files unexpectedly not running, with no indication why unless you examine the log file and see the message that the target has been overridden (for example, https://github.com/dotnet/sdk/issues/841).

It would be better to define the empty BeforeBuild and AfterBuild targets in a .props file so that if they occur in the body of a project the ones from the project take precedence.

@AndyGerlicher @rainersigwald @cdmihai What do you think about the compat implications of this and when we could make such a change? If we changed it for all situations, then targets defined in the “wrong” place in project files would start running where they hadn’t previously. If we are not OK with that, we could change to conditionally defining these targets where they currently are, and then define them in a .props file of the .NET SDK along with a property telling the default MSBuild targets not to define them.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:8
  • Comments:43 (27 by maintainers)

github_iconTop GitHub Comments

11reactions
cdmihaicommented, Feb 9, 2017

A workaround is to use SDK imports:

<Project>
  <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
...
  <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />

  <Target Name="BeforeBuild">
...
  </Target>
</Project>
8reactions
terrajobstcommented, Apr 26, 2017

What is the recommended replacement?

<Target Name="MyCode" AfterTargets="AfterBuild" />

– or –

<Target Name="MyCode" AfterTargets="Build" />

– or –

<PropertyGroup>
  <BuildDependsOn>$(BuildDependsOn);MyCode</BuildDependsOn>
</PropertGroup>

<Target Name="MyCode" />

They aren’t equivalent as they have different behavior (and there are probably more ways to do that). We should agree what the right replacement is and recommend it in our docs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

.net - How can I use BeforeBuild and AfterBuild targets with ...
IS it possible to move TargetFramework property to external props file? When I tried doing that, Visual Studio gives me a "one-way upgrade" ......
Read more >
Extend the build process - MSBuild
The Visual Studio build process is defined by a series of MSBuild .targets files that are imported into your project file.
Read more >
MSBuild Basics - Jon Douglas
Project File (.csproj is the MSBuild file) ... All content will be placed inside of the <Project> tag. This includes, properties, items, targets, ......
Read more >
MSBuild Property Functions (2)
In part five we will discuss how the target framework moniker represented in the project file is used to generate a list of...
Read more >
VS2017 + MSBuild Helpful "tricks" : r/csharp
This file is a special file in that MSBuild knows to look for it (along with directory.build.targets). If this file resides next to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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