Azure / azure-functions-dotnet-worker

Azure Functions out-of-process .NET language worker
MIT License
407 stars 167 forks source link

AzureFunctionsVersion is set to an incompatible version #2456

Open Camios opened 2 months ago

Camios commented 2 months ago

What version of .NET does your existing project use?

.NET 6

What version of .NET are you attempting to target?

.NET 8

Description

I'm trying to upgrade my second Function App project from .net 6 (in-process) to .net 8 (isolated). The first one succeeded, but the second no matter what I've tried has an error "AzureFunctionsVersion is set to an incompatible version".

The error is coming from C:\Users\<user>\.nuget\packages\microsoft.azure.functions.worker.sdk\1.17.2\build\Microsoft.Azure.Functions.Worker.Sdk.targets

This section:

    <Target Name="_FunctionsPreBuild" BeforeTargets="BeforeBuild">
        <Message Condition="'$(_AzureFunctionsNotSet)' == 'true'" Importance="high" Text="AzureFunctionsVersion not configured in the project. Setting AzureFunctionsVersion to v3"/>
        <Error Condition="$(AzureFunctionsVersion.StartsWith('v1',StringComparison.OrdinalIgnoreCase))" Text="AzureFunctionsVersion is set to an incompatible version, Please set it to v4"/>
        <Error Condition="$(AzureFunctionsVersion.StartsWith('v2',StringComparison.OrdinalIgnoreCase))" Text="AzureFunctionsVersion is set to an incompatible version, Please set it to v3"/>
        <Error Condition="!$(AzureFunctionsVersion.StartsWith('v3',StringComparison.OrdinalIgnoreCase)) And !$(AzureFunctionsVersion.StartsWith('v4',StringComparison.OrdinalIgnoreCase))" Text="AzureFunctionsVersion is set to an incompatible version"/>
        <Error Condition="'$(_ToolingSuffix)' == ''" Text="Invalid combination of TargetFramework and AzureFunctionsVersion is set."/>
        <Error Condition="'$(_IsFunctionsSdkBuild)' == 'true'" Text="Microsoft.NET.Sdk.Functions package is meant to be used with in-proc function apps. Please remove the reference to this package in isolated function apps."/>
    </Target>

As an experiment I put <Error Condition="true" Text="$(AzureFunctionsVersion)"/> at the top of the section and my first, working project is spitting out "v4", but the second, failing project is spitting out "v0"!

The failing project csproj's contents are attached but it has always had a value of v4 - it wasn't changed by the upgrade assistant.

I tried copying the contents of csproj from of the working project and it still fails.

I tried unloading the first project; I tried cleaning the solution; deleting the .vs file for the sln; and deleting the bin and obj folders but that didn't help.

I did see some strange warnings:

Severity    Code    Description Project File    Line    Suppression State   Details
Warning (active)    MSB4011 "C:\Program Files\dotnet\sdk\8.0.300-preview.24203.14\Sdks\Microsoft.NET.Sdk.Publish\Sdk\Sdk.props" cannot be imported again. It was already imported at "C:\Users\<user>\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Publish.props (13,3)". This is most likely a build authoring error. This subsequent import will be ignored. [(path to).csproj]    ProjectName C:\Users\<user>\.nuget\packages\microsoft.azure.functions.worker.sdk\1.17.2\build\Microsoft.Azure.Functions.Worker.Sdk.Publish.props    13      
Warning (active)    MSB4011 "C:\Program Files\dotnet\sdk\8.0.300-preview.24203.14\Sdks\Microsoft.NET.Sdk.Publish\Sdk\Sdk.targets" cannot be imported again. It was already imported at "C:\Users\<user>\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.Publish.targets (14,3)". This is most likely a build authoring error. This subsequent import will be ignored. [(path to).csproj]    ProjectName     C:\Users\<user>\.nuget\packages\microsoft.azure.functions.worker.sdk\1.17.2\build\Microsoft.Azure.Functions.Worker.Sdk.Publish.targets  15      
Warning (active)    MSB4011 "C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VisualStudio\Managed.Functions\Microsoft.Azure.Functions.Designtime.targets" cannot be imported again. It was already imported at "C:\Users\<user>\.nuget\packages\microsoft.net.sdk.functions\4.4.0\build\Microsoft.NET.Sdk.Functions.targets (65,3)". This is most likely a build authoring error. This subsequent import will be ignored. [(path to).csproj]   ProjectName     C:\Users\<user>\.nuget\packages\microsoft.azure.functions.worker.sdk\1.17.2\build\Microsoft.Azure.Functions.Worker.Sdk.targets  56      

