Skip to content

Commit b1a94a1

Browse files
authored
Use full namespace for Task.FromResult in function metadata provider generator to avoid namespace conflict (#2681)
1 parent 30fd904 commit b1a94a1

15 files changed

+181
-33
lines changed

sdk/Sdk.Generators/FunctionMetadataProviderGenerator/FunctionMetadataProviderGenerator.Emitter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
4949
{
5050
var metadataList = new List<IFunctionMetadata>();
5151
{{functionMetadataInfo}}
52-
return Task.FromResult(metadataList.ToImmutableArray());
52+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
5353
}
5454
}
5555

sdk/release_notes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212

1313
- Changed `FunctionExecutorGenerator` to avoid generation of long `if`/`else` chains for apps with a large number of functions.
1414

15+
- Use full namespace for `Task.FromResult` in function metadata provider generator to avoid namespace conflict (#2681)
16+
1517
- <entry>
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System.Reflection;
5+
using System.Threading.Tasks;
6+
using Microsoft.Azure.Functions.Worker.Sdk.Generators;
7+
using Microsoft.CodeAnalysis.CSharp;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Hosting;
10+
using Xunit;
11+
12+
namespace Microsoft.Azure.Functions.SdkGeneratorTests
13+
{
14+
public partial class FunctionMetadataProviderGeneratorTests
15+
{
16+
public class AmbiguousNamespaceTests
17+
{
18+
private readonly Assembly[] _referencedExtensionAssemblies;
19+
20+
public AmbiguousNamespaceTests()
21+
{
22+
// load all extensions used in tests (match extensions tested on E2E app? Or include ALL extensions?)
23+
var abstractionsExtension = Assembly.LoadFrom("Microsoft.Azure.Functions.Worker.Extensions.Abstractions.dll");
24+
var httpExtension = Assembly.LoadFrom("Microsoft.Azure.Functions.Worker.Extensions.Http.dll");
25+
var hostingExtension = typeof(HostBuilder).Assembly;
26+
var diExtension = typeof(DefaultServiceProviderFactory).Assembly;
27+
var hostingAbExtension = typeof(IHost).Assembly;
28+
var diAbExtension = typeof(IServiceCollection).Assembly;
29+
30+
_referencedExtensionAssemblies = new[]
31+
{
32+
abstractionsExtension,
33+
httpExtension,
34+
hostingExtension,
35+
hostingAbExtension,
36+
diExtension,
37+
diAbExtension
38+
};
39+
}
40+
41+
[Theory]
42+
[InlineData(LanguageVersion.CSharp7_3)]
43+
[InlineData(LanguageVersion.CSharp8)]
44+
[InlineData(LanguageVersion.CSharp9)]
45+
[InlineData(LanguageVersion.CSharp10)]
46+
[InlineData(LanguageVersion.CSharp11)]
47+
[InlineData(LanguageVersion.Latest)]
48+
public async Task NamespaceEndingWithTask(LanguageVersion languageVersion)
49+
{
50+
string inputCode = """
51+
using System;
52+
using System.Collections.Generic;
53+
using Microsoft.Azure.Functions.Worker;
54+
using Microsoft.Azure.Functions.Worker.Http;
55+
56+
namespace MyCompany.Task
57+
{
58+
public static class HttpTriggerSimple
59+
{
60+
[Function(nameof(HttpTriggerSimple))]
61+
public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.User, "get")] HttpRequestData req, FunctionContext c)
62+
{
63+
return Run(req);
64+
}
65+
66+
public static HttpResponseData Run(HttpRequestData req)
67+
=> req.CreateResponse(System.Net.HttpStatusCode.OK);
68+
}
69+
}
70+
""";
71+
72+
string expectedGeneratedFileName = $"GeneratedFunctionMetadataProvider.g.cs";
73+
string expectedOutput = """
74+
// <auto-generated/>
75+
using System;
76+
using System.Collections.Generic;
77+
using System.Collections.Immutable;
78+
using System.Text.Json;
79+
using System.Threading.Tasks;
80+
using Microsoft.Azure.Functions.Worker;
81+
using Microsoft.Azure.Functions.Worker.Core.FunctionMetadata;
82+
using Microsoft.Extensions.DependencyInjection;
83+
using Microsoft.Extensions.Hosting;
84+
85+
namespace TestProject
86+
{
87+
/// <summary>
88+
/// Custom <see cref="IFunctionMetadataProvider"/> implementation that returns function metadata definitions for the current worker."/>
89+
/// </summary>
90+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
91+
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
92+
public class GeneratedFunctionMetadataProvider : IFunctionMetadataProvider
93+
{
94+
/// <inheritdoc/>
95+
public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string directory)
96+
{
97+
var metadataList = new List<IFunctionMetadata>();
98+
var Function0RawBindings = new List<string>();
99+
Function0RawBindings.Add(@"{""name"":""req"",""type"":""httpTrigger"",""direction"":""In"",""authLevel"":""User"",""methods"":[""get""]}");
100+
Function0RawBindings.Add(@"{""name"":""$return"",""type"":""http"",""direction"":""Out""}");
101+
102+
var Function0 = new DefaultFunctionMetadata
103+
{
104+
Language = "dotnet-isolated",
105+
Name = "HttpTriggerSimple",
106+
EntryPoint = "MyCompany.Task.HttpTriggerSimple.Run",
107+
RawBindings = Function0RawBindings,
108+
ScriptFile = "TestProject.dll"
109+
};
110+
metadataList.Add(Function0);
111+
112+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
113+
}
114+
}
115+
116+
/// <summary>
117+
/// Extension methods to enable registration of the custom <see cref="IFunctionMetadataProvider"/> implementation generated for the current worker.
118+
/// </summary>
119+
public static class WorkerHostBuilderFunctionMetadataProviderExtension
120+
{
121+
///<summary>
122+
/// Adds the GeneratedFunctionMetadataProvider to the service collection.
123+
/// During initialization, the worker will return generated function metadata instead of relying on the Azure Functions host for function indexing.
124+
///</summary>
125+
public static IHostBuilder ConfigureGeneratedFunctionMetadataProvider(this IHostBuilder builder)
126+
{
127+
builder.ConfigureServices(s =>
128+
{
129+
s.AddSingleton<IFunctionMetadataProvider, GeneratedFunctionMetadataProvider>();
130+
});
131+
return builder;
132+
}
133+
}
134+
}
135+
""";
136+
137+
await TestHelpers.RunTestAsync<FunctionMetadataProviderGenerator>(
138+
_referencedExtensionAssemblies,
139+
inputCode,
140+
expectedGeneratedFileName,
141+
expectedOutput,
142+
languageVersion: languageVersion);
143+
}
144+
}
145+
}
146+
}

test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/AutoConfigureStartupTypeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
112112
}};
113113
metadataList.Add(Function0);
114114
115-
return Task.FromResult(metadataList.ToImmutableArray());
115+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
116116
}}
117117
}}
118118

