Visualstudio-docs: Overriding predefined targets does not work with new <Project Sdk="...">

Created on 11 Sep 2019  Â·  4Comments  Â·  Source: MicrosoftDocs/visualstudio-docs

This does not work (no messages printed):

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <Target Name="BeforeBuild">
    <Message Importance="High"
             Text="BeforeBuild BeforeBuild BeforeBuild !!!" />
  </Target>

  <Target Name="AfterBuild">
    <Message Importance="High"
             Text="AfterBuild AfterBuild AfterBuild !!!" />
  </Target>
</Project>

The reason for this is that MSBuild implicitly adds the Sdk.targets imports at the end of the file, so always AFTER our own definition. This means our own definition gets overridden with empty commands.

We can make it work if the Sdk imports are specified manually as shown in https://docs.microsoft.com/en-us/visualstudio/msbuild/how-to-use-project-sdk?view=vs-2019:

<Project >
  <!-- Implicit top import -->
  <Import Project="Sdk.props"
          Sdk="Microsoft.NET.Sdk" />

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <!-- Implicit bottom import -->
  <Import Project="Sdk.targets"
          Sdk="Microsoft.NET.Sdk" />

  <Target Name="BeforeBuild">
    <Message Importance="High"
             Text="BeforeBuild BeforeBuild BeforeBuild !!!" />
  </Target>

  <Target Name="AfterBuild">
    <Message Importance="High"
             Text="AfterBuild AfterBuild AfterBuild !!!" />
  </Target>
</Project>

The documentation should include a warning that the standard .NET core template must be modified in order for user-defined target overrides to work.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Pri2 doc-bug msbuiltech visual-studio-windowprod

All 4 comments

@Science-and-Motion-Sports -- thank you for your feedback. We are looking into updating the documentation.

Rather than trying to make this topic work for SDK-style projects, it might be better to add a note similar to the suggestion above:

For projects using the SDK attribute, specify your imports manually as described in https://docs.microsoft.com/en-us/visualstudio/msbuild/how-to-use-project-sdk?view=vs-2019.

@rainersigwald for comment

Maybe something like

Note: SDK-style projects have an implicit import of targets _after the last line of the project file_. This means that you cannot override default targets unless you specify your imports manually as described in https://docs.microsoft.com/en-us/visualstudio/msbuild/how-to-use-project-sdk?view=vs-2019.

?

The reason it doesn't work is that common.targets gets imported after the project targets, so the (empty) common.targets ones actually override the project-defined ones.

In general, I'd like to guide folks to create new targets rather than override the stub extension-point targets. But that's a bigger change . . .

Sounds good. I'll add the note. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jnpwly picture jnpwly  Â·  3Comments

CeciAc picture CeciAc  Â·  3Comments

realbart picture realbart  Â·  3Comments

Logerfo picture Logerfo  Â·  3Comments

nitinjs picture nitinjs  Â·  3Comments