Skip to content

Commit dd7aa5d

Browse files
authored
[MSBUILD SDK] Implicitly reference existing SDK analyzer packages (#3275)
* Implicitly reference existing SDK analyzer packages * Port analyzer properties * Add extra tests
1 parent bbf7850 commit dd7aa5d

File tree

6 files changed

+116
-5
lines changed

6 files changed

+116
-5
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!--
2+
***********************************************************************************************
3+
Azure.Functions.Sdk.Analyzers.props
4+
5+
WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
6+
created a backup copy. Incorrect changes to this file will make it
7+
impossible to load or build your projects from the command-line or the IDE.
8+
9+
***********************************************************************************************
10+
-->
11+
12+
<Project>
13+
14+
<PropertyGroup>
15+
<FunctionsExecutionModel>isolated</FunctionsExecutionModel>
16+
<FunctionsEnableMetadataSourceGen>true</FunctionsEnableMetadataSourceGen>
17+
<FunctionsAutoRegisterGeneratedMetadataProvider>true</FunctionsAutoRegisterGeneratedMetadataProvider>
18+
<FunctionsEnableExecutorSourceGen>true</FunctionsEnableExecutorSourceGen>
19+
<FunctionsAutoRegisterGeneratedFunctionsExecutor>true</FunctionsAutoRegisterGeneratedFunctionsExecutor>
20+
</PropertyGroup>
21+
22+
<ItemGroup>
23+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk.Analyzers" Version="1.2.2" IsImplicitlyDefined="true">
24+
<PrivateAssets>all</PrivateAssets>
25+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
26+
</PackageReference>
27+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk.Generators" Version="1.3.6" IsImplicitlyDefined="true">
28+
<PrivateAssets>all</PrivateAssets>
29+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
30+
</PackageReference>
31+
</ItemGroup>
32+
33+
<ItemGroup>
34+
<CompilerVisibleProperty Include="FunctionsEnableMetadataSourceGen" />
35+
<CompilerVisibleProperty Include="FunctionsEnableExecutorSourceGen" />
36+
<CompilerVisibleProperty Include="FunctionsAutoRegisterGeneratedFunctionsExecutor" />
37+
<CompilerVisibleProperty Include="FunctionsAutoRegisterGeneratedMetadataProvider" />
38+
<CompilerVisibleProperty Include="FunctionsGeneratedCodeNamespace" />
39+
<CompilerVisibleProperty Include="TargetFrameworkIdentifier" />
40+
<CompilerVisibleProperty Include="FunctionsExecutionModel" />
41+
</ItemGroup>
42+
43+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!--
2+
***********************************************************************************************
3+
Azure.Functions.Sdk.Analyzers.targets
4+
5+
WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
6+
created a backup copy. Incorrect changes to this file will make it
7+
impossible to load or build your projects from the command-line or the IDE.
8+
9+
***********************************************************************************************
10+
-->
11+
12+
<Project>
13+
14+
<PropertyGroup>
15+
<FunctionsGeneratedCodeNamespace Condition="'$(FunctionsGeneratedCodeNamespace)' == ''">$(RootNamespace.Replace("-", "_"))</FunctionsGeneratedCodeNamespace>
16+
</PropertyGroup>
17+
18+
</Project>

src/Azure.Functions.Sdk/Targets/Azure.Functions.Sdk.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and
1717
</PropertyGroup>
1818

1919
<Import Sdk="Microsoft.NET.Sdk.Worker" Project="Sdk.props" />
20+
<Import Project="$(MSBuildThisFileDirectory)Azure.Functions.Sdk.Analyzers.props" />
2021
<Import Project="$(MSBuildThisFileDirectory)extensions/Azure.Functions.Sdk.Extensions.props" />
2122

2223
<!-- Enable Azure Functions project capability to enable tools -->

src/Azure.Functions.Sdk/Targets/Azure.Functions.Sdk.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and
1515
<UsingTask TaskName="$(AzureFunctionsTaskNamespace).FuncSdkLog" AssemblyFile="$(AzureFunctionsSdkTasksAssembly)" />
1616
<UsingTask TaskName="$(AzureFunctionsTaskNamespace).GenerateWorkerConfig" AssemblyFile="$(AzureFunctionsSdkTasksAssembly)" />
1717

18-
1918
<PropertyGroup>
2019
<_AzureFunctionsVersionStandardized>$(AzureFunctionsVersion.ToLowerInvariant().Split('-')[0])</_AzureFunctionsVersionStandardized>
2120
<_FunctionsRuntimeMajorVersion>$(_AzureFunctionsVersionStandardized.TrimStart('vV'))</_FunctionsRuntimeMajorVersion>
@@ -24,6 +23,7 @@ WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and
2423
<FunctionsToolingSuffix Condition="'$(FunctionsToolingSuffix)' == ''">$(_ToolingSuffix)</FunctionsToolingSuffix>
2524
</PropertyGroup>
2625

26+
<Import Project="$(MSBuildThisFileDirectory)Azure.Functions.Sdk.Analyzers.targets" />
2727
<Import Project="$(MSBuildThisFileDirectory)extensions/Azure.Functions.Sdk.Extensions.targets" />
2828
<Import Project="$(MSBuildFunctionsTargetsPath)Microsoft.Azure.Functions.Designtime.targets"
2929
Condition="Exists('$(MSBuildFunctionsTargetsPath)Microsoft.Azure.Functions.Designtime.targets')" />

test/Azure.Functions.Sdk.Tests/Integration/SdkEndToEndTests.Items.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ namespace Azure.Functions.Sdk.Tests.Integration;
88

99
public partial class SdkEndToEndTests
1010
{
11+
private static readonly string[] CompilerVisiblePropertiesExpected =
12+
[
13+
"FunctionsEnableMetadataSourceGen",
14+
"FunctionsAutoRegisterGeneratedMetadataProvider",
15+
"FunctionsEnableExecutorSourceGen",
16+
"FunctionsAutoRegisterGeneratedFunctionsExecutor",
17+
"FunctionsGeneratedCodeNamespace",
18+
"TargetFrameworkIdentifier",
19+
"FunctionsExecutionModel",
20+
];
21+
1122
[Fact]
1223
public void Item_LocalSettingsJson_ExpectedMetadata()
1324
{
@@ -45,7 +56,7 @@ public void Item_HostJson_ExpectedMetadata()
4556
}
4657

4758
[Fact]
48-
public void Item_WorkerPackage_IsIncluded()
59+
public void Item_ImplicitPackages_AreIncluded()
4960
{
5061
// Arrange
5162
ProjectCreator project = ProjectCreator.Templates.AzureFunctionsProject(
@@ -56,9 +67,32 @@ public void Item_WorkerPackage_IsIncluded()
5667

5768
// Assert
5869
items.Should().ContainSingle(x => x.EvaluatedInclude == "Microsoft.Azure.Functions.Worker")
59-
.Which.Should()
60-
.HaveMetadata("Version", "2.2.0")
61-
.And.HaveMetadata("IsImplicitlyDefined", "true");
70+
.Which.Should().HaveMetadata("IsImplicitlyDefined", "true");
71+
72+
items.Should().ContainSingle(x => x.EvaluatedInclude == "Microsoft.Azure.Functions.Worker.Sdk.Analyzers")
73+
.Which.Should().HaveMetadata("IsImplicitlyDefined", "true")
74+
.And.HaveMetadata("PrivateAssets", "all");
75+
76+
items.Should().ContainSingle(x => x.EvaluatedInclude == "Microsoft.Azure.Functions.Worker.Sdk.Generators")
77+
.Which.Should().HaveMetadata("IsImplicitlyDefined", "true")
78+
.And.HaveMetadata("PrivateAssets", "all");
79+
}
80+
81+
[Fact]
82+
public void Item_CompilerVisibleProperty_AreIncluded()
83+
{
84+
// Arrange
85+
ProjectCreator project = ProjectCreator.Templates.AzureFunctionsProject(
86+
GetTempCsproj());
87+
88+
// Act
89+
project.TryGetItems("CompilerVisibleProperty", out IReadOnlyCollection<ProjectItem>? items);
90+
91+
// Assert
92+
foreach (string expected in CompilerVisiblePropertiesExpected)
93+
{
94+
items.Should().ContainSingle(x => x.EvaluatedInclude == expected);
95+
}
6296
}
6397

6498
[Fact]

test/Azure.Functions.Sdk.Tests/Integration/SdkEndToEndTests.Properties.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,19 @@ public void Property_ToolingSuffix_NetFx_MatchesTargetFramework(string tfm)
5858
// Assert
5959
toolingSuffix.Should().Be("netfx-isolated");
6060
}
61+
62+
[Fact]
63+
public void Property_AnalyzerProperties_ArePresent()
64+
{
65+
// Arrange
66+
ProjectCreator project = ProjectCreator.Templates.AzureFunctionsProject(
67+
GetTempCsproj());
68+
69+
// Act & Assert
70+
foreach (string property in CompilerVisiblePropertiesExpected)
71+
{
72+
project.TryGetPropertyValue(property, out string? value);
73+
value.Should().NotBeNullOrEmpty($"Property '{property}' should have a value.");
74+
}
75+
}
6176
}

0 commit comments

Comments
 (0)