msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

VersionGreaterThanOrEquals error on dotnet build

Open phil-warner opened this issue 1 year ago • 10 comments

Describe the bug

When running dotnet build I get the following error:

/usr/local/share/dotnet/sdk/8.0.302/Microsoft.NETFramework.CurrentVersion.props(117,25): error MSB4184: The expression "[MSBuild]::VersionGreaterThanOrEquals(net7.0, 4.0)" cannot be evaluated.

To Reproduce

  • Running dotnet-sdk on MacOS Ventura.
  • SDK version is 8.0.302.
  • Solution generated using openapi generator.
  • OpenAPI version 3.0.1

csproj file (truncated for brevity) as follows:

<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{C6A8B93A-3AC3-4E5E-8584-B0B4D328ED47}</ProjectGuid>
    <PackageId>TheSMSWorks.API</PackageId>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Org.OpenAPITools</RootNamespace>
    <AssemblyName>Org.OpenAPITools</AssemblyName>
    <TargetFramework>net7.0</TargetFramework>
    <FileAlignment>512</FileAlignment>
    <Authors>The SMS Works Ltd</Authors>
    <Company>The SMS Works Ltd</Company>
    <Description>C# .net 6 library for the SMS Works API</Description>
    <DocumentationFile>bin\$(Configuration)\$(TargetFramework)\Org.OpenAPITools.xml</DocumentationFile>
    <RepositoryUrl>https://github.com/TheSMSWorks/smsw-csharp-netcore6.git</RepositoryUrl>
    <RepositoryType>git</RepositoryType>
    <PackageReleaseNotes>Minor update</PackageReleaseNotes>
    <LangVersion>8.0</LangVersion>
    <Nullable>enable</Nullable>
    <Version>1.11.0</Version>
  </PropertyGroup>
 ... etc

Further technical details

The output of dotnet --info is as follows:

.NET SDK:
 Version:           8.0.302
 Commit:            ef14e02af8
 Workload version:  8.0.300-manifests.00e64df5
 MSBuild version:   17.10.4+10fbfbf2e

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  13.6
 OS Platform: Darwin
 RID:         osx-x64
 Base Path:   /usr/local/share/dotnet/sdk/8.0.302/

.NET workloads installed:
There are no installed workloads to display.

Host:
  Version:      8.0.6
  Architecture: x64
  Commit:       3b8b000a0e

.NET SDKs installed:
  2.2.106 [/usr/local/share/dotnet/sdk]
  3.1.402 [/usr/local/share/dotnet/sdk]
  3.1.426 [/usr/local/share/dotnet/sdk]
  6.0.414 [/usr/local/share/dotnet/sdk]
  7.0.308 [/usr/local/share/dotnet/sdk]
  8.0.302 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.2.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.2.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.8 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.32 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.22 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.11 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.6 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.22 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.8 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.32 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.22 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.11 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.6 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  Not found

phil-warner avatar Jun 24 '24 12:06 phil-warner

That failing props line is from msbuild so transferring: https://github.com/dotnet/msbuild/blob/vs17.10/src/Tasks/Microsoft.NETFramework.CurrentVersion.props

marcpopMSFT avatar Jul 12 '24 22:07 marcpopMSFT

Microsoft.NETFramework.CurrentVersion.props compares $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '4.0')).

Microsoft.NET.TargetFrameworkInference.targets would set <TargetFrameworkVersion>v$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)', 2))</TargetFrameworkVersion>, which should evaluate to v7.0 when $(TargetFramework) is net7.0.

Either something else sets the TargetFrameworkVersion property incorrectly, or the GetTargetFrameworkVersion intrinsic function returns the wrong value.

To check whether GetTargetFrameworkVersion works correctly in your environment, can you try to build this project:

<Project>
  <Target Name="Evaluate">
    <!-- Made this an error so that Terminal Logger won't hide it.  -->
    <Error Text="Result: $([MSBuild]::GetTargetFrameworkVersion('net7.0', 2))" />
  </Target>
</Project>

On Windows, I see output like:

MSBuild version 17.10.4+10fbfbf2e for .NET
C:\TEMP\target.proj(4,5): error : Result: 7.0

If you get "Result: net7.0" instead, then it seems a NuGet problem, as GetTargetFrameworkVersion is implemented there.

If you get the correct "Result: 7.0", then perhaps something in your project or in a referenced NuGet package sets TargetFrameworkVersion wrong. https://github.com/dotnet/msbuild/issues/2711#issuecomment-1176263310 may help locate the cause.

KalleOlaviNiemitalo avatar Jul 13 '24 11:07 KalleOlaviNiemitalo

Hi. Thank you. I get the following from terminal on MacOS:

Build FAILED.

/Users/***/Documents/GitHub/test.csproj(4,5): error : Result: 7.0
    0 Warning(s)
    1 Error(s

How do I use 'export MsBuildLogPropertyTracking=3'?

phil-warner avatar Jul 15 '24 13:07 phil-warner

That project file looks like a weird hybrid between a modern .NET SDK project (which you'd need to target net7.0) and an older .NET Framework-targeting project. OpenAPI generator 3.0.1 is very old (.NET Core 2.1 timeframe)--are you sure it supports this type of output?

rainersigwald avatar Jul 15 '24 13:07 rainersigwald

Hi @rainersigwald. The generator version being used is 5.4.0, which is downloaded by the generator when we set the targetFramework parameter. The version of the OpenAPI spec is 3.0.1 - sorry for any confusion.

phil-warner avatar Jul 15 '24 16:07 phil-warner

@phil-warner Ok, but that project is still pretty nonsensical. I would recommend pursuing that aspect of things.

rainersigwald avatar Jul 15 '24 16:07 rainersigwald

Which aspects look wrong to you @rainersigwald ?

phil-warner avatar Jul 17 '24 08:07 phil-warner

The project has <TargetFramework>net7.0</TargetFramework> but not <Project Sdk="Microsoft.NET.Sdk">.

KalleOlaviNiemitalo avatar Jul 17 '24 08:07 KalleOlaviNiemitalo

@phil-warner any luck looking at the generator side? And just to confirm, you were using -g csharp-netcore?

rainersigwald avatar Jul 30 '24 13:07 rainersigwald

@phil-warner any luck looking at the generator side? And just to confirm, you were using -g csharp-netcore?

No luck, seemingly. This is my command to generate the client (I can't seem to find a reference to 'csharp-netcore'):

openapi-generator-cli generate -i https://api.thesmsworks.co.uk/docs/openapi.json -g csharp --additional-properties=targetFramework=net6.0 --additional-properties=optionalAssemblyInfo -o smsw-csharp-netcore6

phil-warner avatar Aug 05 '24 08:08 phil-warner

Hi all. We've managed to resolve the issue by re-installing the openapi generator and using it to generate a fresh solution. I do appreciate the input from everyone very much - thank you.

phil-warner avatar Sep 13 '24 17:09 phil-warner