diff --git a/sdk/Sdk.Generators/FunctionMetadataProviderGenerator/FunctionMetadataProviderGenerator.Emitter.cs b/sdk/Sdk.Generators/FunctionMetadataProviderGenerator/FunctionMetadataProviderGenerator.Emitter.cs index a715eeacf..0fa52199e 100644 --- a/sdk/Sdk.Generators/FunctionMetadataProviderGenerator/FunctionMetadataProviderGenerator.Emitter.cs +++ b/sdk/Sdk.Generators/FunctionMetadataProviderGenerator/FunctionMetadataProviderGenerator.Emitter.cs @@ -49,7 +49,7 @@ public Task> GetFunctionMetadataAsync(string d { var metadataList = new List(); {{functionMetadataInfo}} - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } diff --git a/sdk/release_notes.md b/sdk/release_notes.md index ec5ffdba2..4988d71c7 100644 --- a/sdk/release_notes.md +++ b/sdk/release_notes.md @@ -12,4 +12,6 @@ - Changed `FunctionExecutorGenerator` to avoid generation of long `if`/`else` chains for apps with a large number of functions. +- Use full namespace for `Task.FromResult` in function metadata provider generator to avoid namespace conflict (#2681) + - diff --git a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/AmbiguousNamespaceTests.cs b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/AmbiguousNamespaceTests.cs new file mode 100644 index 000000000..207ae5e06 --- /dev/null +++ b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/AmbiguousNamespaceTests.cs @@ -0,0 +1,146 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.Azure.Functions.Worker.Sdk.Generators; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Xunit; + +namespace Microsoft.Azure.Functions.SdkGeneratorTests +{ + public partial class FunctionMetadataProviderGeneratorTests + { + public class AmbiguousNamespaceTests + { + private readonly Assembly[] _referencedExtensionAssemblies; + + public AmbiguousNamespaceTests() + { + // load all extensions used in tests (match extensions tested on E2E app? Or include ALL extensions?) + var abstractionsExtension = Assembly.LoadFrom("Microsoft.Azure.Functions.Worker.Extensions.Abstractions.dll"); + var httpExtension = Assembly.LoadFrom("Microsoft.Azure.Functions.Worker.Extensions.Http.dll"); + var hostingExtension = typeof(HostBuilder).Assembly; + var diExtension = typeof(DefaultServiceProviderFactory).Assembly; + var hostingAbExtension = typeof(IHost).Assembly; + var diAbExtension = typeof(IServiceCollection).Assembly; + + _referencedExtensionAssemblies = new[] + { + abstractionsExtension, + httpExtension, + hostingExtension, + hostingAbExtension, + diExtension, + diAbExtension + }; + } + + [Theory] + [InlineData(LanguageVersion.CSharp7_3)] + [InlineData(LanguageVersion.CSharp8)] + [InlineData(LanguageVersion.CSharp9)] + [InlineData(LanguageVersion.CSharp10)] + [InlineData(LanguageVersion.CSharp11)] + [InlineData(LanguageVersion.Latest)] + public async Task NamespaceEndingWithTask(LanguageVersion languageVersion) + { + string inputCode = """ + using System; + using System.Collections.Generic; + using Microsoft.Azure.Functions.Worker; + using Microsoft.Azure.Functions.Worker.Http; + + namespace MyCompany.Task + { + public static class HttpTriggerSimple + { + [Function(nameof(HttpTriggerSimple))] + public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.User, "get")] HttpRequestData req, FunctionContext c) + { + return Run(req); + } + + public static HttpResponseData Run(HttpRequestData req) + => req.CreateResponse(System.Net.HttpStatusCode.OK); + } + } + """; + + string expectedGeneratedFileName = $"GeneratedFunctionMetadataProvider.g.cs"; + string expectedOutput = """ + // + using System; + using System.Collections.Generic; + using System.Collections.Immutable; + using System.Text.Json; + using System.Threading.Tasks; + using Microsoft.Azure.Functions.Worker; + using Microsoft.Azure.Functions.Worker.Core.FunctionMetadata; + using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.Hosting; + + namespace TestProject + { + /// + /// Custom implementation that returns function metadata definitions for the current worker."/> + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + public class GeneratedFunctionMetadataProvider : IFunctionMetadataProvider + { + /// + public Task> GetFunctionMetadataAsync(string directory) + { + var metadataList = new List(); + var Function0RawBindings = new List(); + Function0RawBindings.Add(@"{""name"":""req"",""type"":""httpTrigger"",""direction"":""In"",""authLevel"":""User"",""methods"":[""get""]}"); + Function0RawBindings.Add(@"{""name"":""$return"",""type"":""http"",""direction"":""Out""}"); + + var Function0 = new DefaultFunctionMetadata + { + Language = "dotnet-isolated", + Name = "HttpTriggerSimple", + EntryPoint = "MyCompany.Task.HttpTriggerSimple.Run", + RawBindings = Function0RawBindings, + ScriptFile = "TestProject.dll" + }; + metadataList.Add(Function0); + + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); + } + } + + /// + /// Extension methods to enable registration of the custom implementation generated for the current worker. + /// + public static class WorkerHostBuilderFunctionMetadataProviderExtension + { + /// + /// Adds the GeneratedFunctionMetadataProvider to the service collection. + /// During initialization, the worker will return generated function metadata instead of relying on the Azure Functions host for function indexing. + /// + public static IHostBuilder ConfigureGeneratedFunctionMetadataProvider(this IHostBuilder builder) + { + builder.ConfigureServices(s => + { + s.AddSingleton(); + }); + return builder; + } + } + } + """; + + await TestHelpers.RunTestAsync( + _referencedExtensionAssemblies, + inputCode, + expectedGeneratedFileName, + expectedOutput, + languageVersion: languageVersion); + } + } + } +} diff --git a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/AutoConfigureStartupTypeTests.cs b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/AutoConfigureStartupTypeTests.cs index ae6dbdc56..3ca59c1bf 100644 --- a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/AutoConfigureStartupTypeTests.cs +++ b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/AutoConfigureStartupTypeTests.cs @@ -112,7 +112,7 @@ public Task> GetFunctionMetadataAsync(string d }}; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); }} }} diff --git a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/DependentAssemblyTest.NetFx.cs b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/DependentAssemblyTest.NetFx.cs index 9b2e87c00..007b680b7 100644 --- a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/DependentAssemblyTest.NetFx.cs +++ b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/DependentAssemblyTest.NetFx.cs @@ -135,7 +135,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function2); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } diff --git a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/DependentAssemblyTest.cs b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/DependentAssemblyTest.cs index 7de6e6864..1890d6dd0 100644 --- a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/DependentAssemblyTest.cs +++ b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/DependentAssemblyTest.cs @@ -166,7 +166,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function5); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -307,7 +307,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function4); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } diff --git a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/EventHubsBindingsTests.cs b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/EventHubsBindingsTests.cs index 8c4264741..929886732 100644 --- a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/EventHubsBindingsTests.cs +++ b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/EventHubsBindingsTests.cs @@ -124,7 +124,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -258,7 +258,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -361,7 +361,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -552,7 +552,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function3); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -693,7 +693,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function1); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -821,7 +821,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function1); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } diff --git a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/HttpTriggerTests.cs b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/HttpTriggerTests.cs index b95db3252..8cb0b632b 100644 --- a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/HttpTriggerTests.cs +++ b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/HttpTriggerTests.cs @@ -106,7 +106,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -208,7 +208,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -315,7 +315,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -442,7 +442,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function1); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } diff --git a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/IntegratedTriggersAndBindingsTests.cs b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/IntegratedTriggersAndBindingsTests.cs index 44ffaab66..115384f84 100644 --- a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/IntegratedTriggersAndBindingsTests.cs +++ b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/IntegratedTriggersAndBindingsTests.cs @@ -158,7 +158,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function1); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -299,7 +299,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function1); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -423,7 +423,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -532,7 +532,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -627,7 +627,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -723,7 +723,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -854,7 +854,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function2); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -987,7 +987,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function2); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } diff --git a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/KafkaTests.cs b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/KafkaTests.cs index fa0052b08..48e0c3819 100644 --- a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/KafkaTests.cs +++ b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/KafkaTests.cs @@ -114,7 +114,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } diff --git a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/NestedTypesTest.cs b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/NestedTypesTest.cs index adbcbff6a..a37b6afb7 100644 --- a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/NestedTypesTest.cs +++ b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/NestedTypesTest.cs @@ -112,7 +112,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -216,7 +216,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } diff --git a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/RetryOptionsTests.cs b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/RetryOptionsTests.cs index 7800fe95e..0046b717b 100644 --- a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/RetryOptionsTests.cs +++ b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/RetryOptionsTests.cs @@ -117,7 +117,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -224,7 +224,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } diff --git a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/ServiceBustTests.cs b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/ServiceBustTests.cs index d08eb4aec..c4f7d3c61 100644 --- a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/ServiceBustTests.cs +++ b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/ServiceBustTests.cs @@ -115,7 +115,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } diff --git a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/SignalRTest.cs b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/SignalRTest.cs index e136a42aa..e11cb3d83 100644 --- a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/SignalRTest.cs +++ b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/SignalRTest.cs @@ -106,7 +106,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } diff --git a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/StorageBindingTests.cs b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/StorageBindingTests.cs index b632d0c2d..2df2f73cb 100644 --- a/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/StorageBindingTests.cs +++ b/test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/StorageBindingTests.cs @@ -114,7 +114,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -262,7 +262,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function2); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } } @@ -367,7 +367,7 @@ public Task> GetFunctionMetadataAsync(string d }; metadataList.Add(Function0); - return Task.FromResult(metadataList.ToImmutableArray()); + return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray()); } }