test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/DependentAssemblyTest.NetFx.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
135135
};
136136
metadataList.Add(Function2);
137137
138-
return Task.FromResult(metadataList.ToImmutableArray());
138+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
139139
}
140140
}
141141

test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/DependentAssemblyTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
166166
};
167167
metadataList.Add(Function5);
168168
169-
return Task.FromResult(metadataList.ToImmutableArray());
169+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
170170
}
171171
}
172172
@@ -307,7 +307,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
307307
};
308308
metadataList.Add(Function4);
309309
310-
return Task.FromResult(metadataList.ToImmutableArray());
310+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
311311
}
312312
}
313313

test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/EventHubsBindingsTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
124124
};
125125
metadataList.Add(Function0);
126126
127-
return Task.FromResult(metadataList.ToImmutableArray());
127+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
128128
}
129129
}
130130
@@ -258,7 +258,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
258258
};
259259
metadataList.Add(Function0);
260260
261-
return Task.FromResult(metadataList.ToImmutableArray());
261+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
262262
}
263263
}
264264
@@ -361,7 +361,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
361361
};
362362
metadataList.Add(Function0);
363363
364-
return Task.FromResult(metadataList.ToImmutableArray());
364+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
365365
}
366366
}
367367
@@ -552,7 +552,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
552552
};
553553
metadataList.Add(Function3);
554554
555-
return Task.FromResult(metadataList.ToImmutableArray());
555+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
556556
}
557557
}
558558
@@ -693,7 +693,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
693693
};
694694
metadataList.Add(Function1);
695695
696-
return Task.FromResult(metadataList.ToImmutableArray());
696+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
697697
}
698698
}
699699
@@ -821,7 +821,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
821821
};
822822
metadataList.Add(Function1);
823823
824-
return Task.FromResult(metadataList.ToImmutableArray());
824+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
825825
}
826826
}
827827

test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/HttpTriggerTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
106106
};
107107
metadataList.Add(Function0);
108108
109-
return Task.FromResult(metadataList.ToImmutableArray());
109+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
110110
}
111111
}
112112
@@ -208,7 +208,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
208208
};
209209
metadataList.Add(Function0);
210210
211-
return Task.FromResult(metadataList.ToImmutableArray());
211+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
212212
}
213213
}
214214
@@ -315,7 +315,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
315315
};
316316
metadataList.Add(Function0);
317317
318-
return Task.FromResult(metadataList.ToImmutableArray());
318+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
319319
}
320320
}
321321
@@ -442,7 +442,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
442442
};
443443
metadataList.Add(Function1);
444444
445-
return Task.FromResult(metadataList.ToImmutableArray());
445+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
446446
}
447447
}
448448

test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/IntegratedTriggersAndBindingsTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
158158
};
159159
metadataList.Add(Function1);
160160
161-
return Task.FromResult(metadataList.ToImmutableArray());
161+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
162162
}
163163
}
164164
@@ -299,7 +299,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
299299
};
300300
metadataList.Add(Function1);
301301
302-
return Task.FromResult(metadataList.ToImmutableArray());
302+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
303303
}
304304
}
305305
@@ -423,7 +423,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
423423
};
424424
metadataList.Add(Function0);
425425
426-
return Task.FromResult(metadataList.ToImmutableArray());
426+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
427427
}
428428
}
429429
@@ -532,7 +532,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
532532
};
533533
metadataList.Add(Function0);
534534
535-
return Task.FromResult(metadataList.ToImmutableArray());
535+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
536536
}
537537
}
538538
@@ -627,7 +627,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
627627
};
628628
metadataList.Add(Function0);
629629
630-
return Task.FromResult(metadataList.ToImmutableArray());
630+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
631631
}
632632
}
633633
@@ -723,7 +723,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
723723
};
724724
metadataList.Add(Function0);
725725
726-
return Task.FromResult(metadataList.ToImmutableArray());
726+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
727727
}
728728
}
729729
@@ -854,7 +854,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
854854
};
855855
metadataList.Add(Function2);
856856
857-
return Task.FromResult(metadataList.ToImmutableArray());
857+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
858858
}
859859
}
860860
@@ -987,7 +987,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
987987
};
988988
metadataList.Add(Function2);
989989
990-
return Task.FromResult(metadataList.ToImmutableArray());
990+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
991991
}
992992
}
993993

test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/KafkaTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
114114
};
115115
metadataList.Add(Function0);
116116
117-
return Task.FromResult(metadataList.ToImmutableArray());
117+
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
118118
}
119119
}
120120

0 commit comments

Comments
 (0)