Repository navigation
Fix LoggerMessage generator copying unbalanced directive trivia - #133483
Conversation
|
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. |
|
Tagging subscribers to this area: @dotnet/runtime-infrastructure |
Co-authored-by: rosebyte <[email protected]>
|
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. |
There was a problem hiding this comment.
🟢 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 ofSyntaxToken.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
|
@rosebyte there are some test failures that look related |
|
@akoeplinger, you're right, the issue was on the changes side, not the CI automation, it should be fixed now. |
The
LoggerMessagegenerator copies internal#iftrivia without its matching#endifwhen conditional boundaries split method modifiers or target/enclosing generic headers, producing CS1027 from otherwise valid source.