Skip to content

Improve LoggerMessageGenerator and JsonSourceGenerator incrementality#124077

Merged
tarekgh merged 26 commits intomainfrom
copilot/fix-logger-message-generator-incrementality
Feb 9, 2026
Merged

Improve LoggerMessageGenerator and JsonSourceGenerator incrementality#124077
tarekgh merged 26 commits intomainfrom
copilot/fix-logger-message-generator-incrementality

Conversation

Copy link
Contributor

Copilot AI commented Feb 6, 2026

Description

Both generators were passing Compilation through their pipelines, causing full re-runs on any compilation change rather than only when attributed syntax nodes changed. This PR refactors both generators to follow proper incremental generator patterns.

Changes Made

LoggerMessageGenerator:

  • Moved parsing logic into ForAttributeWithMetadataName transform, returning immutable specs with structural equality
  • Added WithTrackingName(StepNames.LoggerMessageTransform) for Roslyn 4.4+
  • Execute merges methods from different partial class files using HashSet for O(1) deduplication
  • Execute deduplicates diagnostics using HashSet
  • Reports MissingRequiredType diagnostic when System.Exception is unavailable in both Roslyn 3.11 and 4.0+ paths
  • Fixed multiple bugs (N×N duplication, nested class collisions, null handling, partial class merging)
  • Included polyfills and helpers for netstandard2.0
  • Maintained Roslyn 3.11 compatibility

JsonSourceGenerator:

  • Removed CompilationProvider dependency
  • Moved KnownTypeSymbols into transform

Testing:

  • Added incremental generation tests (Roslyn 4.8+)
  • Tests use proper tree tracking and consistent parse options
  • Tests use Assert.True for tracking validation
  • Fixed WithDiagnostics files (internal methods)

Pragma Suppression Behavioral Note

In the test .targets and .WithDiagnostics.cs files, the PR adds project-level <NoWarn> for SYSLIB1002;SYSLIB1013;SYSLIB1018;8795 and <WarningsNotAsErrors>8795</WarningsNotAsErrors>. This is because DiagnosticInfo.GetTrimmedLocation creates locations without SyntaxTree references, breaking #pragma warning disable matching. While this only affects the test project directly, the same DiagnosticInfo pattern is used in the production Roslyn 4.0 generator path, meaning end-users of the LoggerMessageGenerator will lose the ability to suppress diagnostics via #pragma directives. This is a behavioral regression. The existing System.Text.Json generator already has this limitation too, so the Logging source generator will start to have the same behavior as System.Text.Json.

Performance Impact

Generator caches pipeline values and only reprocesses when attributed methods change.

Testing

  • ✅ All generator variants build successfully (Roslyn 3.11, 4.0, 4.4, 4.8)
  • ✅ Test project builds without errors
  • ✅ Incremental tests use consistent language versions
  • ✅ MissingExceptionType test passes in both Roslyn 3.11 and 4.0+ paths
  • ✅ Optimized partial class merging
  • ✅ Fixed CI failures
Original prompt

This section details on the original issue you should resolve

<issue_title>Improve LoggerMessageGenerator incrementality</issue_title>
<issue_description>### Description

IncrementalValueProvider<(Compilation, ImmutableArray<ClassDeclarationSyntax>)> compilationAndClasses =
context.CompilationProvider.Combine(classDeclarations.Collect());

I think having the Compilation in the generator pipeline will cause it to fully run on every change, so this is losing the benefits of being incremental.

cc @333fred

Reproduction Steps

Expected behavior

Generator pipeline shouldn't have a Compilation or ISymbols, etc.

Actual behavior

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response</issue_description>

Comments on the Issue (you are @copilot in this section)

