Skip to content

Fix LoggerMessage generator copying unbalanced directive trivia - #133483

Merged
rosebyte merged 8 commits into
mainfrom
copilot/fix-cs1027-logging-source-generator
Nov 4, 2026
Merged

rosebyte merged 8 commits into
mainfrom
copilot/fix-cs1027-logging-source-generator

Conversation

Copilot AI commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

The LoggerMessage generator copies internal #if trivia without its matching #endif when conditional boundaries split method modifiers or target/enclosing generic headers, producing CS1027 from otherwise valid source.

  • Parser: Reconstruct modifiers and generic headers from active token text, preserving escaped identifiers while excluding trivia and generic-parameter attributes.
  • Regression coverage: Compile generated output for both conditional branches, including differing signatures, escaped identifiers, attributes, and comments.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
16 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/runtime-infrastructure
See info in area-owners.md if you want to be subscribed.

Copilot AI changed the title [WIP] Fix CS1027 issue in LoggerMessage source generator Fix LoggerMessage generator copying unbalanced directive trivia Sep 9, 2026
Copilot AI requested a review from rosebyte September 9, 2026 11:49
@rosebyte
rosebyte marked this pull request as ready for review September 10, 2026 06:49
Copilot AI lite review requested due to automatic review settings September 10, 2026 06:49
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The changes are narrowly targeted to eliminate trivia copying in generated declarations and are backed by regression tests that compile the generated output in both conditional branches.

Pull request overview

This PR fixes a LoggerMessage source generator failure mode where preprocessor directive trivia (#if/#else) could be copied into generated declarations without the matching #endif, producing invalid generated C# (CS1027). It adjusts how the generator reconstructs method modifiers and generic type headers to use token text only (excluding trivia), and adds regression tests that compile the generated output under both symbol configurations.

Changes:

  • Reconstruct logging method modifiers from modifier token text (SyntaxToken.Text) instead of SyntaxToken.ToString() to avoid copying directive trivia.
  • Reconstruct type names with generic parameters by joining only type-parameter identifiers (excluding attributes and directive trivia).
  • Add regression tests that validate the generated output compiles for both defined/undefined conditional symbols across several tricky declaration shapes.
File summaries
File Description
src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs Avoid copying directive trivia when capturing method modifiers and generic type headers used in generated partial declarations.
src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorEmitterTests.cs Add compile-based regression tests covering conditional directive boundaries in modifiers and generic headers (including escaped identifiers and generic-parameter attributes).
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings September 21, 2026 07:44
@rosebyte
rosebyte enabled auto-merge (squash) September 21, 2026 07:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

No unresolved issues were identified.

Review effort: Lite
Findings: None

@akoeplinger

Copy link
Copy Markdown
Member

@rosebyte there are some test failures that look related

@rosebyte

rosebyte commented Nov 4, 2026

Copy link
Copy Markdown
Member

@akoeplinger, you're right, the issue was on the changes side, not the CI automation, it should be fixed now.

@rosebyte
rosebyte merged commit 20fa71f into main Nov 4, 2026
83 of 84 checks passed
@rosebyte
rosebyte deleted the copilot/fix-cs1027-logging-source-generator branch November 4, 2026 14:54
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Logging source generator produces CS1027 for conditional modifiers and generic headers

5 participants