msbuild: Bug when using MSBuild with custom target since NET5.0 update

Issue Description

I’m hitting a bug when consuming a custom project SDK target (http://nuget.org/packages/h5.target):

Invalid static method invocation syntax: "[MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)', 2)". Method '[MSBuild]::GetTargetFrameworkVersion' not found. Static method invocation should be of the form: $([FullTypeName]::Method()), e.g. $([System.IO.Path]::Combine(`a`, `b`)). Check that all parameters are defined, are of the correct type, and are specified in the right order.  C:\Program Files\dotnet\sdk\5.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets

The code in the targets file that seems to be triggering this issue is the following:

  <PropertyGroup Condition="'$(TargetFramework)' != '' and ('$(TargetFrameworkIdentifier)' == '' or '$(TargetFrameworkVersion)' == '')">

    <TargetFrameworkIdentifier>$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)'))</TargetFrameworkIdentifier>
    <TargetFrameworkVersion>v$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)', 2))</TargetFrameworkVersion>
  </PropertyGroup>

If I manually force the properties to the expected (or dummy) values, so that we avoid the GetTargetFrameworkIdentifier, GetTargetFrameworkVersion, GetTargetPlatformIdentifier and GetTargetPlatformVersion calls above:

    <TargetFrameworkIdentifier>.NETStandard</TargetFrameworkIdentifier>
    <TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
    <TargetPlatformIdentifier>h5</TargetPlatformIdentifier>
    <TargetPlatformVersion>2.0</TargetPlatformVersion>

Then the compilation using MSBuild works fine.

Any ideas?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Tried to repro again and not seeing any issues:

dotnet msbuild -version

Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET       
Copyright (C) Microsoft Corporation. All rights reserved.

16.8.0.52002

dotnet --version

5.0.100

@theolivenbaum could you try building a tiny project (via dotnet build on your 5.0.100 sdk) that uses [MSBuild]::GetTargetFrameworkIdentifier? This should eliminate whether it could be MSBuildLocator in your scenario.

The project I’m building to try and repro this is simply:

<Project>
<PropertyGroup>
    <TargetFrameworkIdentifier>.NETStandard</TargetFrameworkIdentifier>
    <TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
    <TargetPlatformIdentifier>h5</TargetPlatformIdentifier>
    <TargetPlatformVersion>2.0</TargetPlatformVersion>
</PropertyGroup>
  <PropertyGroup>
    <TargetFrameworkIdentifier>$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)'))</TargetFrameworkIdentifier>
    <TargetFrameworkVersion>v$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)', 2))</TargetFrameworkVersion>
  </PropertyGroup>

  <Target Name="HelloWorld">
    <Message Text="Hello World!" Importance="High"/>
  </Target>
</Project>

Note that this does fail with your error if I global.json myself to do 3.1.404, which is expected as that version of MSBuild would not have the functions being called.

You mentioned MSBuildLocator is finding the correct MSBuild, can you verify this? I ask because this issue (should) only come up if you were using an older MSBuild wth a newer sdk somehow.

What may be the most helpful in this scenario:

  1. Providing a minimal repro of your specific scenario.
  2. Providing binlogs of your build.