dotnet / msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.
https://docs.microsoft.com/visualstudio/msbuild/msbuild
MIT License
5.2k stars 1.35k forks source link

VersionGreaterThanOrEquals error on dotnet build #10369

Open phil-warner opened 2 months ago

phil-warner commented 2 months ago

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

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
marcpopMSFT commented 1 month ago

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

KalleOlaviNiemitalo commented 1 month ago

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.

phil-warner commented 1 month ago

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'?

rainersigwald commented 1 month ago

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?

phil-warner commented 1 month ago

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.

rainersigwald commented 1 month ago

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

phil-warner commented 1 month ago

Which aspects look wrong to you @rainersigwald ?

KalleOlaviNiemitalo commented 1 month ago

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

rainersigwald commented 3 weeks ago

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

phil-warner commented 3 weeks ago

@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