Escape C# keyword identifiers in Configuration Binder source generator - #131453
Escape C# keyword identifiers in Configuration Binder source generator#131453rosebyte wants to merge 3 commits into
Conversation
The Configuration Binder source generator emitted constructor parameter names verbatim into identifier position without escaping C# keywords. When a model had keyword-named parameters (e.g. @base, @event), the generated code would not compile. Add an EscapeIdentifier helper (matching the pattern already used in the Options source generator) that prefixes identifiers with '@' when they are C# keywords or contextual keywords. Apply it at all emission points where member names appear in code position. Fixes dotnet#130433 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
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. |
|
Tagging subscribers to this area: @dotnet/area-extensions-configuration |
There was a problem hiding this comment.
Pull request overview
Fixes the Configuration Binder source generator so it emits valid C# when constructor parameters or members have names that are C# keywords (e.g., base, event) by escaping identifiers with @ where needed.
Changes:
- Added an
EscapeIdentifierhelper to prefix C# keyword/contextual-keyword identifiers with@. - Updated
CoreBindingHelpersemission sites to use escaped identifiers for locals and member access/initializers. - Added a regression test that validates generated code compiles for a model with keyword-named constructor parameters.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs | Uses escaped identifiers when emitting locals and member access/initializers in generated binding code. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/Helpers.cs | Introduces EscapeIdentifier helper (Roslyn SyntaxFacts-based) for keyword/contextual-keyword detection. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/GeneratorTests.cs | Adds a compilation-based regression test covering keyword-named constructor parameters (and should ideally also cover keyword-named properties). |
|
It seems configuration key names also need escaping, e.g.: @rosebyte Should I open an issue for that, or do you want to include that in this PR? |
|
Also found out with copilot the same, here is demo. Remaining missing cases identified by Copilot CLI on my machine are non-senses. Work without source generator. Do not work with enabled. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (2)
src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs:11
using Microsoft.CodeAnalysis.CSharp;appears to be unused in this file (noSyntaxFacts/SyntaxKindreferences). This can trigger IDE0005 in builds that enforce unused-usings removal; please remove it.
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using SourceGenerators;
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs:205
- The PR description lists only the generator + generator-test files, but this PR also changes binder runtime tests (and adds coverage for configuration key string-literal escaping). Please update the PR description/file list (or split into a follow-up) so the scope matches what’s actually being changed.
[Fact]
public void CanBindConfigurationKeyNamesRequiringEscaping()
{
var dic = new Dictionary<string, string>
|
@svick, @mrek-msft, I'd prefer to make another issue but it seems the change is only scoped to the one file so we might just mention it in PR description and perhaps the issue that we did escape this too, what do you think? |
Fix Configuration Binder source generator emitting unescaped C# keywords as identifiers
The Configuration Binder source generator emitted constructor parameter and property names verbatim into identifier positions without escaping C# keywords. When a model had keyword-named parameters (e.g.
@base,@event), the generated code would not compile.Changes
EscapeIdentifierhelper (matching the pattern already used in the Options source generator) that prefixes identifiers with@when they are C# keywords or contextual keywords.CoreBindingHelpers.cs.Files changed
src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cssrc/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/Helpers.cssrc/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/GeneratorTests.csFixes #130433