@ Tagging subscribers to this area: @dotnet/area-extensions-logging See info in [area-owners.md](https://github.com/dotnet/runtime/blob/main/docs/area-owners.md) if you want to be subscribed.
Issue Details

Description

IncrementalValueProvider<(Compilation, ImmutableArray<ClassDeclarationSyntax>)> compilationAndClasses =
context.CompilationProvider.Combine(classDeclarations.Collect());

I think having the Compilation in the generator pipeline will cause it to fully run on every change, so this is losing the benefits of being incremental.

cc @333fred

Reproduction Steps

Expected behavior

Generator pipeline shouldn't have a Compilation or ISymbols, etc.

Actual behavior

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: Youssef1313
Assignees: -
Labels:

untriaged, area-Extensions-Logging

Milestone: -
@eerhardt Note that the exact same line exists in the System.Text.Json source generator:

IncrementalValueProvider<(Compilation, ImmutableArray<ClassDeclarationSyntax>)> compilationAndClasses =
context.CompilationProvider.Combine(classDeclarations.Collect());

So if a change is needed here - it should be made in both places.

cc @CyrusNajmabadi @joperezr </comment_new>
<comment_new>@Youssef1313
Yeah, I think a change is needed to JsonSourceGenerator too.</comment_new>
<comment_new>@ericstj
Related #93309

We discussed improving the incremental characteristics of the runtime source generators and scoped it out of 9.0</comment_new>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits February 6, 2026 09:18
Removed Compilation from generator pipelines to improve incrementality.
- LoggerMessageGenerator: Extract well-known symbols in ForAttributeWithMetadataName transform
- JsonSourceGenerator: Move KnownTypeSymbols creation into ForAttributeWithMetadataName transform
- Both generators now avoid Combine with CompilationProvider

Co-authored-by: cincuranet <[email protected]>
- Remove conditional using directive for GetBestTypeByMetadataName extension
- Update Roslyn 3.11 generator to use new Parser and Emitter APIs
- Both generators now build successfully

Co-authored-by: cincuranet <[email protected]>
- Change IsBaseOrIdentity to take Compilation instead of SemanticModel
- Simplify Execute method by removing unnecessary grouping
- Add clarifying comment in Roslyn 3.11 generator

Co-authored-by: cincuranet <[email protected]>
Copilot AI changed the title [WIP] Fix LoggerMessageGenerator incrementality issue Improve LoggerMessageGenerator and JsonSourceGenerator incrementality Feb 6, 2026
Copilot AI requested a review from cincuranet February 6, 2026 09:24
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Feb 6, 2026
@cincuranet cincuranet added area-Extensions-Logging and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Feb 6, 2026
@tarekgh
Copy link
Member

tarekgh commented Feb 6, 2026

CC @eiriktsarpalis

@tarekgh
Copy link
Member

tarekgh commented Feb 6, 2026

@copilot is it possible to get the benchmark numbers showing the improvement?

Move all parsing logic into ForAttributeWithMetadataName transform to extract
value-based LoggerClass data. No longer pass SemanticModel or INamedTypeSymbol
references through the incremental pipeline, which was causing cache misses.

Parser now runs in the transform and returns LoggerClass instances directly.
Execute step merges classes and emits code without needing symbols.

Co-authored-by: tarekgh <[email protected]>
Copy link
Contributor Author

Copilot AI commented Feb 6, 2026

@copilot is it possible to get the benchmark numbers showing the improvement?

The improvement is in build-time incrementality rather than runtime performance. The generator now avoids full re-runs when unrelated code changes (e.g., changing a method body that doesn't affect logger attributes).

To measure this, you'd need to:

  1. Create a project with logger methods
  2. Make an unrelated code change
  3. Compare generator re-run behavior (before: re-runs on every compilation change, after: only re-runs when attributed syntax changes)

Roslyn's incremental generator infrastructure doesn't expose built-in metrics for this. The improvement is structural - by removing SemanticModel/ISymbol from the pipeline (which change on every compilation), the pipeline can now properly cache LoggerClass instances and only reprocess when the attributed methods actually change.

Would you like me to create a test project demonstrating the reduced re-runs, or would the structural fix verification be sufficient?

Copilot AI and others added 2 commits February 6, 2026 16:55
…ural equality

- Add LoggerClassSpec, LoggerMethodSpec, LoggerParameterSpec as immutable records
- Implement IEquatable with structural equality using ImmutableEquatableArray
- Keep original mutable classes for parsing, convert to Spec in transform
- Add FromSpec method to convert back to mutable classes for emission
- Include required polyfills (IsExternalInit, RequiredMember, HashHelpers, etc.)

Co-authored-by: tarekgh <[email protected]>
- Use Key.Equals and Value.Equals instead of EqualityComparer
- Remove unnecessary null-conditional operators in GetHashCode
- Consistent with IEquatable constraints

Co-authored-by: tarekgh <[email protected]>
@tarekgh
Copy link
Member

tarekgh commented Feb 8, 2026

@copilot please fix the failure:

Discovering: Microsoft.Extensions.Logging.Generators.Roslyn3.11.Tests (method display = ClassAndMethod, method display options = None)
Discovered: Microsoft.Extensions.Logging.Generators.Roslyn3.11.Tests (found 78 of 79 test cases)
Starting: Microsoft.Extensions.Logging.Generators.Roslyn3.11.Tests (parallel test collections = on [2 threads], stop on fail = off)
Microsoft.Extensions.Logging.Generators.Tests.LoggerMessageGeneratorParserTests.MissingExceptionType [FAIL]
Assert.Single() Failure: The collection was empty
Stack Trace:
/_/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs(627,0): at Microsoft.Extensions.Logging.Generators.Tests.LoggerMessageGeneratorParserTests.MissingExceptionType()
--- End of stack trace from previous location ---
Finished: Microsoft.Extensions.Logging.Generators.Roslyn3.11.Tests

The Roslyn 3.11 generator was silently returning when System.Exception was
unavailable, instead of reporting the MissingRequiredType diagnostic. Split
the combined null check to handle exceptionSymbol separately, matching the
original Parser behavior that reported the diagnostic before returning.

Co-authored-by: tarekgh <[email protected]>
Copy link
Contributor Author

Copilot AI commented Feb 8, 2026

@copilot please fix the failure:

Discovering: Microsoft.Extensions.Logging.Generators.Roslyn3.11.Tests (method display = ClassAndMethod, method display options = None)
...

Fixed in bfe3713. The Roslyn 3.11 path had a combined null check that silently returned when System.Exception was unavailable, instead of reporting the MissingRequiredType diagnostic. Split the check to handle exceptionSymbol == null separately with context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.MissingRequiredType, null, "System.Exception")), matching the original Parser.GetLogClasses behavior.

