Skip to content

Commit 9069e97

Browse files
committed
Avoid all service-related codegen when AddServicesExtension=false
We were still emitting partial method implementations which caused build failures. Adding that property effectively causes only the ServiceAttribute to be emitted.
1 parent f05b1d5 commit 9069e97

File tree

5 files changed

+81
-5
lines changed

5 files changed

+81
-5
lines changed

DependencyInjection.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<Project Path="src/CodeAnalysis.Tests/CodeAnalysis.Tests.csproj" />
44
<Project Path="src/DependencyInjection.Tests/DependencyInjection.Tests.csproj" />
55
<Project Path="src/DependencyInjection/DependencyInjection.csproj" />
6+
<Project Path="src/NoAddServices/NoAddServices.csproj" Id="b96e7987-cd29-4648-920c-ef6e2830eb3f" />
67
</Solution>

src/CodeAnalysis.Tests/AddServicesAnalyzerTests.cs

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Collections.Immutable;
1+
using System.Collections.Immutable;
42
using System.IO;
5-
using System.Linq;
63
using System.Threading.Tasks;
74
using Devlooped.Extensions.DependencyInjection;
8-
using Microsoft.CodeAnalysis.CSharp;
95
using Microsoft.CodeAnalysis.CSharp.Testing;
106
using Microsoft.CodeAnalysis.Testing;
117
using Xunit;
@@ -150,6 +146,59 @@ public static void Main()
150146
await test.RunAsync();
151147
}
152148

149+
[Fact]
150+
public async Task NoServicesAddedIfFalse()
151+
{
152+
var test = new AnalyzerTest
153+
{
154+
TestCode = """
155+
using System;
156+
using Microsoft.Extensions.DependencyInjection;
157+
158+
public record Command;
159+
160+
public static class Program
161+
{
162+
public static void Main()
163+
{
164+
var services = new ServiceCollection();
165+
{|#0:services.AddSingleton(new Command())|};
166+
}
167+
}
168+
""",
169+
TestState =
170+
{
171+
AnalyzerConfigFiles =
172+
{
173+
("/.editorconfig",
174+
"""
175+
is_global = true
176+
build_property.AddServicesExtension = false
177+
""")
178+
},
179+
Sources =
180+
{
181+
ThisAssembly.Resources.AddServicesNoReflectionExtension.Text,
182+
ThisAssembly.Resources.ServiceAttribute.Text,
183+
ThisAssembly.Resources.ServiceAttribute_1.Text
184+
},
185+
ReferenceAssemblies = new ReferenceAssemblies(
186+
"net8.0",
187+
new PackageIdentity(
188+
"Microsoft.NETCore.App.Ref", "8.0.0"),
189+
Path.Combine("ref", "net8.0"))
190+
.AddPackages(ImmutableArray.Create(
191+
new PackageIdentity("Microsoft.Extensions.DependencyInjection", "8.0.0")))
192+
},
193+
}.WithPreprocessorSymbols();
194+
195+
var expected = Verifier.Diagnostic(AddServicesAnalyzer.NoAddServicesCall).WithLocation(0);
196+
test.ExpectedDiagnostics.Add(expected);
197+
198+
await test.RunAsync();
199+
}
200+
201+
153202
[Fact]
154203
public async Task WarnIfAddServicesMissingMultipleLocations()
155204
{

src/DependencyInjection.Tests/DependencyInjection.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<TargetFramework>net10.0</TargetFramework>
77
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
88
<RootNamespace>Tests</RootNamespace>
9+
<!--<AddServicesExtension>false</AddServicesExtension>-->
910
</PropertyGroup>
1011

1112
<ItemGroup>

src/DependencyInjection/IncrementalGenerator.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ void ReportInconsistencies(SourceProductionContext context, ImmutableArray<Servi
339339

340340
void AddPartial(string methodName, SourceProductionContext ctx, (ImmutableArray<KeyedService> Types, Compilation Compilation) data)
341341
{
342+
if (data.Types.IsEmpty)
343+
return;
344+
342345
var builder = new StringBuilder()
343346
.AppendLine("// <auto-generated />");
344347

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Import Project="..\DependencyInjection\Devlooped.Extensions.DependencyInjection.props" />
4+
5+
<PropertyGroup>
6+
<TargetFramework>net10.0</TargetFramework>
7+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
8+
<!-- This property causes no AddXXX partial methods to be emitted -->
9+
<AddServicesExtension>false</AddServicesExtension>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.1" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\DependencyInjection\DependencyInjection.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
18+
</ItemGroup>
19+
20+
<Import Project="..\DependencyInjection\Devlooped.Extensions.DependencyInjection.targets" />
21+
22+
</Project>

0 commit comments

Comments
 (0)