Project configuration and dependencies

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <UserSecretsId>(snipped)</UserSecretsId>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.4.0" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.5.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.1" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
  </ItemGroup>
</Project>

Link to a repository that reproduces the issue

No response

Camios commented 2 months ago

If I comment-out the AzureFunctionsVersion or change it to "v3" , then I get a different error "Invalid combination of TargetFramework and AzureFunctionsVersion is set." from C:\Users\(user)\.nuget\packages\microsoft.azure.functions.worker.sdk\1.17.2\build\Microsoft.Azure.Functions.Worker.Sdk.targets 64

Camios commented 2 months ago

If I revert all the changes in the solution made by the upgrade assistant (and fixes to made to try to get it compile), then it rebuilds successfully

Camios commented 2 months ago

Turns out the problem might have been that <ImplicitUsings> should technically be enable but the Microsoft.Azure.Functions.Worker.Sdk is expecting enabled. Which isn't the correct value for normal projects, but if it isn't set to enabled for Function Apps, then it breaks things. Seems like a bug

kshyju commented 2 months ago

You should not be having a package reference to Microsoft.NET.Sdk.Functions package in an isolated function. That package is for in-proc model apps.

I suggest you to create a brand new .NET Isolated 8 function app using tooling (such as Visual studio) and use that for reference.

Camios commented 2 months ago

You should not be having a package reference to Microsoft.NET.Sdk.Functions package in an isolated function. That package is for in-proc model apps.

I suggest you to create a brand new .NET Isolated 8 function app using tooling (such as Visual studio) and use that for reference.

This category I picked when creating the issue was to raise problems with the .NET Upgrade Assistant. In this case there's possibly two problems.

Firstly, there's the addition of ImplicitUsing incorrectly having a value of enabled when it should be enable (ref but perhaps that is a bug in the Function Apps SDK where it incorrectly expects enabled and the Upgrade Assistant is having to play along?

Secondly, leaving behind incorrect namespace(s) - Microsoft.NET.Sdk.Functions

It seems the combination of these two things caused the error. Both of these are caused by the .NET Upgrade Assistant for Azure Function App projects. Please fix this part of the upgrade assistant.

Camios commented 2 months ago

Speaking of ImplicitUsing, is there a list of global namespaces for the Azure Functions SDK?

kshyju commented 2 months ago

@aishwaryabh FYI

MathiasSmedemark commented 1 month ago

The V4 should be with a capital letter, otherwise it will throw the error :)

Camios commented 1 month ago

The V4 should be with a capital letter, otherwise it will throw the error :)

@MathiasSmedemark The check ignores case !$(AzureFunctionsVersion.StartsWith('v4',StringComparison.OrdinalIgnoreCase))

jeffw16 commented 2 days ago

I suggest you to create a brand new .NET Isolated 8 function app using tooling (such as Visual studio) and use that for reference.

I don't think people need to go as far as to make a new function app, even for reference. That's a huge waste of time. All that was needed was to remove Microsoft.NET.Sdk.Functions and ensure the appropriate references in the Microsoft.Azure.Functions.Worker namespace are present.

Camios commented 1 day ago

This ticket isn't just about what was wrong and to get a workaround. What was more important to me is that the Microsoft project upgrader extension was the thing that got it wrong and it needs to be fixed.