@tarekgh
Copy link
Member

tarekgh commented Feb 9, 2026

@copilot run the code review skill

Copy link
Contributor Author

Copilot AI commented Feb 9, 2026

@copilot run the code review skill

Code review completed. Here are the findings:

No ❌ errors found.

⚠️ warning — .Collect() defeats per-item incrementality for source output

Line 102 in LoggerMessageGenerator.Roslyn4.0.cs:

context.RegisterSourceOutput(loggerClasses.Collect(), static (spc, items) => Execute(items, spc));

Using .Collect() gathers all items into a single ImmutableArray, which means the Execute callback is invoked once with the entire collection whenever any item changes. This is necessary here because the generator produces a single combined output file (LoggerMessage.g.cs), but it does mean Execute re-runs and re-emits the entire file on any change to any attributed method. The incremental benefit comes from the transform step (which properly caches per-method via WithTrackingName), so the parsing work is properly skipped — but the emission step is not incremental. This matches the existing behavior (the old code also produced one combined file) and is an acceptable tradeoff. No action needed.

⚠️ warning — existingMethodKeys HashSet is rebuilt on every merge iteration

Lines 151-155: When merging partial class methods, a new HashSet<(string Name, int EventId)> is built from existingClass.Methods for each incoming LoggerClassSpec with the same key. In practice, partial class files are uncommon and have few methods, so this is fine. But if a future user has many partial files for a single class, it would be more efficient to maintain the set across merge iterations. Low priority, follow-up material.

💡 suggestion — BuildClassKey allocates a List<string> on every call

Lines 182-194: BuildClassKey creates a List<string>, reverses it, and joins it. Could use a StringBuilder or walk the chain in reverse to avoid the list allocation. Minor optimization for a cold path — follow-up at best.

💡 suggestion — LoggerClassSpec uses record with manual Equals/GetHashCode

The spec types are declared as sealed record but provide manual IEquatable<T> implementations. C# records auto-generate equality, so declaring custom Equals/GetHashCode overrides the record's default. This works correctly, but using sealed class instead of sealed record would be slightly less surprising since the record-generated equality is never used. This is cosmetic — no action needed.

Verified correct:

  • Spec records implement proper structural equality covering all fields including recursive ParentClass
  • ImmutableEquatableArray<T> provides proper sequence equality for pipeline caching
  • DiagnosticInfo with trimmed locations is consistent with the System.Text.Json generator pattern
  • Partial class merging using HashSet<(string Name, int EventId)> handles the multi-file scenario correctly
  • Diagnostic deduplication using HashSet<DiagnosticInfo> prevents duplicate reporting
  • MissingRequiredType diagnostic is correctly reported in both Roslyn 3.11 and 4.0+ paths
  • OrderBy(key, StringComparer.Ordinal) preserves deterministic output
  • JsonSourceGenerator change is minimal and correct — just moves KnownTypeSymbols into the transform
  • Roslyn 3.11 backward compatibility maintained
  • Incremental tests properly use existingTree.Options for consistent language versions
  • WithDiagnostics test files correctly use internal methods to avoid CS8795

