Skip to content

Commit 8f3c554

Browse files
authored
Re-add SDK refactor with referenced assembly scanning fixed. (#2347)
* Revert "Reverting changes introduced in #1946 (#2313)" (#2346) This reverts commit c34a868. * Directly use ReferencePath for scanning reference assemblies * update release_notes.md * Update log message * Add e2e test to verify SDK assembly scanning * Include nuget.org for test * Pass https source first * Add package with no restore * Refactor Sdk e2e build steps * Remove --no-restore
1 parent cf06831 commit 8f3c554

File tree

19 files changed

+536
-520
lines changed

19 files changed

+536
-520
lines changed

DotNetWorker.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Worker.Extensions.Shared.Te
140140
EndProject
141141
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Worker.Extensions.Http.AspNetCore.Analyzers", "extensions\Worker.Extensions.Http.AspNetCore.Analyzers\Worker.Extensions.Http.AspNetCore.Analyzers.csproj", "{7B6C2920-7A02-43B2-8DA0-7B76B9FAFC6B}"
142142
EndProject
143+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Net7Worker", "samples\Net7Worker\Net7Worker.csproj", "{BE1F79C3-24FA-4BC8-BAB2-C1AD321B13FF}"
144+
EndProject
143145
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependentAssemblyWithFunctions.NetStandard", "test\DependentAssemblyWithFunctions.NetStandard\DependentAssemblyWithFunctions.NetStandard.csproj", "{198DA072-F94F-4585-A744-97B3DAC21686}"
144146
EndProject
145147
Global
@@ -344,6 +346,10 @@ Global
344346
{D8E79B53-9A44-46CC-9D7A-E9494FC8CAF4}.Debug|Any CPU.Build.0 = Debug|Any CPU
345347
{D8E79B53-9A44-46CC-9D7A-E9494FC8CAF4}.Release|Any CPU.ActiveCfg = Release|Any CPU
346348
{D8E79B53-9A44-46CC-9D7A-E9494FC8CAF4}.Release|Any CPU.Build.0 = Release|Any CPU
349+
{BE1F79C3-24FA-4BC8-BAB2-C1AD321B13FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
350+
{BE1F79C3-24FA-4BC8-BAB2-C1AD321B13FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
351+
{BE1F79C3-24FA-4BC8-BAB2-C1AD321B13FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
352+
{BE1F79C3-24FA-4BC8-BAB2-C1AD321B13FF}.Release|Any CPU.Build.0 = Release|Any CPU
347353
{B6342174-5436-4846-B43C-39D8E34AE5CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
348354
{B6342174-5436-4846-B43C-39D8E34AE5CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
349355
{B6342174-5436-4846-B43C-39D8E34AE5CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -417,6 +423,7 @@ Global
417423
{D8E79B53-9A44-46CC-9D7A-E9494FC8CAF4} = {AA4D318D-101B-49E7-A4EC-B34165AEDB33}
418424
{B6342174-5436-4846-B43C-39D8E34AE5CF} = {FD7243E4-BF18-43F8-8744-BA1D17ACF378}
419425
{7B6C2920-7A02-43B2-8DA0-7B76B9FAFC6B} = {A7B4FF1E-3DF7-4F28-9333-D0961CDDF702}
426+
{BE1F79C3-24FA-4BC8-BAB2-C1AD321B13FF} = {9D6603BD-7EA2-4D11-A69C-0D9E01317FD6}
420427
{198DA072-F94F-4585-A744-97B3DAC21686} = {B5821230-6E0A-4535-88A9-ED31B6F07596}
421428
EndGlobalSection
422429
GlobalSection(ExtensibilityGlobals) = postSolution

build/Sdk.slnf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"solution": {
3+
"path": "..\\DotNetWorker.sln",
4+
"projects": [
5+
"sdk\\Sdk.Analyzers\\Sdk.Analyzers.csproj",
6+
"sdk\\Sdk.Generators\\Sdk.Generators.csproj",
7+
"sdk\\Sdk\\Sdk.csproj"
8+
]
9+
}
10+
}

samples/FunctionApp/FunctionApp.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
88
<SignAssembly>true</SignAssembly>
99
<AssemblyOriginatorKeyFile>..\..\key.snk</AssemblyOriginatorKeyFile>
10+
<SdkVersion>1.17.2</SdkVersion>
1011
</PropertyGroup>
1112
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
1213
<DefineConstants>DEBUG;TRACE</DefineConstants>
1314
</PropertyGroup>
1415

1516
<ItemGroup>
1617
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
17-
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.0" />
18+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="$(SdkVersion)" />
1819
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1920
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
2021
</ItemGroup>

sdk/Sdk/ExtensionsCsprojGenerator.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.Azure.Functions.Worker.Sdk
1010
{
1111
internal class ExtensionsCsprojGenerator
1212
{
13-
private const string ExtensionsProjectName = "WorkerExtensions.csproj";
13+
internal const string ExtensionsProjectName = "WorkerExtensions.csproj";
1414

1515
private readonly IDictionary<string, string> _extensions;
1616
private readonly string _outputPath;
@@ -31,9 +31,20 @@ public void Generate()
3131
{
3232
var extensionsCsprojFilePath = Path.Combine(_outputPath, ExtensionsProjectName);
3333

34-
RecreateDirectory(_outputPath);
34+
string csproj = GetCsProjContent();
35+
if (File.Exists(extensionsCsprojFilePath))
36+
{
37+
string existing = File.ReadAllText(extensionsCsprojFilePath);
38+
if (string.Equals(csproj, existing, StringComparison.Ordinal))
39+
{
40+
// If contents are the same, only touch the file to update timestamp.
41+
File.SetLastWriteTimeUtc(extensionsCsprojFilePath, DateTime.UtcNow);
42+
return;
43+
}
44+
}
3545

36-
WriteExtensionsCsProj(extensionsCsprojFilePath);
46+
RecreateDirectory(_outputPath);
47+
File.WriteAllText(extensionsCsprojFilePath, csproj);
3748
}
3849

3950
private void RecreateDirectory(string directoryPath)
@@ -46,13 +57,6 @@ private void RecreateDirectory(string directoryPath)
4657
Directory.CreateDirectory(directoryPath);
4758
}
4859

49-
private void WriteExtensionsCsProj(string filePath)
50-
{
51-
string csprojContent = GetCsProjContent();
52-
53-
File.WriteAllText(filePath, csprojContent);
54-
}
55-
5660
internal string GetCsProjContent()
5761
{
5862
string extensionReferences = GetExtensionReferences();

sdk/Sdk/FunctionMetadataGenerator.cs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Dynamic;
88
using System.IO;
99
using System.Linq;
10+
using Microsoft.Build.Framework;
1011
using Mono.Cecil;
1112
using Mono.Collections.Generic;
1213

@@ -42,44 +43,33 @@ public IDictionary<string, string> Extensions
4243
}
4344
}
4445

45-
public IEnumerable<SdkFunctionMetadata> GenerateFunctionMetadata(string assemblyPath, IEnumerable<string> referencePaths)
46+
public IEnumerable<SdkFunctionMetadata> GenerateFunctionMetadata(string assemblyPath, IEnumerable<ITaskItem> referencePaths)
4647
{
47-
string sourcePath = Path.GetDirectoryName(assemblyPath);
48+
var targetAssemblies = new List<string>() { assemblyPath };
4849

49-
var targetAssemblies = new List<string>(Directory.GetFiles(sourcePath, "*.dll"));
50+
// We don't need to scan assemblies that come from a framework (.NET, AspNetCore), they won't have functions types in them.
51+
targetAssemblies.AddRange(referencePaths.Where(x => x.GetMetadata("FrameworkReferenceName") == "").Select(x => x.ItemSpec));
52+
_logger.LogMessage($"Found {targetAssemblies.Count} assemblies to evaluate.");
5053

51-
if (!assemblyPath.EndsWith(".dll"))
54+
var resolver = new DefaultAssemblyResolver();
55+
foreach (string referencePath in referencePaths.Select(p => Path.GetDirectoryName(p.ItemSpec)).Distinct())
5256
{
53-
targetAssemblies.Add(assemblyPath);
57+
resolver.AddSearchDirectory(referencePath);
5458
}
5559

60+
ReaderParameters readerParams = new ReaderParameters { AssemblyResolver = resolver };
5661
var functions = new List<SdkFunctionMetadata>();
57-
58-
_logger.LogMessage($"Found {targetAssemblies.Count} assemblies to evaluate in '{sourcePath}':");
59-
6062
foreach (var path in targetAssemblies)
6163
{
6264
using (_logger.Indent())
6365
{
64-
_logger.LogMessage($"{Path.GetFileName(path)}");
66+
_logger.LogMessage($"Collecting function metadata from {Path.GetFileName(path)}.");
6567

6668
using (_logger.Indent())
6769
{
6870
try
6971
{
70-
BaseAssemblyResolver resolver = new DefaultAssemblyResolver();
71-
72-
foreach (string referencePath in referencePaths.Select(p => Path.GetDirectoryName(p)).Distinct())
73-
{
74-
resolver.AddSearchDirectory(referencePath);
75-
}
76-
77-
resolver.AddSearchDirectory(Path.GetDirectoryName(path));
78-
79-
ReaderParameters readerParams = new ReaderParameters { AssemblyResolver = resolver };
80-
8172
var moduleDefinition = ModuleDefinition.ReadModule(path, readerParams);
82-
8373
functions.AddRange(GenerateFunctionMetadata(moduleDefinition));
8474
}
8575
catch (BadImageFormatException)

sdk/Sdk/Sdk.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
<PropertyGroup>
44
<MinorProductVersion>17</MinorProductVersion>
5-
<PatchProductVersion>2</PatchProductVersion>
5+
<PatchProductVersion>3</PatchProductVersion>
6+
<VersionSuffix>-preview1</VersionSuffix>
67
<TargetFrameworks>netstandard2.0;net472</TargetFrameworks>
78
<PackageId>Microsoft.Azure.Functions.Worker.Sdk</PackageId>
89
<Description>This package provides development time support for the Azure Functions .NET Worker.</Description>

0 commit comments

Comments
 (0)