Skip to content

Commit a43c169

Browse files
[release/6.0] Startup analyzers work for minimal apps (#35987)
* wip * more * test wip * shared tests * fix main * fb Co-authored-by: Brennan <[email protected]>
1 parent 8aec784 commit a43c169

9 files changed

+751
-340
lines changed

src/Analyzers/Analyzers/src/BuildServiceProviderAnalyzer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public BuildServiceProviderAnalyzer(StartupAnalysis context)
1919
public void AnalyzeSymbol(SymbolAnalysisContext context)
2020
{
2121
Debug.Assert(context.Symbol.Kind == SymbolKind.NamedType);
22-
Debug.Assert(StartupFacts.IsStartupClass(_context.StartupSymbols, (INamedTypeSymbol)context.Symbol));
2322

2423
var type = (INamedTypeSymbol)context.Symbol;
2524

src/Analyzers/Analyzers/src/StartupAnalyzer.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Immutable;
66
using Microsoft.CodeAnalysis;
77
using Microsoft.CodeAnalysis.Diagnostics;
8+
using Microsoft.CodeAnalysis.Operations;
89

910
namespace Microsoft.AspNetCore.Analyzers
1011
{
@@ -35,10 +36,12 @@ private void OnCompilationStart(CompilationStartAnalysisContext context)
3536
return;
3637
}
3738

39+
var entryPoint = context.Compilation.GetEntryPoint(context.CancellationToken);
40+
3841
context.RegisterSymbolStartAction(context =>
3942
{
4043
var type = (INamedTypeSymbol)context.Symbol;
41-
if (!StartupFacts.IsStartupClass(symbols, type))
44+
if (!StartupFacts.IsStartupClass(symbols, type) && !SymbolEqualityComparer.Default.Equals(entryPoint?.ContainingType, type))
4245
{
4346
// Not a startup class, nothing to do.
4447
return;
@@ -60,18 +63,28 @@ private void OnCompilationStart(CompilationStartAnalysisContext context)
6063
}
6164

6265
var method = (IMethodSymbol)context.OwningSymbol;
63-
if (StartupFacts.IsConfigureServices(symbols, method))
66+
var isConfigureServices = StartupFacts.IsConfigureServices(symbols, method);
67+
if (isConfigureServices)
6468
{
6569
OnConfigureServicesMethodFound(method);
70+
}
6671

72+
// In the future we can consider looking at more methods, but for now limit to Main, implicit Main, and Configure* methods
73+
var isMain = SymbolEqualityComparer.Default.Equals(entryPoint, context.OwningSymbol);
74+
75+
if (isConfigureServices || isMain)
76+
{
6777
services.AnalyzeConfigureServices(context);
6878
options.AnalyzeConfigureServices(context);
6979
}
7080

71-
if (StartupFacts.IsConfigure(symbols, method))
81+
var isConfigure = StartupFacts.IsConfigure(symbols, method);
82+
if (isConfigure)
7283
{
7384
OnConfigureMethodFound(method);
74-
85+
}
86+
if (isConfigure || isMain)
87+
{
7588
middleware.AnalyzeConfigureMethod(context);
7689
}
7790
});

src/Analyzers/Analyzers/src/UseAuthorizationAnalyzer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public UseAuthorizationAnalyzer(StartupAnalysis context)
2020
public void AnalyzeSymbol(SymbolAnalysisContext context)
2121
{
2222
Debug.Assert(context.Symbol.Kind == SymbolKind.NamedType);
23-
Debug.Assert(StartupFacts.IsStartupClass(_context.StartupSymbols, (INamedTypeSymbol)context.Symbol));
2423

2524
var type = (INamedTypeSymbol)context.Symbol;
2625

src/Analyzers/Analyzers/src/UseMvcAnalyzer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
@@ -19,7 +19,6 @@ public UseMvcAnalyzer(StartupAnalysis context)
1919
public void AnalyzeSymbol(SymbolAnalysisContext context)
2020
{
2121
Debug.Assert(context.Symbol.Kind == SymbolKind.NamedType);
22-
Debug.Assert(StartupFacts.IsStartupClass(_context.StartupSymbols, (INamedTypeSymbol)context.Symbol));
2322

2423
var type = (INamedTypeSymbol)context.Symbol;
2524

src/Analyzers/Analyzers/test/AnalyzersDiagnosticAnalyzerRunner.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ namespace Microsoft.AspNetCore.Analyzers
1414
{
1515
internal class AnalyzersDiagnosticAnalyzerRunner : DiagnosticAnalyzerRunner
1616
{
17-
public AnalyzersDiagnosticAnalyzerRunner(DiagnosticAnalyzer analyzer)
17+
private readonly OutputKind _outputKind;
18+
19+
public AnalyzersDiagnosticAnalyzerRunner(DiagnosticAnalyzer analyzer, OutputKind? outputKind = null)
1820
{
1921
Analyzer = analyzer;
22+
_outputKind = outputKind ?? OutputKind.DynamicallyLinkedLibrary;
2023
}
2124

2225
public DiagnosticAnalyzer Analyzer { get; }
@@ -51,5 +54,10 @@ public Task<Diagnostic[]> GetDiagnosticsAsync(Project project)
5154
{
5255
return GetDiagnosticsAsync(new[] { project }, Analyzer, Array.Empty<string>());
5356
}
57+
58+
protected override CompilationOptions ConfigureCompilationOptions(CompilationOptions options)
59+
{
60+
return options.WithOutputKind(_outputKind);
61+
}
5462
}
5563
}

src/Analyzers/Analyzers/test/Microsoft.AspNetCore.Analyzers.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<Reference Include="Microsoft.AspNetCore.Components.Server" />
2323
<Reference Include="Microsoft.AspNetCore.Mvc" />
2424
<Reference Include="Microsoft.AspNetCore.SignalR" />
25+
<Reference Include="Microsoft.AspNetCore" />
2526
</ItemGroup>
2627

2728
</Project>

0 commit comments

Comments
 (0)