@copilot run the code review skill

Code review completed. Found and fixed one issue in fdae4aa:

⚠️ hasStringCreate uses = instead of |= — In Execute, hasStringCreate = item.HasStringCreate overwrites on each item. While all items from the same compilation agree, |= is more correct-by-construction and costs nothing.

💡 TemplateMap ordering (follow-up, not blocking) — LoggerMethod.ToSpec() creates TemplateMap from Dictionary iteration order which isn't guaranteed by contract. ImmutableEquatableArray.Equals uses SequenceEqual, so different iteration orders could cause false cache misses. Sorting by key would make it deterministic. In practice this is unlikely to cause issues since the same method processed the same way produces the same order.

No errors found. All other aspects look correct:

  • ✅ Spec records' Equals/GetHashCode are complete, covering all fields including recursive ParentClass
  • ✅ Partial class merging via BuildClassKey + HashSet<(Name, EventId)> dedup is correct
  • Diag() double-reporting is safe — _reportDiagnostic is null in Roslyn 4.0+ (only collects), non-null in Roslyn 3.11 (reports immediately)
  • default return from transform correctly handled via item.Diagnostics is not null check
  • ✅ Diagnostic deduplication via HashSet<DiagnosticInfo> prevents duplicates
  • ✅ Deterministic output via OrderBy(key, StringComparer.Ordinal)
  • ✅ MissingRequiredType diagnostic reported in both Roslyn 3.11 and 4.0+ paths
  • ✅ JsonSourceGenerator change is minimal and correct
  • ✅ Test parse options consistency is correct

All items from the same compilation should agree on hasStringCreate, but
using |= instead of = is more resilient and costs nothing.

Co-authored-by: tarekgh <[email protected]>
Copilot AI requested a review from tarekgh February 9, 2026 03:54
@tarekgh tarekgh merged commit 5709f35 into main Feb 9, 2026
86 checks passed
Copilot AI added a commit that referenced this pull request Feb 9, 2026
Restore the original pattern where KnownTypeSymbols is created via
CompilationProvider to maintain 1-1 correspondence between Compilation
and KnownTypeSymbols instances, avoiding performance regression.

Co-authored-by: eiriktsarpalis <[email protected]>
@eiriktsarpalis eiriktsarpalis deleted the copilot/fix-logger-message-generator-incrementality branch February 9, 2026 18:53
@akoeplinger
Copy link
Member

@tarekgh do you have a roslyn tracking issue for the #pragma warning disable issue? we also hit it in the codeflow PR: dotnet/dotnet#4727 (comment)

We'll likely also need to file some breaking change notice since we document the pragma approach in https://learn.microsoft.com/en-us/dotnet/fundamentals/syslib-diagnostics/syslib1025

/cc @ericstj

@akoeplinger
Copy link
Member

I found #92509 and dotnet/roslyn#68291 which have more background on the issue

@tarekgh
Copy link
Member

tarekgh commented Feb 10, 2026

Right, #92509 is the tracking issue for different generators. I'll file the breaking change doc.

@tarekgh tarekgh added the breaking-change Issue or PR that represents a breaking API or functional change over a previous release. label Feb 10, 2026
@dotnet-policy-service dotnet-policy-service bot added the needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet label Feb 10, 2026
@github-actions
Copy link
Contributor

📋 Breaking Change Documentation Required

Create a breaking change issue with AI-generated content

Generated by Breaking Change Documentation Tool - 2026-02-10 15:02:39

@tarekgh
Copy link
Member

tarekgh commented Feb 10, 2026

I filed the breaking change doc and linked it to this PR.

@akoeplinger thanks for pointing at the failures and sorry for the inconvenience. Please let me know if there is anything else I can help with.

@tarekgh tarekgh removed the needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet label Feb 10, 2026
eiriktsarpalis added a commit that referenced this pull request Feb 11, 2026
…4186)

## Description

Reverts System.Text.Json changes from PR #124077 per @eiriktsarpalis
feedback. The change caused a performance regression by breaking the 1-1
correspondence between `Compilation` and `KnownTypeSymbols` instances,
creating fresh instances for every `JsonSerializerContext` instead of
caching per compilation.

