Skip to content

feat: create custom Roslyn analyzer for validation and diagnostics #22

Description

@j-d-ha

Problem Statement

Currently, validation and diagnostic reporting for Lambda.Host only occurs during the build phase through the source generator in Lambda.Host.SourceGenerators. This means developers don't get real-time feedback in their IDE about issues like:

  • Multiple method call violations (LH0001)
  • Multiple parameters of the same type (LH0002)

This delayed feedback slows down development and makes it harder to catch errors early.

Proposed Solution

Create a dedicated Roslyn analyzer project (Lambda.Host.Analyzers) that provides real-time diagnostics directly in the IDE. This will give developers immediate visual feedback (squiggly lines, warnings, errors) as they type, before they even build the project.

Technical Approach

  1. Create new project: Lambda.Host.Analyzers.csproj

    • Target netstandard2.0 (same as source generators)
    • Reference Microsoft.CodeAnalysis.CSharp (version 4.14.0 to match existing)
    • Set EnforceExtendedAnalyzerRules and IsRoslynComponent properties
  2. Extract validation logic from Lambda.Host.SourceGenerators:

    • Reuse existing DiagnosticDescriptor definitions from Diagnostics.cs
    • Port validation logic into analyzer-specific diagnostic analyzers
    • May need slight adjustments to work in the analyzer context vs. source generator context
  3. Implement Roslyn diagnostic analyzers:

    • Create syntax/semantic analyzers that detect problematic patterns
    • Report diagnostics using the same IDs (LH0001, LH0002, etc.)
    • Follow .NET analyzer best practices
  4. Package and distribute:

    • Configure as analyzer in .csproj with <IncludeBuildOutput>false</IncludeBuildOutput>
    • Ensure it's included in the Lambda.Host NuGet package

Acceptance Criteria

  • New Lambda.Host.Analyzers project created with proper configuration
  • All existing diagnostics (LH0001, LH0002) work in real-time in IDE
  • Diagnostics appear with appropriate severity levels (Error, Warning)
  • No duplicate diagnostics between analyzer and source generator
  • Unit tests for analyzer rules
  • Documentation updated with analyzer information
  • Analyzer included in NuGet package

Technical Notes

  • Most of the validation logic can be borrowed from the existing source generator
  • Some adjustments may be needed since analyzers operate on syntax/semantic model differently than source generators
  • Consider using incremental analyzers for better performance
  • Follow Roslyn analyzer guidelines: https://github.com/dotnet/roslyn-analyzers

Related Files

  • src/Lambda.Host.SourceGenerators/Diagnostics.cs - Existing diagnostic definitions
  • src/Lambda.Host.SourceGenerators/MapHandlerSourceOutput.cs - Contains validation logic to extract

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions