Skip to content

Startup analyzers work for minimal apps #35747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public BuildServiceProviderAnalyzer(StartupAnalysis context)
public void AnalyzeSymbol(SymbolAnalysisContext context)
{
Debug.Assert(context.Symbol.Kind == SymbolKind.NamedType);
Debug.Assert(StartupFacts.IsStartupClass(_context.StartupSymbols, (INamedTypeSymbol)context.Symbol));

var type = (INamedTypeSymbol)context.Symbol;

Expand Down
21 changes: 17 additions & 4 deletions src/Analyzers/Analyzers/src/StartupAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;

namespace Microsoft.AspNetCore.Analyzers
{
Expand Down Expand Up @@ -35,10 +36,12 @@ private void OnCompilationStart(CompilationStartAnalysisContext context)
return;
}

var entryPoint = context.Compilation.GetEntryPoint(context.CancellationToken);

context.RegisterSymbolStartAction(context =>
{
var type = (INamedTypeSymbol)context.Symbol;
if (!StartupFacts.IsStartupClass(symbols, type))
if (!StartupFacts.IsStartupClass(symbols, type) && !SymbolEqualityComparer.Default.Equals(entryPoint?.ContainingType, type))
{
// Not a startup class, nothing to do.
return;
Expand All @@ -60,18 +63,28 @@ private void OnCompilationStart(CompilationStartAnalysisContext context)
}

var method = (IMethodSymbol)context.OwningSymbol;
if (StartupFacts.IsConfigureServices(symbols, method))
var isConfigureServices = StartupFacts.IsConfigureServices(symbols, method);
if (isConfigureServices)
{
OnConfigureServicesMethodFound(method);
}

// In the future we can consider looking at more methods, but for now limit to Main, implicit Main, and Configure* methods
var isMain = SymbolEqualityComparer.Default.Equals(entryPoint, context.OwningSymbol);

if (isConfigureServices || isMain)
{
services.AnalyzeConfigureServices(context);
options.AnalyzeConfigureServices(context);
}

if (StartupFacts.IsConfigure(symbols, method))
var isConfigure = StartupFacts.IsConfigure(symbols, method);
if (isConfigure)
{
OnConfigureMethodFound(method);

}
if (isConfigure || isMain)
{
middleware.AnalyzeConfigureMethod(context);
}
});
Expand Down
1 change: 0 additions & 1 deletion src/Analyzers/Analyzers/src/UseAuthorizationAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public UseAuthorizationAnalyzer(StartupAnalysis context)
public void AnalyzeSymbol(SymbolAnalysisContext context)
{
Debug.Assert(context.Symbol.Kind == SymbolKind.NamedType);
Debug.Assert(StartupFacts.IsStartupClass(_context.StartupSymbols, (INamedTypeSymbol)context.Symbol));

var type = (INamedTypeSymbol)context.Symbol;

Expand Down
3 changes: 1 addition & 2 deletions src/Analyzers/Analyzers/src/UseMvcAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

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

var type = (INamedTypeSymbol)context.Symbol;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ namespace Microsoft.AspNetCore.Analyzers
{
internal class AnalyzersDiagnosticAnalyzerRunner : DiagnosticAnalyzerRunner
{
public AnalyzersDiagnosticAnalyzerRunner(DiagnosticAnalyzer analyzer)
private readonly OutputKind _outputKind;

public AnalyzersDiagnosticAnalyzerRunner(DiagnosticAnalyzer analyzer, OutputKind? outputKind = null)
{
Analyzer = analyzer;
_outputKind = outputKind ?? OutputKind.DynamicallyLinkedLibrary;
}

public DiagnosticAnalyzer Analyzer { get; }
Expand Down Expand Up @@ -51,5 +54,10 @@ public Task<Diagnostic[]> GetDiagnosticsAsync(Project project)
{
return GetDiagnosticsAsync(new[] { project }, Analyzer, Array.Empty<string>());
}

protected override CompilationOptions ConfigureCompilationOptions(CompilationOptions options)
{
return options.WithOutputKind(_outputKind);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<Reference Include="Microsoft.AspNetCore.Components.Server" />
<Reference Include="Microsoft.AspNetCore.Mvc" />
<Reference Include="Microsoft.AspNetCore.SignalR" />
<Reference Include="Microsoft.AspNetCore" />
</ItemGroup>

</Project>
Loading