Sdk: BeforeBuild/AfterBuild targets not honored.

Created on 25 Feb 2017  路  5Comments  路  Source: dotnet/sdk

Steps to reproduce

Requires a project with the following

...
  <Target Name="BeforeBuild">
    <Message Text="Some message." />
  </Target>
...

Then

dotnet restore
dotnet build

Expected behavior

I expect the BeforeBuild target to be executed. Even adding BeforeTargets="Build" doesn't work. Similarly for AfterBuild/DependsOnTargets="Build".

Actual behavior

The target isn't run.

Environment data

dotnet --info output:

.NET Command Line Tools (1.0.0-rc4-004771)

Product Information:
 Version:            1.0.0-rc4-004771
 Commit SHA-1 hash:  4228198f0e

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  14.04
 OS Platform: Linux
 RID:         ubuntu.14.04-x64
 Base Path:   /usr/share/dotnet/sdk/1.0.0-rc4-004771

Most helpful comment

Try this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard1.4</TargetFramework>
  </PropertyGroup>

  <Target Name="Foo"
          AfterTargets="Build">
    <Message Text="Bar" Importance="High" />
  </Target>

</Project>

The .NET Core SDK is not, AFAIK, bringing forward the BeforeBuild/AfterBuild extensibility model since the same behavior can be provided by MSBuild's BeforeTargets/AfterTargets attributes, as exemplified above.

All 5 comments

Try this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard1.4</TargetFramework>
  </PropertyGroup>

  <Target Name="Foo"
          AfterTargets="Build">
    <Message Text="Bar" Importance="High" />
  </Target>

</Project>

The .NET Core SDK is not, AFAIK, bringing forward the BeforeBuild/AfterBuild extensibility model since the same behavior can be provided by MSBuild's BeforeTargets/AfterTargets attributes, as exemplified above.

Re-opening because the behavior here is still odd.

If I use <Target Name="Foo" BeforeTargets="Build"> like you suggest, it works as expected, but if I name the target BeforeBuild, the it's _not_ executed, so it appears that there is some (incomplete?) switching on the target name.

@brettfo can you please move this issue to the microsoft/msbuild repo? particularly this last piece about this not working depending on the target name.

Issue appears to already exist at Microsoft/MSBuild#1680.

This one is also interesting: https://github.com/microsoft/msbuild/issues/1680#issuecomment-468710590

While <BeforeBuild> and <AfterBuild> do not work, oddly, these work fine:

<Target Name="CustomBeforeBuild" BeforeTargets="BeforeBuild" />
<Target Name="CustomAfterBuild" AfterTargets="AfterBuild" />
Was this page helpful?
0 / 5 - 0 ratings