LoggerMessageGenerator changes remain unchanged.

## Changes

Restored the original incremental pipeline pattern in
`JsonSourceGenerator.Roslyn4.0.cs`:

**Before (PR #124077):**
```csharp
contextGenerationSpecs = context.SyntaxProvider
    .ForAttributeWithMetadataName(
        ...,
        (context, cancellationToken) =>
        {
            var knownTypeSymbols = new KnownTypeSymbols(context.SemanticModel.Compilation);
            Parser parser = new(knownTypeSymbols);
            // ... parsing
        })
```

**After (this PR):**
```csharp
IncrementalValueProvider<KnownTypeSymbols> knownTypeSymbols = context.CompilationProvider
    .Select((compilation, _) => new KnownTypeSymbols(compilation));

contextGenerationSpecs = context.SyntaxProvider
    .ForAttributeWithMetadataName(
        ...,
        (context, _) => (ContextClass: context.TargetNode, context.SemanticModel))
    .Combine(knownTypeSymbols)
    .Select(static (tuple, cancellationToken) =>
    {
        Parser parser = new(tuple.Right);  // KnownTypeSymbols
        // ... parsing using tuple.Left.ContextClass, tuple.Left.SemanticModel
    })
```

This ensures `KnownTypeSymbols` is cached per compilation in the
incremental pipeline, eliminating redundant type symbol resolution.

## Testing

- Built clr+libs successfully
- All 154 System.Text.Json.SourceGeneration unit tests pass

<!-- START COPILOT ORIGINAL PROMPT -->



<details>

<summary>Original prompt</summary>

> ## Context
> 
> PR #124077 ("Improve LoggerMessageGenerator and JsonSourceGenerator
incrementality") was merged and included changes to both the
`LoggerMessageGenerator` and `JsonSourceGenerator`. However, per
feedback from @eiriktsarpalis in
#124077 (comment),
the System.Text.Json changes should be reverted because:
> 
> 1. Passing `Compilation` in the incremental graph does not
automatically imply re-runs of the source generator every time it gets
updated. This is only true in cases where such updates flow all the way
to the `SourceProductionContext`. The STJ generator does not do this
(source production is only triggered on the basis of equality of the
resultant `ContextGenerationSpec`) and they have testing that validates
that property.
> 2. More importantly, the change actually **regresses performance**:
`KnownTypeSymbols` defines a cache of known type symbols that the
generator needs to reference. By removing it from the incremental graph,
there is no longer a 1-1 correspondence between `Compilation` instances
and `KnownTypeSymbols` instances, instead creating a fresh instance
every time a `JsonSerializerContext` is processed for a given
compilation.
> 
> ## What needs to be done
> 
> Revert ONLY the changes to
`src/libraries/System.Text.Json/gen/JsonSourceGenerator.Roslyn4.0.cs`
introduced by PR #124077 (merge commit
`5709f35ed344f59062042d234b9a68683ec7e7a6`).
> 
> The file was changed to:
> - Remove the `CompilationProvider` dependency and the
`knownTypeSymbols` incremental value provider
> - Move `KnownTypeSymbols` instantiation into the
`ForAttributeWithMetadataName` transform callback
> - Restructure the pipeline to no longer use
`.Combine(knownTypeSymbols)` and `.Select()`
> 
> The revert should restore the original pattern where:
> - `KnownTypeSymbols` is created via
`context.CompilationProvider.Select(...)` as an
`IncrementalValueProvider<KnownTypeSymbols>`
> - The attribute provider uses `.Combine(knownTypeSymbols)` 
> - A subsequent `.Select()` performs the parsing using `tuple.Left`
(for the context class + semantic model) and `tuple.Right` (for the
known type symbols)
> - The `ForAttributeWithMetadataName` transform only extracts
`(ContextClass, SemanticModel)` without doing any parsing
> 
> **Do NOT revert any LoggerMessageGenerator changes** — those are
correct and should remain as-is.
> 


</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

*This pull request was created from Copilot chat.*
>

<!-- START COPILOT CODING AGENT TIPS -->
---

✨ Let Copilot coding agent [set things up for
you](https://github.com/dotnet/runtime/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: eiriktsarpalis <[email protected]>
Co-authored-by: Eirik Tsarpalis <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Extensions-Logging breaking-change Issue or PR that represents a breaking API or functional change over a previous release. source-generator Indicates an issue with a source generator feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve LoggerMessageGenerator incrementality

8 participants