Skip to content

Commit 4a72f42

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 4a72f42

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-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" />
67
</Solution>

src/CodeAnalysis.Tests/AddServicesAnalyzerTests.cs

Lines changed: 1 addition & 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;

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)